

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return element-wise base masked array raised to power from second array in Numpy
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 -- --]
- Related Questions & Answers
- Set the first array elements raised to powers from second array element-wise in Numpy
- Return the base 10 logarithm of the input array element-wise in Numpy
- Return the bases when first array elements are raised to powers from second array in Python
- Return specified diagonals from a masked array in NumPy
- Return each element of the masked array rounded in Numpy
- Check the base of a masked array in NumPy
- Subtract every element from a scalar value and return a new masked Array in NumPy
- Return array of indices of the maximum values from a masked array in NumPy
- Return array of indices of the minimum values from a masked array in NumPy
- Convert masked array element to int Type in NumPy
- Return the truth value of an array equal to another element-wise in Numpy
- Return the element-wise square-root of a complex type array in Numpy
- Return the non-negative square-root of an array element-wise in Numpy
- Return the transpose of the masked array in NumPy
- Return a copy of the masked array in NumPy