Numpy Load, Explained

In this tutorial, I’ll show you how to use Numpy load to load Numpy arrays from stored npy or npz files.

So I’ll explain what Numpy load does, I’ll explain the syntax of np.load, and I’ll show you step-by-step examples of how to use it.

If you need something specific, you can click on any of the following links:

Table of Contents:

A Quick Introduction to Numpy Load

The Numpy load function is pretty straight forward: it “loads” Numpy arrays from Numpy storage files (i.e., .npy files and .npz files).

That being the case, let’s quickly review stored Numpy files.

A quick review of Numpy and stored Numpy files

As you probably know, Numpy is a data science package for Python that is used for numerical data manipulation and analysis.

When we use Numpy, we use it inside our Python working environment.

But sometimes, we need to store our data in an external file that we can put on a computer disk for long term storage. These long-term files that store Numpy array data are .npy and .npz files. We can create .npy and .npz files with Numpy save and Numpy savez.

Later though, if we want to work with those stored arrays again, we need to re-load those array files from those .npy or .npz files back into our working environment.

How do we do this?

We do it with Numpy load.

A visual example that shows how Numpy load "loads" a Numpy array from an npy or npz file.

So essentially, we put Numpy array data in long-term storage with Numpy save, and we can load it back into our working environment later with Numpy load.

Numpy load is fairly easy to use, but a few details depend on how exactly you use the syntax.

Let’s take a look at it.

The syntax of Numpy Load

Here, we’ll look at the exact syntax of Numpy load including some of the details like optional parameters.

A quick note

One note about the syntax.

Whenever we use a Python package such as Numpy, we need to load it into our environment before we can use it.

Exactly how we load a package actually impacts the syntax.

The reason for this is because we often import a package with an alias.

In the case of Numpy, we typically import it with the alias np, like this:

import numpy as np

Again, this is important, because when we import Numpy like this, it allows us to call Numpy functions with the prefix np.

Everything else going forward in this tutorial assumes that you’ve imported Numpy with the import statement shown above.

np.load syntax

In the simplest case, the syntax for Numpy load is simple.

Assuming that you’ve imported Numpy with the alias np, you type the function as np.load().

Inside the parenthesis, you provide the name of the .npy or .npz file that you want to load.

An image that explains the syntax of np.load.

The first argument to the function is the name of the file from which you want to load your data.

Then there are a few optional parameters that you can use to modify the behavior of the function.

The Parameters and Inputs of Numpy Load

Let’s look quickly at the parameters of Numpy load:

  • filename
  • mmap_mode
  • allow_pickle
  • fix_imports
  • encoding
filename (required)

The filename argument is the first input to the function, and it is required.

Keep in mind that this argument is a positional argument. Python assumes that the first input to Numpy load is the name of the file you want to load.

This filename will typically be a .npy file or an .npz file.

mmap_mode

The mmap_mode parameter controls whether or not the function uses memory mapping.

By default, this is set to mmap_mode = None.

Other possible arguments to this parameter are:

  • ‘r+’
  • ‘r’
  • ‘w+’
  • ‘c’

If you use any of these arguments, Numpy load will memory map the file using the mode that’s specified by the argument that you choose.

Note that according to the official documentation, “memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory.”

allow_pickle

This parameter controls whether the function will be able to load “pickled” arrays stored in an npy or npz file.

By default, this parameter is set to allow_pickle = False.

Loading pickled data can cause security issues, which is one reason why this parameter is set to False by default.

fix_imports

The fix_imports parameter is helpful if you attempt to load pickled data that was stored on a Python 2 system into a Python 3 system.

By default, this parameter is set to fix_imports = True.

encoding

This parameter controls what encoding np.load uses when it reads Python 2 strings.

By default, this is set to encoding='ASCII'.

Numpy Load Output

Numpy load returns the Numpy arrays that are stored in the file.

As you’ll see in the examples, this can be either a single numpy array, in the case of an .npy file.

Or it can be multiple arrays, in the case of an .npz file. In this case, you will be able to access the arrays by name, using the stored name of the array as a “key” (much like a dictionary).

You’ll see an example of this in example 2.

Examples of How to Use Numpy Load

Now that you’ve seen the syntax, let’s look at some simple examples of how to use Numpy load.

Examples:

Run this code to import Numpy

Before you run the examples, you’ll need to run some preliminary setup code.

Specifically, you need to import Numpy.

You can do that with this code:

import numpy as np

Remember: when we import Numpy with the alias np, we can use np as a prefix when we call the function. I explained this in the syntax section above.

EXAMPLE 1: Load a single Numpy array from an .npy file

In this example, we’re going to load a single numpy array from an .npy file.

This will require a few steps. We will:

  • create a Numpy array
  • save the Numpy array to an .npy file
  • load the array using Numpy load

Let’s do each of those, one at a time.

Create Array

First, we’l create a Numpy simple array that we can store, and then load.

To do this, we’ll use the Numpy array function to manually create an array with a specific set of values.

We’ll call np.array, and store the array with the name, my_array.

# CREATE NUMPY ARRAY
my_array = np.array([[0,2,4],[1,3,5]])

Now, let’s print it out:

print(my_array)

OUT:

[[0 2 4]
 [1 3 5]]

This is a simple array with 2 rows and 3 columns.

We’ll be able to save this with Numpy save, and then load it with Numpy load.

Create npy file

Now, we’ll save our Numpy array in order to create an .npy file.

np.save('my_array_temp.npy', my_array)

Here, we’ve called np.save and created an .npy file called my_array_temp.npy.

Load npy file

Now, we’re going to load our Numpy array by calling np.load on the file we just created, my_array_temp.npy.

np.load('my_array_temp.npy')

OUT:

array([[0, 2, 4],
       [1, 3, 5]])

As you can see, when you call np.load, it loads the array stored in the .npy file to your Python environment. Remember: this is the array that we created previously and stored with Numpy save.

Importantly, the way that I’ve called np.load here, the array has simply been sent to the console.

If you want to keep that array with a variable name, you need to pass the output to a variable, like this:

my_reloaded_array = np.load('my_array_temp.npy')

EXAMPLE 2: Load a multiple Numpy arrays from an .npz file, with specific array names

In this example, we’re going to load two Numpy arrays. This time, we’re going to load our arrays from an .npz file, where we’ve stored the arrays with specific names.

Again: this will require multiple steps. We will:

  • create two Numpy arrays
  • save the arrays to an .npz file, with specific names
  • load the arrays using Numpy load

Let’s do it.

Create Arrays

For the first step of this example, we’re going to create two Numpy arrays.

Here, we’ll create a 1-dimensional of integers using Numpy arange.

We’ll also create an array with the logs of those numbers using Numpy log.

integer_array = np.arange(start = 1, stop = 6)
log_array = np.log(integer_array)

And let’s print those arrays out:

print(integer_array)
print(log_array)

OUT:

[1 2 3 4 5]

[0.   0.69314718   1.09861229   1.38629436   1.60943791]

We’re going to save these arrays with specific names, using Numpy savez.

Create npz file

Here, we’re going to use Numpy savez to save both of these Numpy arrays to a single .npz file.

Moreover, we’re going to save those files with specific names inside the .npz file.

np.savez('my_arrays_temp.npz', int_arr = integer_array, log_arr = log_array)

To learn more about how this works, you can read our tutorial about Numpy savez.

Load the Arrays

And now that we have the arrays stored to an npz file, we’ll load the arrays with Numpy load.

loaded_arrays = np.load('my_arrays_temp.npz')

Here, we’ve loaded the npz file to the variable called loaded_arrays.

When we load an npz file like this, we can inspect the contents using the files attribute:

loaded_arrays.files

OUT:

['int_arr', 'log_arr']

As you can see, there are two arrays in this loaded npz file. These are the Numpy arrays that we stored in the previous section using Numpy save.

To retrieve the contents of the arrays, we can treat loaded_arrays like a container, and use the file names we just printed out as the keys. This will retrieve the stored numpy arrays:

loaded_arrays['array_2d']
loaded_arrays['linspace_array_1d']

OUT:

array([1, 2, 3, 4, 5])

array([0.,  0.69314718,  1.09861229,  1.38629436,  1.60943791])

Here, we’ve used the file names associated with loaded_arrays, and loaded them by referencing the file names like container keys. Provide the file name and it retrieves the stored Numpy array.

Frequently asked questions about Numpy Load

Now that you’ve learned about Numpy load and seen some examples, let’s review some frequently asked questions about the function.

Frequently asked questions:

Question 1: How do you load Numpy data from a text file?

Numpy load works with .npy or .npz files, but if you want to load numeric data that’s stored in a text file, you need to use Numpy loadtxt.

To learn more, you can read our tutorial about Numpy loadtxt.

Leave your other questions in the comments below

Do you have other questions about Numpy load?

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 load.

Numpy load is useful if you strictly need to load a Numpy array, but if you really 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

Joshua Ebner

Joshua Ebner is the founder, CEO, and Chief Data Scientist of Sharp Sight. Prior to founding the company, Josh worked as a Data Scientist at Apple. He has a degree in Physics from Cornell University.

2 thoughts on “Numpy Load, Explained”

  1. Hello, thank you for your very interesting tutos, numpy save/load is a tool I did not think to use.
    Unfortunately there are some misses in the tuto. I tried to follow the link to numpy savez and I get this error: “Oops! That page can’t be found.” There is the same problem when trying to follow the link to Numpy loadtxt. I looked at your whole blog but I did not find them.
    Can you give acces to these pages again ?
    Thank you.
    Serge UGE

    Reply

Leave a Comment