Numpy savez : How to implement in python with stepwise example?

Numpy savez implement in python

Numpy savez saves multiple numpy arrays into single file. It saves them into a special file format “.npz ”  format without any compression. In this article, We will create a .npz file by combining two NumPy array. We will also see how can we load them into the memory.


Numpy savez Implementation –

In this section, We will demonstrate NumPy savez () function in three steps.

Step 1: Import packages

As savez () is numpy function. We need to import it as first step.We will also import tempfile python module because we will use it storing files. Here is the code for that.

from tempfile import TemporaryFile
import numpy as np

Step 2: Array Creation –

In this step, We will create two arrays which we will use in saving with “.npz” format.

tmp_file = TemporaryFile()
arr_1 = np.arange(10)
arr_2 = np.arange(10)

Step 3: invoking savez() function-

Its the final step. Where we will call the savez function. It will store the .npz file. Here we will parameterize arr_1 and arr_2 as the input array.

np.savez(tmp_file, arr_1, arr_2)

Complete code for savez() –

Here is the full code which we break into the steps for explaining you.

from tempfile import TemporaryFile
import numpy as np
tmp_file = TemporaryFile()
arr_1 = np.arange(10)
arr_2 = np.arange(10)
np.savez(tmp_file, arr_1, arr_2)

How to load .npz file?

It is just a reverse process from above section. Here we will see how can we load the numpy “.npz”.

_ =
npzfile = np.load(tmp_file)
numpy savez() loading
numpy savez() loading

Saving arrays with paramerterize name-

In the above section, Where we store the arrays in “.npz” . We have used default file name in “.npz” files as arr_0 and arr_1. It will be in the same fashion with multiple files. Anyways lets set thier name in the combine file.

np.savez(tmp_file, file_1=arr_1, file_2=arr_2)

Here we have provided the name as file_1 and file_2 as stored array file names.


Saving numpy arrays is one the best way to store statistcal models.  We have seen FastTet etc Language models which saves the numpy array in “.npz” format.  Well, I hope now you are quite confident about saving the numpy arrays with savez() function. Please provide your view on comment box.



Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages, where he and his team share knowledge and help others learn more about data science.
Share via
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner