- 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
Return the masked array data as a string containing the raw bytes and set the row major order of the result in Numpy
To return the array data as a string containing the raw bytes in the array, use the ma.MaskedArray.tobytes() method in Numpy.
The fill_value parameter is the value used to fill in the masked values. Default is None, in which case MaskedArray.fill_value is used.
The order parameter is the Order of the data item in the copy. Default is ‘C’.
‘C’ - C order (row major).
‘F’ - Fortran order (column major).
‘A’ - Any, current order of array.
None - Same as ‘A’.
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([[49, 85, 45], [67, 33, 59]]) print("Array...
", arr) print("
Array type...
", arr.dtype)
Get the dimensions of the Array −
print("Array Dimensions...
",arr.ndim)
Create a masked array and mask some of them as invalid −
maskArr = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0]]) 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)
Get the shape of the Masked Array −
print("
Our Masked Array Shape...
",maskArr.shape)
Get the number of elements of the Masked Array −
print("
Elements in the Masked Array...
",maskArr.size)
Return the array data as a string containing the raw bytes in the array, use the ma.MaskedArray.tobytes() method in Numpy. The Order of the data item in the copy is set using the "order" parameter. The 'C' order is for C order i.e. row major −
print("
Result...
",maskArr.tobytes(fill_value = 1111, order = 'C'))
Example
# Python ma.MaskedArray - Return the array data as a string containing the raw bytes in the array and # set the row major order of the resultant data import numpy as np import numpy.ma as ma # Create an array with int elements using the numpy.array() method arr = np.array([[49, 85, 45], [67, 33, 59]]) print("Array...
", arr) print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Create a masked array and mask some of them as invalid maskArr = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0]]) 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) # Get the shape of the Masked Array print("
Our Masked Array Shape...
",maskArr.shape) # Get the number of elements of the Masked Array print("
Elements in the Masked Array...
",maskArr.size) # To return the array data as a string containing the raw bytes in the array, use the ma.MaskedArray.tobytes() method in Numpy # The "fill_value" parameter ios the value used to fill in the masked values. # Default is None, in which case MaskedArray.fill_value is used. # The Order of the data item in the copy is set using the "order" parameter # The 'C' order is for C order i.e. row major print("
Result...
",maskArr.tobytes(fill_value = 1111, order = 'C'))
Output
Array... [[49 85 45] [67 33 59]] Array type... int64 Array Dimensions... 2 Our Masked Array [[49 85 --] [67 -- 59]] Our Masked Array type... int64 Our Masked Array Dimensions... 2 Our Masked Array Shape... (2, 3) Elements in the Masked Array... 6 Result... b'1\x00\x00\x00\x00\x00\x00\x00U\x00\x00\x00\x00\x00\x00\x00W\x04\x00\x00\x00\x00\x00\x00C\x00\x00\x00\x00\x00\x00\x00W\x04\x00\x00\x00\x00\x00\x00;\x00\x00\x00\x00\x00\x00\x00'
- Related Articles
- Return the masked array data as a string containing the raw bytes and set the column major order of the result in Numpy
- Return the array data as a string containing the raw bytes in the array in Numpy
- Return the masked array data as a string containing the raw bytes in the array and fill the invalid entries in Numpy
- Return a masked array containing the same data but with a new shape viewed as row-major order in Numpy
- Return a masked array containing the same data but with a new shape viewed as column-major order in Numpy
- Return a copy of the masked array collapsed into one dimension in row-major order in Numpy
- Swap the bytes of the masked array data in Numpy
- Return the pickle of the masked array as a string in NumPy
- Swap the bytes of the masked array data inplace in Numpy
- Return the underlying data as a view of the masked array in Numpy
- Return a new Three-Dimensional array without initializing entries and store the data in row-major order in Numpy
- Return a copy of the masked array collapsed into one dimension in column-major order in Numpy
- Return a masked array containing the same data but with a new shape in Numpy
- Return all the non-masked data as a 1-D array in Numpy
- Return the addresses of the data and mask areas of a masked array in Numpy
