计算Python中输入值的平方根
要计算输入值的平方根,在Python Numpy中使用scimath.sqrt()方法。该方法返回x的平方根。如果x是标量,则输出也是标量,否则返回一个数组。参数x是输入值。对于负的输入元素,返回一个复数值。
步骤
首先,导入所需的库−
import numpy as np
使用array()方法创建一个numpy数组−
arr = np.array([1, 4, 9, 16, 25, 36])
显示数组 –
print("Our Array...\n",arr)
检查尺寸 –
print("\nDimensions of our Array...\n",arr.ndim)
获取数据类型 –
print("\nDatatype of our Array object...\n",arr.dtype)
获取形状 −
print("\nShape of our Array object...\n",arr.shape)
使用Python Numpy中的scimath.sqrt()方法计算输入的平方根。该方法返回x的平方根。如果x是标量,输出也是标量;如果x是数组,将返回一个数组-
print("\nResult...\n",np.emath.sqrt(arr))
示例
import numpy as np
# Creating a numpy array using the array() method
arr = np.array([1, 4, 9, 16, 25, 36])
# Display the array
print("Our Array...\n",arr)
# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)
# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)
# Get the Shape
print("\nShape of our Array object...\n",arr.shape)
# To compute the square root of input, use the scimath.sqrt() method in Python Numpy
# The method returns the square root of x. If x was a scalar, so is out, otherwise an array is returned.
print("\nResult...\n",np.emath.sqrt(arr))
输出
Our Array...
[ 1 4 9 16 25 36]
Dimensions of our Array...
1
Datatype of our Array object...
int64
Shape of our Array object...
(6,)
Result...
[1. 2. 3. 4. 5. 6.]