在 NumPy 中返回陣列的非負平方根(逐元素計算)


要返回陣列的非負平方根(逐元素計算),請在 Python NumPy 中使用 **numpy.sqrt()** 方法。

一個與 x 形狀相同的陣列,包含 x 中每個元素的正平方根。如果 x 中的任何元素是複數,則返回一個複數陣列(並計算負實數的平方根)。如果 x 中的所有元素都是實數,則 y 也是實數,負元素返回 nan。如果提供了 out,則 y 是對它的引用。如果 x 是標量,則這是一個標量。

out 是儲存結果的位置。如果提供,則其形狀必須是輸入廣播到的形狀。如果沒有提供或為 None,則返回一個新分配的陣列。元組(僅可能作為關鍵字引數)的長度必須等於輸出的數量。

步驟

首先,匯入所需的庫 -

import numpy as np

使用 array() 方法建立一個數組 -

arr = np.array([4, 9, 10000, 100, 841, 225])

顯示陣列 -

print("Array...
", arr)

獲取陣列的型別 -

print("
Our Array type...
", arr.dtype)

獲取陣列的維度 -

print("
Our Array Dimension...
",arr.ndim)

獲取陣列的形狀 -

print("
Our Array Shape...
",arr.shape)

要返回陣列的非負平方根(逐元素計算),請使用 numpy.sqrt() 方法 -

print("
Result...
",np.sqrt(arr))

示例

import numpy as np

# Create an array using the array() method
arr = np.array([4, 9, 10000, 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 non-negative square-root of an array, element-wise, use the numpy.sqrt() method in Python Numpy print("
Result...
",np.sqrt(arr))

輸出

Array...
[ 4 9 10000 100 841 225]

Our Array type...
int64

Our Array Dimension...
1

Our Array Shape...
(6,)

Result...
[ 2. 3. 100. 10. 29. 15.]

更新於: 2022年2月8日

224 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.