
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Element-wise Base Masked Array Raised to Power 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...
", 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 −
resArr = np.ma.power(maskArr, arrPower) print("
Resultant Array..
.", 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...
", 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)
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 -- --]