從 Numpy 中的掩碼陣列獲取虛數部分
要從掩碼陣列中獲取虛數部分,請使用 Numpy 中的ma.MaskedArray.imag屬性。此屬性是此 MaskedArray 虛數部分的一個檢視。
掩碼要麼是 nomask,表示關聯陣列的沒有值無效,要麼是布林陣列,用於確定關聯陣列的每個元素的值是否有效。
步驟
首先,匯入所需的庫 −
import numpy as np import numpy.ma as ma
使用 numpy.array() 方法建立複數元素陣列 −
arr = np.array([68.+4.j , 49.+7.j , 120.+2.j , 64.+0.j])
print("Array..
",arr)
print("
Get the imaginary part",arr.imag)
print("
Get the datatype
",arr.dtype)建立一個掩碼陣列並將其中的某些元素遮罩為無效 −
maskArr = ma.masked_array(arr, mask =[False, False, True, False])
print("
Our Masked Array
", maskArr)
print("
Our Masked Array type...
", maskArr.dtype)獲取掩碼陣列的維度 −
print("
Our Masked Array Dimensions...
",maskArr.ndim)
獲取掩碼陣列的形狀 −
print("
Our Masked Array Shape...
",maskArr.shape)獲取掩碼陣列的元素數 −
print("
Elements in the Masked Array...
",maskArr.size)
要從掩碼陣列中獲取虛數部分,請使用 Numpy 中的 ma.MaskedArray.imag 屬性 −
print("
Get the imaginary part...
",maskArr.imag)示例
import numpy as np
import numpy.ma as ma
# Creating an array of complex number elements using the numpy.array() method
arr = np.array([68.+4.j , 49.+7.j , 120.+2.j , 64.+0.j])
print("Array..
",arr)
print("
Get the imaginary part",arr.imag)
print("
Get the datatype
",arr.dtype)
print("
The number of elements
",arr.size)
# Create a masked array and mask some of them as invalid
maskArr = ma.masked_array(arr, mask =[False, False, True, False])
print("
Our Masked Array
", maskArr)
print("
Our Masked Array type...
", maskArr.dtype)
# Get the dimensions of the Masked Array
print("
Our Masked Array Dimensions...
",maskArr.ndim)
# Get the shape of the Masked Array
print("
Our Masked Array Shape...
",maskArr.shape)
# Get the number of elements of the Masked Array
print("
Elements in the Masked Array...
",maskArr.size)
# To get the imaginary part from the masked array, use the ma.MaskedArray.image attribute in Numpy
print("
Get the imaginary part...
",maskArr.imag)輸出
Array.. [ 68.+4.j 49.+7.j 120.+2.j 64.+0.j] Get the imaginary part [4. 7. 2. 0.] Get the datatype complex128 The number of elements 4 Our Masked Array [(68+4j) (49+7j) -- (64+0j)] Our Masked Array type... complex128 Our Masked Array Dimensions... 1 Our Masked Array Shape... (4,) Elements in the Masked Array... 4 Get the imaginary part... [4.0 7.0 -- 0.0]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP