在NumPy中將底數提升到不同的指數
要將底數提升到不同的指數,請在Python中使用**numpy.power()**方法。這裡,第一個引數是底數,第二個引數是指數。
將x1中的每個底數提升到x2中位置對應的冪。x1和x2必須可廣播到相同的形狀。整數型別提升到負整數冪將引發ValueError。負值提升到非整數值將返回nan。要獲得複數結果,請將輸入轉換為複數,或指定dtype為複數。
該條件在輸入上進行廣播。在條件為True的位置,out陣列將設定為ufunc結果。在其他地方,out陣列將保留其原始值。請注意,如果透過預設的out=None建立一個未初始化的out陣列,則其中條件為False的位置將保持未初始化狀態。
步驟
首先,匯入所需的庫:
import numpy as np
建立一個數組:
arr = np.array([5, 10, 25, 30, 40, 50])
顯示陣列:
print("Array...
", arr)獲取陣列的型別:
print("
Our Array type...
", arr.dtype)
獲取陣列的維度:
print("
Our Array Dimension...
",arr.ndim)獲取陣列的形狀:
print("
Our Array Shape...
",arr.shape)
設定不同的指數:
p = [2, 3, 2, 3, 2, 3]
要將底數提升到不同的指數,請在Python中使用numpy.power()方法。這裡,第一個引數是底數,第二個引數是指數:
print("
Result...
",np.power(arr, p))
示例
import numpy as np
# Create an array
arr = np.array([5, 10, 25, 30, 40, 50])
# Display the array
print("Array...
", arr)
# Get the type of the array
print("
Our Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Our Array Dimension...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# Set the different exponent
p = [2, 3, 2, 3, 2, 3]
# To raise the bases to different exponents, use the numpy.power() method in Python
# Here, the 1st parameter is the base and the 2nd exponents
print("
Result...
",np.power(arr, p))輸出
Array... [ 5 10 25 30 40 50] Our Array type... int64 Our Array Dimension... 1 Our Array Shape... (6,) Result... [ 25 1000 625 27000 1600 125000]
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP