Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Convert masked array element to int Type in NumPy
To convert masked array to int type, use the ma.MaskedArray.__int__() method in Numpy. A mask is either nomask, indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.
Steps
At first, import the required library −
import numpy as np import numpy.ma as ma
Create an array with using the numpy.array() method −
arr = np.array([14.76])
print("Array...", arr)
print("\nArray type...", arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...",arr.ndim)
Create a masked array −
maskArr = ma.masked_array(arr, mask =[False])
print("\nOur Masked Array
", maskArr)
print("\nOur Masked Array type...
", maskArr.dtype)
Get the dimensions of the Masked Array −
print("\nOur Masked Array Dimensions...
",maskArr.ndim)
Convert masked array to int type, use the ma.MaskedArray.__int__() method in Numpy −
print("\nResult Converted to int type...
",maskArr.__int__())
Example
import numpy as np
import numpy.ma as ma
# Create an array with using the numpy.array() method
arr = np.array([14.76])
print("Array...", arr)
print("\nArray type...", arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...",arr.ndim)
# Create a masked array
maskArr = ma.masked_array(arr, mask =[False])
print("\nOur Masked Array
", maskArr)
print("\nOur Masked Array type...
", maskArr.dtype)
# Get the dimensions of the Masked Array
print("\nOur Masked Array Dimensions...
",maskArr.ndim)
# To convert masked array to int type, use the ma.MaskedArray.__int__() method in Numpy
print("\nResult Converted to int type...
",maskArr.__int__())
Output
Array... [14.76] Array type... float64 Array Dimensions... 1 Our Masked Array [14.76] Our Masked Array type... float64 Our Masked Array Dimensions... 1 Result Converted to int type... 14
Advertisements
