- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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("
Array type...", arr.dtype)
Get the dimensions of the Array −
print("
Array Dimensions...",arr.ndim)
Create a masked array −
maskArr = ma.masked_array(arr, mask =[False]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)
Get the dimensions of the Masked Array −
print("
Our Masked Array Dimensions...
",maskArr.ndim)
Convert masked array to int type, use the ma.MaskedArray.__int__() method in Numpy −
print("
Result 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("
Array type...", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...",arr.ndim) # Create a masked array maskArr = ma.masked_array(arr, mask =[False]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype) # Get the dimensions of the Masked Array print("
Our Masked Array Dimensions...
",maskArr.ndim) # To convert masked array to int type, use the ma.MaskedArray.__int__() method in Numpy print("
Result 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
- Related Articles
- Convert Masked Array elements to Float Type in Numpy
- Convert the input to a masked array of the given data-type in Numpy
- Convert the input to a masked array conserving subclasses in Numpy
- Return each element of the masked array rounded in Numpy
- Return element-wise base masked array raised to power from second array in Numpy
- Return the copy of a masked array cast to a specified type in Numpy
- How to convert yyyymmdd in INT type to date?
- Check which element in a masked array is equal to a given value in NumPy
- Add every element of a masked Array with a scalar value in NumPy
- Subtract a scalar value from every element of a masked Array in NumPy
- Divide a scalar value into every element of a masked Array in NumPy
- Add a scalar value to each element and return a new masked array in NumPy
- Golang Program to convert double type variables to int
- Golang Program to convert int type variables to String
- Swift Program to Convert Char type variables to Int

Advertisements