NumPy中陣列的逐元素立方根
要在Python NumPy中逐元素返回陣列的立方根,請使用**numpy.cbrt()**方法。返回一個與x形狀相同的陣列,其中包含x中每個元素的立方根。如果提供了out,則y是對它的引用。如果x是標量,則這是一個標量。
此條件會廣播到輸入。在條件為True的位置,out陣列將設定為ufunc結果。在其他位置,out陣列將保留其原始值。請注意,如果透過預設的out=None建立未初始化的out陣列,則其中條件為False的位置將保持未初始化狀態。
NumPy 提供了全面的數學函式、隨機數生成器、線性代數例程、傅立葉變換等等。它支援各種硬體和計算平臺,並且可以很好地與分散式、GPU和稀疏陣列庫配合使用。
步驟
首先,匯入所需的庫:
import numpy as np
使用array()方法建立一個數組:
arr = np.array([125, 27, 1000, 100, 841, 225])
顯示陣列:
print("Array...
", arr)獲取陣列的型別:
print("
Our Array type...
", arr.dtype)
獲取陣列的維度:
print("
Our Array Dimension...
",arr.ndim)獲取陣列的形狀:
print("
Our Array Shape...
",arr.shape)
要在Python NumPy中逐元素返回陣列的立方根,請使用numpy.cbrt()方法:
print("
Result...
",np.cbrt(arr))示例
import numpy as np
# Create an array using the array() method
arr = np.array([125, 27, 1000, 100, 841, 225])
# 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 return the cube-root of an array, element-wise, use the numpy.cbrt() method in Python Numpy
print("
Result...
",np.cbrt(arr))輸出
Array... [ 125 27 1000 100 841 225] Our Array type... int64 Our Array Dimension... 1 Our Array Shape... (6,) Result... [ 5. 3. 10. 4.64158883 9.43913068 6.082202 ]
廣告
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP