對NumPy陣列中的每個元素進行立方運算
要對陣列中的每個元素進行立方運算(逐元素運算),請使用Python中的**numpy.power()**方法。這裡,第一個引數是底數,第二個引數是指數。由於我們想要立方,因此指數為3。
將x1中的每個底數提升到x2中位置對應的冪。x1和x2必須能夠廣播到相同的形狀。整數型別提升到負整數冪將引發ValueError。負值提升到非整數值將返回nan。要獲得複數結果,請將輸入轉換為複數,或將dtype指定為複數。
步驟
首先,匯入所需的庫:
import numpy as np
建立一個數組:
arr = np.array([5, 10, 25, 7, 9])
顯示陣列:
print("Array...
", arr)獲取陣列的型別:
print("
Our Array type...
", arr.dtype)
獲取陣列的維度:
print("
Our Array Dimension...
",arr.ndim)獲取陣列的形狀:
print("
Our Array Shape...
",arr.shape)要對陣列中的每個元素進行立方運算(逐元素運算),請使用Python中的numpy.power()方法。這裡,第一個引數是底數,第二個引數是指數。由於我們想要立方,因此指數為3:
print("
Result...
",np.power(arr, 3))示例
import numpy as np
# Create an array
arr = np.array([5, 10, 25, 7, 9])
# 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)
# To cube each element in an array., element-wise, use the numpy.power() method in Python
# Here, the 1st parameter is the base and the 2nd exponents
# Since, we want the cube, the exponent is 3
print("
Result...
",np.power(arr, 3))輸出
Array... [ 5 10 25 7 9] Our Array type... int64 Our Array Dimension... 1 Our Array Shape... (5,) Result... [ 125 1000 15625 343 729]
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP