w3resource

How to save and load a large NumPy array to and from a compressed .npz file?

NumPy: I/O Operations Exercise-11 with Solution

Write a NumPy program that saves a large NumPy array to a compressed .npz file and then reads it back into a NumPy array.

Sample Solution:

Python Code:

import numpy as np

# Create a large NumPy array
data_array = np.random.rand(1000, 1000)

# Define the path to the compressed .npz file
compressed_file_path = 'data_compressed.npz'

# Save the large NumPy array to a compressed .npz file
np.savez_compressed(compressed_file_path, data_array=data_array)

# Load the NumPy array from the compressed .npz file
loaded_data = np.load(compressed_file_path)
loaded_array = loaded_data['data_array']

# Print the original and loaded NumPy arrays
print("Original NumPy Array:")
print(data_array)

print("\nLoaded NumPy Array from Compressed .npz File:")
print(loaded_array)

Output:

Original NumPy Array:
[[0.42411157 0.89311541 0.14227554 ... 0.34292198 0.66140166 0.7117873 ]
 [0.47560704 0.97190361 0.05941864 ... 0.50820546 0.22822676 0.98568349]
 [0.03442543 0.87479321 0.16233207 ... 0.31578898 0.74740725 0.90136316]
 ...
 [0.3721405  0.2293716  0.01759259 ... 0.87295513 0.90359804 0.46607548]
 [0.13659883 0.05339666 0.26766979 ... 0.24272354 0.44563242 0.70060465]
 [0.08349193 0.57621029 0.9926931  ... 0.41319467 0.12608326 0.66274037]]

Loaded NumPy Array from Compressed .npz File:
[[0.42411157 0.89311541 0.14227554 ... 0.34292198 0.66140166 0.7117873 ]
 [0.47560704 0.97190361 0.05941864 ... 0.50820546 0.22822676 0.98568349]
 [0.03442543 0.87479321 0.16233207 ... 0.31578898 0.74740725 0.90136316]
 ...
 [0.3721405  0.2293716  0.01759259 ... 0.87295513 0.90359804 0.46607548]
 [0.13659883 0.05339666 0.26766979 ... 0.24272354 0.44563242 0.70060465]
 [0.08349193 0.57621029 0.9926931  ... 0.41319467 0.12608326 0.66274037]]

Explanation:

  • Import NumPy Library: Import the NumPy library to handle arrays.
  • Create Large NumPy Array: Generate a large NumPy array with random values using np.random.rand().
  • Define Compressed File Path: Specify the path where the compressed .npz file will be saved.
  • Save Array to Compressed .npz File: Use np.savez_compressed() to save the large NumPy array to a compressed .npz file.
  • Load Array from Compressed .npz File: Use np.load() to read the contents of the compressed .npz file back into a NumPy object.
  • Extract Loaded Array: Extract the array from the loaded data using the key used during saving.
  • Finally print the original NumPy array and the loaded array to verify that the data was saved and read correctly.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: How to read a specific dataset from an HDF5 file into a NumPy array?
Next: Write a NumPy array to an Excel file and read it back.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/numpy/save-and-load-a-large-numpy-array-to-and-from-a-compressed-dot-npz-file.php