This tutorial will show you how to save your Numpy datasets with Numpy save.

It explains what the function does, explains the syntax, and shows step-by-step examples of how to use np.save.

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

**Table of Contents:**

## A Quick Introduction to Numpy Save

Numpy save is a function from the Numpy package for Python.

What this function does is simple:

Numpy save *saves* your Numpy data to a file that can be stored on your computer and shared with others.

Specifically, `np.save()`

saves Numpy arrays as a binary file with the ending `.npy`

.

There are, however, a few important details about this that depend on exactly how you use the syntax.

That being the case, let’s look at the syntax of `np.save()`

.

## The syntax of Numpy Save

Now that you’ve learned what the Numpy save function does, let’s take a look at the syntax.

#### A quick note

One quick note about the syntax.

Before we use Numpy functions, we need to import the Numpy package into our working environment.

How exactly we do this can change the syntax.

The common convention is to import Numpy with the alias `np`

. You can do that like this:

import numpy as np

Everything that follows assumes that you’ve imported Numpy like this.

### np.save syntax

The syntax for Numpy save is fairly straight forward.

Assuming that you’ve imported Numpy as described above, you type the function as `np.save()`

.

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

The second parameter is the Numpy array data that you want to save.

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

### The Parameters and Inputs of Numpy Save

Let’s take a closer look at the inputs and optional parameters of the function.

`filename`

`array`

`allow_pickle`

`fix_imports`

`filename`

(required)

The `filename`

argument is the first input to the function.

This argument is strictly a positional argument. What that means in this case is that the np.save function assumes that the *first input* to the function is the “filename”.

This filename can either be:

- the name of a file-object
- a string (i.e. a name for the output file, presented as a string)
- a path

If the filename argument is a string or path, the function will append the file suffix “`.npy`

” if it is not already part of the name.

`array`

(required)

The `array`

input is the array data that you want to save.

This will be a Numpy array or an “array like” object. So technically, you can input a Python list or other array like object here.

`allow_pickle`

By default, this parameter is set to `allow_pickle=True`

. This saves your array data using Python pickles.

Pickling your data may cause some security issues (i.e., when you load pickled data, it may be able to run malicious code).

Pickling your data may cause issues with data portability, since some systems may be unable to load the pickled data (e.g., if they don’t have the required packages installed).

If you want to turn Pickling off, you can set `allow_pickle = False`

.

`fix_imports`

This parameter is set to `fix_imports = True`

. This helps make the pickled output file compatible with and readable on a Python 2 system, since there are sometimes compatibility issues with pickled data between Python 2 and Python 3.

## Examples of How to Use Numpy Save

Now that we’ve looked at the syntax, let’s take a look at some examples of Numpy save.

**Examples:**

#### Run this code first

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

Specifically, you’ll need to import Numpy, and also create a Numpy array that we can work with.

Let’s first import Numpy:

import numpy as np

And now, we’ll create a Numpy array with the Numpy array function.

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

Let’s quickly print it out.

print(my_array)

OUT:

[[1 2 3] [4 5 6]]

As you can see, this is a simple Numpy array with 2 rows and 3 columns. We’ll be able to save this to an .npy file with Numpy save.

### EXAMPLE 1: Save an existing Numpy array to a .npy file

Here, I’ll show you a simple example of how to save a Numpy array to a `.npy`

file.

To do this, we’ll call `np.save()`

. The two arguments to the function will be the name of the output file, and the name of the numpy array that we want to save.

Let’s take a look.

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

So after running this code, we now have a local file in our computer’s file system called `my_array_temp.npy`

.

If you want, you can also use Numpy load to load the data back into your working environment.

# LOAD NUMPY ARRAY np.load('my_array_temp.npy', allow_pickle = True)

OUT:

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

So you’ll notice that when we load the `.npy`

file using `np.load()`

, it loads the data as a Numpy array.

## Frequently asked questions about Numpy Save

Now that you’ve learned about Numpy save and seen some examples, let’s review some frequently asked questions about this technique.

**Frequently asked questions:**

- Why can’t I read the saved .npy file in a text editor?
- How can I save multiple Numpy arrays into a single file?
- How can I save a Numpy array to a text file?

### Question 1: Why can’t I read the saved .npy file in a text editor?

Files saved in `.npy`

format are in binary format.

That means, it will be impossible to directly read or inspect your Numpy data that’s saved in a `.npy`

file in a text editor.

If you want to inspect the data that’s in a `.npy`

file, you’ll need to load the data with Numpy load.

### Question 2: How can I save multiple Numpy arrays into a single file?

You’ll want to use Numpy savez instead of Numpy save.

Numpy savez allows you to save multiple Numpy arrays to a single .npy file.

### Question 3: How can I save a Numpy array to a text file?

If you want to save your Numpy array to a text file instead of a .npy file, you can use Numpy savetxt.

##### Leave your other questions in the comments below

Do you have other questions about Numpy save?

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

Numpy save is useful if you strictly need to save 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: