Login  Register

How do you save a NumPy array as a file?

Posted by pythonsevenmentor on Feb 11, 2025; 2:07am
URL: http://imagej.273.s1.nabble.com/How-do-you-save-a-NumPy-array-as-a-file-tp5025579.html

You can save a NumPy array as a file using different methods depending on the format you need. Here are some commonly used approaches:
1. Save as a .npy File (Binary Format)
The .npy format is NumPy's native binary file format, which is efficient and preserves data types.import numpy as np

# Create a sample NumPy array
arr = np.array([[1, 2, 3], [4, 5, 6]])

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

# Load the array back
loaded_arr = np.load("array.npy")
print(loaded_arr)

Link: Online Python Training in Pune