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
Dump a pickle of the masked array in NumPy
To pickle the masked array, use the ma.MaskedArray.dumps() method in Python Numpy. A masked array is the combination of a standard numpy.ndarray and a mask. 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.
NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.
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([[55, 85, 68, 84], [67, 33, 39, 53], [29, 88, 51, 37], [56, 45, 99, 85]])
print("Array...
", arr)
print("\nArray 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 =[[1, 1, 0, 0], [ 0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 0]])
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)
Get the shape of the Masked Array −
print("\nOur Masked Array Shape...
",maskArr.shape)
Get the number of elements of the Masked Array −
print("\nElements in the Masked Array...
",maskArr.size)
To pickle the masked array, use the ma.MaskedArray.dumps() method −
pickArr = maskArr.dumps()
Display the pickle −
print("\nPickle of the masked array...
",pickArr)
Example
import numpy as np
import numpy.ma as ma
# Create an array with int elements using the numpy.array() method
arr = np.array([[55, 85, 68, 84], [67, 33, 39, 53], [29, 88, 51, 37], [56, 45, 99, 85]])
print("Array...
", arr)
print("\nArray type...
", arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...
",arr.ndim)
# Create a masked array and mask some of them as invalid
maskArr = ma.masked_array(arr, mask =[[1, 1, 0, 0], [ 0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 0]])
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)
# Get the shape of the Masked Array
print("\nOur Masked Array Shape...
",maskArr.shape)
# Get the number of elements of the Masked Array
print("\nElements in the Masked Array...
",maskArr.size)
# To pickle the masked array, use the ma.MaskedArray.dumps() method
pickArr = maskArr.dumps()
# Display the pickle
print("\nPickle of the masked array...
",pickArr)
Output
Array... [[55 85 68 84] [67 33 39 53] [29 88 51 37] [56 45 99 85]] Array type... int64 Array Dimensions... 2 Our Masked Array [[-- -- 68 84] [67 33 -- 53] [29 88 51 --] [56 -- 99 85]] Our Masked Array type... int64 Our Masked Array Dimensions... 2 Our Masked Array Shape... (4, 4) Elements in the Masked Array... 16 Pickle of the masked array... b"\x80\x02cnumpy.ma.core
_mareconstruct\nq\x00(cnumpy.ma.core\nMaskedArray\nq\x01cnumpy\nndarray\nq\x02K\x00\x85q\x03X\x01\x00\x00\x00bq\x04tq\x05Rq\x06(K\x01K\x04K\x04\x86q\x07cnumpy\ndtype\nq\x08X\x02\x00\x00\x00i8q\t\x89\x88\x87q\nRq\x0b(K\x03X\x01\x00\x00\x00<q\x0cNNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00tq\rb\x89c_codecs\nencode\nq\x0eX\x80\x00\x00\x007\x00\x00\x00\x00\x00\x00\x00U\x00\x00\x00\x00\x00\x00\x00D\x00\x00\x00\x00\x00\x00\x00T\x00\x00\x00\x00\x00\x00\x00C\x00\x00\x00\x00\x00\x00\x00!\x00\x00\x00\x00\x00\x00\x00'\x00\x00\x00\x00\x00\x00\x005\x00\x00\x00\x00\x00\x00\x00\x1d\x00\x00\x00\x00\x00\x00\x00X\x00\x00\x00\x00\x00\x00\x003\x00\x00\x00\x00\x00\x00\x00%\x00\x00\x00\x00\x00\x00\x008\x00\x00\x00\x00\x00\x00\x00-\x00\x00\x00\x00\x00\x00\x00c\x00\x00\x00\x00\x00\x00\x00U\x00\x00\x00\x00\x00\x00\x00q\x0fX\x06\x00\x00\x00latin1q\x10\x86q\x11Rq\x12h\x0eX\x10\x00\x00\x00\x01\x01\x00\x00\x00\x00\x01\x00\x00\x00\x00\x01\x00\x01\x00\x00q\x13h\x10\x86q\x14Rq\x15Ntq\x16b."
