How Do I Save a File in.npy Format in Python?

Have you ever come across a .npy file?

Let us see how to save in .npy format in this article. Numpy’s binary data storage format is NPY.

Numpy is a necessary module for performing data science operations efficiently. Importing, saving, and processing data consumes a significant amount of time in the field of Data Science. CSV files are a good choice for importing and exporting data.

However, there are times when you need to save data in order to use it again in Python. Numpy provides the.npy format in such cases.

Importing and exporting data from and to .npy files are faster and efficient than other methods.

Numpy provides the numpy.save() method for saving files in.npy format. It only allows you to save data in array format. Before saving, it converts the array to a binary file. It is ultimately this binary file that is saved.

numpy.save() method : The numpy.save() function saves the input array to a disc file with the npy extension (.npy).

Using numpy.save() Method To save a File

Approach:

  • Import numpy module as np using the import keyword.
  • Pass some random number to the np.arange() method to get an array from 0 to 7.
  • Store it in a variable.
  • Print the above-obtained array.
  • Save the above-obtained array to ‘Python-programs’ using the np.save() method.
  • Print some random message to check whether the file is saved or not.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module as np using the import keyword
import numpy as np
# Pass some random number to the np.arange() method to get an array from 0 to 7
# Store it in a variable.
got_arry = np.arange(8)
# Print the above obtained array.
print("The above obtained array is:")
print(got_arry)
# Save the above obtained array to 'Python-programs' file using the np.save() method
np.save('Python-programs', got_arry)
print("File saved succesfully in Python-programs.npy")

Output:

The above obtained array is: 
[0 1 2 3 4 5 6 7] 
File saved succesfully in Python-programs.npy

The above code will save your array to a binary file called ‘Python-programs.npy’ as file name.

How to Import .npy File?

To load the data back into Python, we’ll use numpy.load() method.

Approach:

  • Import numpy module as np using the import keyword.
  • Pass the file name to the np.load() method to get back the data from the file (Python-programs.npy).
  • Store it in a variable.
  • Print the data from the file after loading back the File.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module as np using the import keyword
import numpy as np
# Pass the file name to the np.load() method to get back the data from the file
# (Python-programs.npy)
# Store it in a variable.
rslt_data = np.load('Python-programs.npy')
# Print the data from the file after loading back the File
print("The result data after loading back the File:")
print(rslt_data)

Output:

The result data after loading back the File: 
[0 1 2 3 4 5 6 7]