从 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]

更新时间: 22-2-2022

93 次浏览

开启你的职业生涯

完成课程获得认证

立即开始
广告
© . All rights reserved.