Numpy Savez, Explained
This article is originally published at https://www.sharpsightlabs.com
This tutorial will show you how to save multiple Numpy datasets in a single .npz
file with Numpy savez.
It explains what the np.savez does, explains the syntax, and shows step-by-step examples of how to use Numpy savez.
If you need something specific, just click on any of the following links.
Table of Contents:
A Quick Introduction to Numpy Savez
Numpy savez is a tool from Python’s Numpy package.
Remember: Numpy is a package that we use in Python for numeric data manipulation.
Frequently, after we’ve wrangled our data with Numpy, we need to save our Numpy arrays to external files on our computer for storage, or for sharing with other people.
This is where Numpy savez comes in.
Numpy savez allows us to save multiple Numpy arrays as an uncompressed .npz
file.
A quick review of .npz files
Very quickly, let’s review what a .npz
file is.
An .npz
is a file that stores Numpy data of multiple Numpy arrays on a computer disk.
Technically, it is a container for multiple .npy
files, where an .npy
file stores data for a single Numpy array.
So an .npy
file stores data for a single array, and an .npz
file stores data for multiple arrays.
An .npz
file stories not only the numeric values contained in the Numpy arrays being stored, but all of the other relevant information that’s required to reconstruct the array, such as the shape and data type of the array.
Additionally, it’s worth noting here that the Numpy arrays stored in .npz
and .npy
files can be loaded back into your programming environment with the Numpy load function.
The syntax of Numpy Savez
Now that we’ve discussed what Numpy savez does, let’s take a look at the syntax of the function.
A quick note
Very quickly, I want to remind you about something important that’s related to the syntax.
Remember: whenever you use a package in one of your Python programs, you must import it first. And you can always import it with an alias.
Typically, when we use the Numpy package, we import it with the alias np
.
So most data scientists import Numpy like this:
import numpy as np
This is important, because once we import Numpy with this alias, we can use it as a prefix for our Numpy functions, which affects the syntax slightly.
Going forward, I’m going to assume that you’ve imported Numpy with the np
alias.
np.savez syntax
The syntax for Numpy savez is simple.
Assuming that you’ve imported the Numpy package with the np
alias as described above, you can call the function as np.savez()
.
The first argument to the function is the name that you want to give to the output file.
The second argument (and third, etc) are the names of the Numpy arrays that you want to save.
Technically, you also have the option of saving the specified Numpy arrays with a specific “keyword” name as well.
I’ll show you examples of how to use this function, and some of the variations, in the examples section.
Examples of How to Use Numpy Savez
Now that I’ve shown you the syntax, let’s look at some examples of how to use Numpy savez to store Numpy arrays.
Examples:
- Save two Numpy arrays to an
.npz
file - Save two Numpy arrays to an
.npz
file, and give the saved arrays “keyword” names
Run this code first
Before you run the examples, you’ll need to run some code to set things up.
Specifically, you’ll need to import Numpy, and you’ll need to create a few Numpy arrays that you can use in the examples.
Import Numpy
First, we’ll import Numpy.
import numpy as np
Remember: here we’re importing Numpy with the alias np
.
Create Arrays
Next, we’re going to create two numpy arrays.
We’ll create one 2-dimensional array with the Numpy Array function.
We’ll also create an array with 5 evenly spaced values from 0 to 100 using Numpy linspace.
my_array_1 = np.array([[1,2,3],[4,5,6]]) my_array_2 = np.linspace(start = 0, stop = 100, num = 5)
Let’s quickly print out those new arrays.
print(my_array_1) print(my_array_2)
OUT:
[[1 2 3] [4 5 6]] [ 0. 25. 50. 75. 100.]
We’ll be able to save these Numpy arrays into a combined file using Numpy savez.
EXAMPLE 1: Save two Numpy arrays to an .npz file
We’ll start by saving our two Numpy arrays (that we created above) into a single .npz
file.
To do this, we’ll call np.savez()
with 3 arguments:
- the name of the output file
- the name of the first array we want to save
- the name of the second array we want to save
Let’s take a look.
Save data
np.savez('my_arrays_temp.npz', my_array_1, my_array_2)
After running this code, we have a file on our computer disc called my_arrays_temp.npz
.
Reload data
Just so you can see how this works, we’ll load the data back into our environment using the Numpy Load function.
loaded_arrays = np.load('my_arrays_temp.npz', allow_pickle = True)
Here, we have the npz
file loaded to the variable name loaded_arrays
.
We can access the file names with the .files
attribute:
loaded_arrays.files
OUT:
['arr_0', 'arr_1']
These are the names of the two stored arrays in this npz file.
We can access them like accessing the elements of a Pandas list:
loaded_arrays['arr_0'] loaded_arrays['arr_1']
OUT:
array([[1, 2, 3], [4, 5, 6]]) array([ 0., 25., 50., 75., 100.])
So you’ll notice that the original arrays that we saved have been stored in the file my_arrays_temp.npz
. But the names of those arrays have been changed by Numpy savez.
There’s actually a way to save the arrays with a specific name. Let’s take a look at that.
EXAMPLE 2: Save two Numpy arrays to an .npz file, and give the saved arrays “keyword” names
Here, we’re going to save our Numpy arrays to an npz
file, and give the arrays specific names in that file.
Save Arrays
First, we’ll save the arrays.
Here, the first argument to the savez function will be the name of the output file.
But then we’re going to provide a set of name/value pairs.
Essentially, we’ll provide the name that we want to use to save the array, and the array that we want to store. We’ll provide one pair for every array.
np.savez('my_arrays_temp_v2.npz', array_2d = my_array_1, linspace_array_1d = my_array_2)
Notice that we’re storing my_array_1
with the name array_2d
and we’re storing my_array_2
with the name linspace_array_1d
.
Load Arrays
Now, we can load the arrays back to out environment with the Numpy load function.
loaded_arrays_2 = np.load('my_arrays_temp_v2.npz', allow_pickle = True)
Here, we’ve loaded the npz
file to loaded_arrays_2
.
Let’s examine the names of the arrays:
loaded_arrays_2.files
['array_2d', 'linspace_array_1d']
Notice that the names of the arrays that are in the npz
file correspond to the names that we specified above when we used the code np.savez('my_arrays_temp_v2.npz', array_2d = my_array_1, linspace_array_1d = my_array_2)
.
If we want, we can retrieve the contents of the arrays, like so:
loaded_arrays['array_2d'] loaded_arrays['linspace_array_1d']
OUT:
array([[1, 2, 3], [4, 5, 6]]) array([ 0., 25., 50., 75., 100.])
Frequently asked questions about Numpy Savez
Now that you’ve learned about how Numpy savez works and seen some examples, let’s look at some frequently asked questions about this technique.
Frequently asked questions:
- Why can’t I read the saved .npz file in a text editor?
- What function should I use to store a single Numpy array?
Question 1: Why can’t I read the saved .npz file in a text editor?
Arrays that we save in .npz
format are stored in a binary format.
What that means, is that once the arrays are stored in an npz
file, it’s impossible to directly read or inspect those arrays in a text editor.
To read the contents of the arrays, you need to load them back into your Python environment as proper Numpy arrays, using np.load.
Question 2: What function should I use to store a single Numpy array?
Numpy savez is primarily used if you want to store multiple Numpy arrays in one storage file.
However, if you want to only store one Numpy array, there’s a separate function called Numpy save. Numpy save is probably better if you’re only storing a single array.
Leave your other questions in the comments below
Do you have other questions about Numpy savez?
If so, leave your questions in the comments section below.
Join our course to learn more about Numpy
In this tutorial, I’ve shown you how to use Numpy savez.
This technique is useful if you need to store Numpy arrays to a disk file. But if you want to master numeric data manipulation in Python, you’ll need to learn a lot more Numpy.
That said, if you’re serious about learning Numpy, you should consider joining our premium course called Numpy Mastery.
Numpy Mastery will teach you everything you need to know about Numpy, including:
- How to create Numpy arrays
- How to Numpy axes work
- What the “Numpy random seed” function does
- How to use the Numpy random functions
- How to reshape, split, and combine your Numpy arrays
- Applying mathematical operations on Numpy arrays
- and more …
The course will also provide you with our unique practice system. This practice system will enable you to memorize all of the Numpy syntax that you learn.
If you’re struggled to remember Numpy syntax, this is the course you’ve been looking for.
If you practice like we show you, you’ll memorize all of the critical Numpy syntax in only a few weeks.
Find out more here:
Learn More About Numpy Mastery
Thanks for visiting r-craft.org
This article is originally published at https://www.sharpsightlabs.com
Please visit source website for post related comments.