Return element-wise base masked array raised to power from second array in Numpy

NumpyServer Side ProgrammingProgramming

To return element-wise base array raised to power from second array, use the MaskedArray.power() method in Python Numpy.

The where parameter is a condition broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

The out parameter is a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

Steps

At first, import the required library −

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...\n", 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("\nOur Masked Array...\n", maskArr)

Get the type of the masked array −

print("\nOur Masked Array type...\n", maskArr.dtype)

Get the dimensions of the Masked Array −

print("\nOur Masked Array Dimensions...\n",maskArr.ndim)

Get the shape of the Masked Array −

print("\nOur Masked Array Shape...\n",maskArr.shape)

Get the number of elements of the Masked Array −

print("\nNumber of elements in the Masked Array...\n",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 −

resArr = np.ma.power(maskArr, arrPower)
print("\nResultant Array..\n.", resArr)

Example

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...\n", 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("\nOur Masked Array...\n", maskArr)

# Get the type of the masked array
print("\nOur Masked Array type...\n", maskArr.dtype)

# Get the dimensions of the Masked Array
print("\nOur Masked Array Dimensions...\n",maskArr.ndim)

# Get the shape of the Masked Array
print("\nOur Masked Array Shape...\n",maskArr.shape)

# Get the number of elements of the Masked Array
print("\nNumber of elements in the Masked Array...\n",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("\nResultant Array..\n.", resArr)

Output

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 -- --]
raja
Updated on 05-Feb-2022 11:00:18

Advertisements