在 Python 中返回複數值輸入的以 2 為底的對數
要返回輸入陣列的以 2 為底的對數,請在 Python Numpy 中使用 numpy.log2() 方法。該方法返回 x 的以 2 為底的對數。如果 x 是標量,則為標量。第一個引數 x 是輸入值,類似陣列。第二個引數是 out,結果儲存到的位置。如果提供,則其形狀必須與輸入廣播到的形狀相同。如果未提供或為 None,則返回一個新分配的陣列。元組(僅作為關鍵字引數可能)的長度必須等於輸出的數量。
第三個引數是 where,條件在輸入上廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他地方,out 陣列將保留其原始值。請注意,如果透過預設的 out=None 建立了一個未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化。
步驟
首先,匯入所需的庫 -
import numpy as np
使用 array() 方法建立陣列 -
arr = np.array([0+1.j, 1, 2+0.j])
顯示陣列 -
print("Array...\n", arr)獲取陣列的型別 -
print("\nOur Array type...\n", arr.dtype)
獲取陣列的維度 -
print("\nOur Array Dimension...\n",arr.ndim)獲取陣列的形狀 -
print("\nOur Array Shape...\n",arr.shape)要返回輸入陣列的以 2 為底的對數,請在 Python Numpy 中使用 numpy.log2() 方法 -
print("\nResult...\n",np.log2(arr))示例
import numpy as np
# Create an array using the array() method
arr = np.array([0+1.j, 1, 2+0.j])
# Display the array
print("Array...\n", arr)
# Get the type of the array
print("\nOur Array type...\n", arr.dtype)
# Get the dimensions of the Array
print("\nOur Array Dimension...\n",arr.ndim)
# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)
# To return the base 2 logarithm of the input array, use the numpy.log2() method in Python Numpy
# The method returns Base-2 logarithm of x. This is a scalar if x is a scalar.
print("\nResult...\n",np.log2(arr))輸出
Array... [0.+1.j 1.+0.j 2.+0.j] Our Array type... complex128 Our Array Dimension... 1 Our Array Shape... (3,) Result... [0.+2.26618007j 0.+0.j 1.+0.j ]
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP