NumPy | save method
Start your free 7-days trial now!
Numpy's save(~)
method writes a Numpy array to a file in .npy
format.
Parameters
1. file
| file
or string
or pathlib.Path
The file to which the Numpy array will be written. The .npy
extension will be appended to the filename if the path does not already include it.
2. arr
| array-like
The array to save.
3. allow_pickle
| boolean
| optional
Whether or not to use pickling to save the array. If your array has a dtype of object, (e.g. your array contains non-numeric data like strings), then this must be set to True
. If your array has numeric dtype
, then this can be set to False
.
By default, allow_pickle=True
.
If the dtype is numeric, then opt for allow_pickle=False
.
As a general rule of thumb, pickles should not be used if they are not required since different versions of Python and Numpy may interprets pickles differently, and so others may not be able to load the files. Moreover, since reading pickled files involve running arbitrary code in the file, the reader will be susceptible to malicious attacks.
4. fix_imports
| boolean
| optional
Whether or not to make the saved file readable by Python 2. By default, fix_imports=True
.
Return value
None.
Examples
x = np.array([3,4,5])np.save("my_data", x)
This saves our array in a file called my_data.npy
, which exists in the same directory as our Python script.