NumPy – Using ndarray.dumps() method (4 examples)

Updated: March 1, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a fundamental package for scientific computing with Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. A key feature of NumPy arrays is the ndarray.dumps() method, which allows for the serialization of an array as a string, facilitating the storage or transfer of data. This tutorial will walk you through how to use the ndarray.dumps() method with four illustrative examples, starting from basic usage to more advanced applications.

What does ndarray.dumps() Do?

Before delving into examples, it’s important to understand what exactly ndarray.dumps() does. This method returns a compact, serialized representation of a NumPy array, which can be later used for storage or transmission. What makes dumps() particularly useful is its ability to serialize complex, multidimensional arrays in a manner that is easily reversible.

The method does not take any parameters.

Example 1: Basic Usage of ndarray.dumps()

Let’s start with a straightforward example:

import numpy as np

# Creating a simple array
simple_array = np.array([1, 2, 3, 4, 5])
# Serializing the array
serialized_array = simple_array.dumps()
print(serialized_array)

Output (not readable for human):

b'\x80\x02cnumpy.core.multiarray\n_reconstruct\nq\x00cnumpy\nndarray\nq\x01K\x00\x85q\x02c_codecs\nencode\nq\x03X\x01\x00\x00\x00bq\x04X\x06\x00\x00\x00latin1q\x05\x86q\x06Rq\x07\x87q\x08Rq\t(K\x01K\x05\x85q\ncnumpy\ndtype\nq\x0bX\x02\x00\x00\x00i8q\x0c\x89\x88\x87q\rRq\x0e(K\x03X\x01\x00\x00\x00<q\x0fNNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00tq\x10b\x89h\x03X(\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00q\x11h\x05\x86q\x12Rq\x13tq\x14b.'

In this case, the output will be a byte string representing the serialized form of the array. This string can be stored in a file or transmitted over a network for later deserialization and use.

Example 2: Working with Multidimensional Arrays

Next, let’s look at how ndarray.dumps() manages more complex, multidimensional arrays:

import numpy as np

# Creating a multidimensional array
matrix = np.array([[1, 2], [3, 4]])
# Serializing the matrix
serialized_matrix = matrix.dumps()
print(serialized_matrix)

This example illustrates that regardless of the dimensionality of the array, dumps() efficiently handles serialization, producing a byte string that encapsulates the structure and data of the array.

Output:

b'\x80\x02cnumpy.core.multiarray\n_reconstruct\nq\x00cnumpy\nndarray\nq\x01K\x00\x85q\x02c_codecs\nencode\nq\x03X\x01\x00\x00\x00bq\x04X\x06\x00\x00\x00latin1q\x05\x86q\x06Rq\x07\x87q\x08Rq\t(K\x01K\x02K\x02\x86q\ncnumpy\ndtype\nq\x0bX\x02\x00\x00\x00i8q\x0c\x89\x88\x87q\rRq\x0e(K\x03X\x01\x00\x00\x00<q\x0fNNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00tq\x10b\x89h\x03X \x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00q\x11h\x05\x86q\x12Rq\x13tq\x14b.'

Example 3: Using dumps() with Object Arrays

NumPy arrays can also hold objects. Here’s how you can serialize an array of objects:

import numpy as np

# Creating an array of objects
object_array = np.array([{'name': 'John', 'age': 30}, {'name': 'Jane', 'age': 25}])
# Serializing the array of objects
serialized_object_array = object_array.dumps()
print(serialized_object_array)

This example demonstrates the versatility of ndarray.dumps(), showing that it can serialize not just numerical arrays but also arrays of complex objects.

Example 4: Combining dumps() with Compression

For large arrays, it might be beneficial to combine serialization with data compression. While dumps() itself does not compress data, you can use Python’s built-in compression libraries in conjunction with dumps() to reduce the size of the serialized string:

import numpy as np
import zlib

# Creating a large array
large_array = np.arange(1000)
# Serializing and compressing the array
compressed_serialized_array = zlib.compress(large_array.dumps())
print(compressed_serialized_array)

This approach is particularly useful for reducing storage requirements or transmission time for large datasets.

Conclusion

The ndarray.dumps() method is a powerful tool in NumPy for serializing arrays. This tutorial presented four examples, from basic to advanced, showcasing how to serialize simple, multidimensional, and object arrays, as well as how to combine serialization with compression. With these examples, you should now have a good understanding of how to leverage dumps() for efficient data storage and transmission.