在NumPy中返回元素级基掩码数组,该数组由第二个数组的幂构成


要返回元素级基数组,其幂来自第二个数组,请在Python NumPy中使用**MaskedArray.power()**方法。

where参数是在输入上广播的条件。在条件为True的位置,输出数组将设置为ufunc结果。在其他地方,输出数组将保留其原始值。请注意,如果通过默认的out=None创建未初始化的输出数组,则其中条件为False的位置将保持未初始化状态。

out参数是存储结果的位置。如果提供,它必须具有输入广播到的形状。如果未提供或为None,则返回一个新分配的数组。元组(仅可能作为关键字参数)的长度必须等于输出的数量。

步骤

首先,导入所需的库:

import numpy as np
import numpy.ma as ma

使用numpy.array()方法创建一个包含整数元素的数组:

arr = np.array([93, 33, 76, 73, 88, 51, 62, 45, 67])
print("Array...
", arr)

创建一个掩码数组并将其中一些标记为无效:

maskArr = ma.masked_array(arr, mask =[ 0, 0, 0, 0, 1, 0, 0, 1, 1])
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("
Number of elements in the Masked Array...
",maskArr.size)

设置幂数组:

arrPower = [2, 3, 4, 2, 4, 3, 5, 3, 2]

要返回元素级基数组,其幂来自第二个数组,请使用MaskedArray.power()方法:

resArr = np.ma.power(maskArr, arrPower)
print("
Resultant Array..
.", resArr)

示例

import numpy as np
import numpy.ma as ma

# Create an array with int elements using the numpy.array() method
arr = np.array([93, 33, 76, 73, 88, 51, 62, 45, 67])
print("Array...
", arr) # Create a masked array and mask some of them as invalid maskArr = ma.masked_array(arr, mask =[ 0, 0, 0, 0, 1, 0, 0, 1, 1]) print("
Our Masked Array...
", maskArr) # Get the type of the masked array 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("
Number of elements in the Masked Array...
",maskArr.size) # Set the power array arrPower = [2, 3, 4, 2, 4, 3, 5, 3, 2] # To return element-wise base array raised to power from second array, use the MaskedArray.power() method in Python Numpy resArr = np.ma.power(maskArr, arrPower) print("
Resultant Array..
.", resArr)

输出

Array...
[93 33 76 73 88 51 62 45 67]

Our Masked Array...
[93 33 76 73 -- 51 62 -- --]

Our Masked Array type...
int64

Our Masked Array Dimensions...
1

Our Masked Array Shape...
(9,)

Number of elements in the Masked Array...
9

Resultant Array..
. [8649 35937 33362176 5329 -- 132651 916132832 -- --]

更新于:2022年2月5日

170 次查看

启动您的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.