

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Return the pickle of the masked array as a string in NumPy
To pickle the masked array, use the ma.MaskedArray.dumps() method. Load the pickle back to array using the pickle.loads() method in 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...\n", arr) print("\nArray type...\n", arr.dtype)
Get the dimensions of the Array −
print("Array Dimensions...\n",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\n", maskArr) print("\nOur Masked Array type...\n", maskArr.dtype)
Get the dimensions of the Masked Array −
print("\nOur Masked Array Dimensions...\n",maskArr.ndim)
Get the shape of the Masked Array −
print("\nOur Masked Array Shape...\n",maskArr.shape)
Get the number of elements of the Masked Array −
print("\nElements in the Masked Array...\n",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...\n",pickArr)
Load the pickle back to array using the pickle.loads() method −
resArr = pickle.loads(pickArr)
Display the resultant array −
print("\nLoading the pickle back to the masked array...\n",resArr)
Example
import numpy as np import numpy.ma as ma import pickle # 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...\n", arr) print("\nArray type...\n", arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n",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\n", maskArr) print("\nOur Masked Array type...\n", maskArr.dtype) # Get the dimensions of the Masked Array print("\nOur Masked Array Dimensions...\n",maskArr.ndim) # Get the shape of the Masked Array print("\nOur Masked Array Shape...\n",maskArr.shape) # Get the number of elements of the Masked Array print("\nElements in the Masked Array...\n",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...\n",pickArr) # Load the pickle back to array using the pickle.loads() method resArr = pickle.loads(pickArr) # Display the resultant array print("\nLoading the pickle back to the masked array...\n",resArr)
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\n_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." Loading the pickle back to the masked array... [[-- -- 68 84] [67 33 -- 53] [29 88 51 --] [56 -- 99 85]]
- Related Questions & Answers
- Dump a pickle of the masked array in NumPy
- Return the underlying data as a view of the masked array in Numpy
- Return a copy of the masked array in NumPy
- Return the mask of a masked array in Numpy
- Return the user-readable string representation of a masked array in Numpy
- Return the transpose of the masked array in NumPy
- Return the length of the masked array in Numpy
- Return the absolute value of a masked Array in NumPy
- Return all the non-masked data as a 1-D array in Numpy
- Return the average of the masked array elements in Numpy
- Return the variance of the masked array elements 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 the data of a masked array as an ndarray
- Return the standard deviation of the masked array elements in NumPy
- Return the sum along diagonals of the masked array in Numpy