What is Numpy.save() in Python?

Filed Under: NumPy
Numpy.Save() in Python

In this tutorial, we will explore numpy.save() in Python under the Numpy module in Python. We will learn exactly what it does and how to use it. Let’s get started!

Numpy.save is a method that lets you save files to .npy format. It only lets you save an array using this method.

What does numpy.save() in Python do?

While saving an array, we can use the numpy.save() in Python to convert the array into a binary file. This array is saved in a .npy file.

.npy files are a good option to store data when you are saving only to reuse in Python.

Being native to the Numpy module, .npy files are more efficient in importing and exporting. So saving to .npy files will save you a lot of time during importing and exporting of data.

In this tutorial, we will learn how to save an array to a .npy file and how to load data back from that file.

Let’s look at saving data to a .npy file first.

How to use the numpy.save() method in Python?

With the context cleared, let’s learn how to use the numpy.save() method to save an array for later use.

Let’s look at the code for saving an array to a .npy file.

import numpy as np 
arr = np.arange(5)
print("data :") 
print(arr)

Output :

data :
[0 1 2 3 4 5]

The above code has created our numpy array. Let’s now save the array in a file.

np.save('jouralDev', arr)
print("Your array has been saved to journalDev.npy")

By running this line of code you will be able to save your array to a .npy file.

Output :

Your array has been saved to journalDev.npy

Here’s the complete code from this section:

import numpy as np 
arr = np.arange(5)
print("data :") 
print(arr)
np.save('jouralDev', arr)
print("Your array has been saved to journalDev.npy")

Let’s learn how to load the data back from a .npy file.

Load .npy Files Saved Using numpy.save()

Numpy offers the method ‘.load()’ that loads the data back from a .npy file.

Let’s use this method to load the data we saved above.

arr = np.load('journalDev.npy')
print("The data is:")
print(arr)

Output :

The data is:
[0 1 2 3 4 5]

Conclusion

This tutorial was about .save() and .load() method under the Numpy module. We learned about .npy files and how to use them for importing and exporting data.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages