在 Python 中返回不同指數的底數
要返回當第一個陣列元素被第二個陣列中的冪提升時得到的底數,可以使用 Python Numpy 中的 float_power() 方法。該方法返回 x1 中的底數提升到 x2 中的指數冪的結果。如果 x1 和 x2 都是標量,則結果也是標量。引數 x1 是底數。引數 x2 是指數。
將 x1 中的每個底數提升到 x2 中位置對應的冪。x1 和 x2 必須能夠廣播到相同的形狀。這與 power 函式不同,因為整數、float16 和 float32 會提升到具有至少 float64 精度的浮點數,以便結果始終不精確。目的是該函式將為負冪返回可用結果,並且很少為正冪溢位。負數提升到非整數冪將返回 nan。要獲得複數結果,請將輸入轉換為複數,或將 dtype 指定為複數。
步驟
首先,匯入所需的庫 -
import numpy as np
底數 -
x1 = range(6)
顯示底數 -
print("The bases...\n",x1)指數 -
x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
顯示指數 -
print("\nThe exponents...\n",x2)要返回當第一個陣列元素被第二個陣列中的冪提升時得到的底數,可以使用 float_power() 方法 -
print("\nResult...\n",np.float_power(x1, x2))
示例
import numpy as np
# The bases
x1 = range(6)
# Display the bases
print("The bases...\n",x1)
# The exponents
x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
# Display the exponents
print("\nThe exponents...\n",x2)
# To return the bases when first array elements are raised to powers from second array, use the float_power() method in Python Numpy
# The method returns the bases in x1 raised to the exponents in x2. This is a scalar if both x1 and x2 are scalars.
print("\nResult...\n",np.float_power(x1, x2))輸出
The bases... range(0, 6) The exponents... [1.0, 2.0, 3.0, 3.0, 2.0, 1.0] Result... [ 0. 1. 8. 27. 16. 5.]
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP