用 Python 中的 emath 計算輸入的平方根
要計算輸入的平方根,請在 Python Numpy 中使用 scimath.sqrt() 方法。該方法返回 x 的平方根。如果 x 是標量,out 也是標量;否則會返回一個數組。引數 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 是標量,out 也是標量;否則返回一個數組 −
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.]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP