在 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.