📜  Python numpy.save()(1)

📅  最后修改于: 2023-12-03 14:46:01.285000             🧑  作者: Mango

Python numpy.save()

The numpy.save() function is used to save a single array to a binary file in NumPy's .npy format. This format is designed to store an array in a way that can be easily accessed and used by other NumPy functions and routines.

Syntax

The syntax for using numpy.save() is as follows:

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

Where:

  • file: The file object or file name to which the data is saved.

  • arr: The numpy array to be saved.

  • allow_pickle: Optional. A boolean value indicating whether to allow pickling of the array. Default is True.

  • fix_imports: Optional. A boolean value indicating whether to fix the numpy version in the file. Default is True.

Example

Here's an example of how to use numpy.save() to save a numpy array to a .npy file:

import numpy as np

# Generate a numpy array
arr = np.arange(10)

# Save the array to a file
np.save('my_array.npy', arr)

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

To load the array from the file, we can use the numpy.load() function, like this:

import numpy as np

# Load the array from file
arr = np.load('my_array.npy')

# Print the loaded array
print(arr)

This will load the my_array.npy file and print the contents of the numpy array that was saved in the file.

Overall, numpy.save() is a useful function for saving large datasets or arrays to disk in a compressed and efficient format.