numpy
  1. numpy-numpysave

Numpy.save() - ( Common NumPy Functions )

Heading h2

Syntax

numpy.save(file, arr, allow_pickle=True, fix_imports=True)

Example

import numpy as np

arr = np.arange(10)

np.save('my_array', arr)

Output

This will save the NumPy array to a file named my_array.npy.

Explanation

The numpy.save() function is used to save a NumPy array to a binary file in NumPy *.npy format. The first argument is the file name and the second argument is the NumPy array that needs to be saved. The allow_pickle parameter is used to allow the array to be saved as a pickle file, and the fix_imports parameter is used to fix any issues with importing Python 2 modules in Python 3.

Use

You can use the numpy.save() function to save and load NumPy arrays to and from disk. This can be useful for caching data that takes a long time to compute. To load the NumPy array from the saved file, use the numpy.load() function.

Important Points

  • The numpy.save() function is used to save a NumPy array to a binary file in NumPy *.npy format
  • The first argument is the file name and the second argument is the NumPy array that needs to be saved
  • The allow_pickle parameter is used to allow the array to be saved as a pickle file, and the fix_imports parameter is used to fix any issues with importing Python 2 modules in Python 3

Summary

In conclusion, the numpy.save() function is a useful NumPy function used to save a NumPy array to a binary file in NumPy *.npy format. This function can be useful for caching data that takes a long time to compute and for later reuse. When read back in using the numpy.load() function, the array will be returned in its original form and can be used as needed.

Published on: