在 Python 中返回第一個陣列元素分別乘以第二個陣列元素的冪後的結果
要返回第一個陣列元素分別乘以第二個陣列元素的冪後的結果,可以使用 Python Numpy 中的 float_power() 方法。該方法返回 x1 中的底數乘以 x2 中的指數的結果。如果 x1 和 x2 都是標量,則結果為標量。引數 x1 為底數。引數 x2 為指數。
將 x1 中的每個底數提高到 x2 中位置對應的冪。x1 和 x2 必須能夠廣播到相同的形狀。這與冪函式的不同之處在於,整數、float16 和 float32 會提升到至少 float64 精度的浮點數,因此結果始終不精確。目的是該函式將為負冪返回可用結果,並且對於正冪很少溢位。
負數提高到非整數冪將返回 nan。要獲得複數結果,請將輸入轉換為複數,或將 dtype 指定為複數。
步驟
首先,匯入所需的庫:
import numpy as np
底數:
x1 = range(6)
顯示底數:
print("The bases...\n",x1)要返回第一個陣列元素分別乘以第二個陣列元素的冪後的結果,可以使用 Python Numpy 中的 float_power() 方法。該方法返回 x1 中的底數乘以 x2 中的指數的結果。如果 x1 和 x2 都是標量,則結果為標量:
print("\nResult...\n",np.float_power(x1, 2))
示例
import numpy as np
# The bases
x1 = range(6)
# Display the bases
print("The bases...\n",x1)
# To return the bases when first array elements are raised to powers from second array, use the float_power() method in Python Numpy
print("\nResult...\n",np.float_power(x1, 2))輸出
The bases... range(0, 6) Result... [ 0. 1. 4. 9. 16. 25.]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP