Saving and Loading Arrays to/from Files
NumPy
offers a variety of functions for saving and loading numPy
arrays to and from files, such as:
save()
: Saves an array to a binary file inNumPy
's.npy
format;savez()
: Saves multiple arrays to a single compressed.npz
file;savetxt()
: Saves an array to a text file;load()
: Loads an array from a binary file inNumPy
's.npy
format;loadtxt()
: Loads an array from a text file.
Compito
Swipe to start coding
- Save the newly created array in
NumPy
's.npy
format. - Save the array to a text file.
- Load back in the array.
Soluzione
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import numpy as np
# Create a NumPy array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Save the array to a binary file in NumPy's .npy format
np.save('array.npy', arr)
# Save the array to a text file
np.savetxt('array.txt', arr)
# Load the array
loaded_arr_npy = np.load('array.npy')
loaded_arr_txt = np.loadtxt('array.txt')
display(arr, loaded_arr_npy, loaded_arr_txt)
Congratulations!
Congratulations on completing this NumPy
tutorial! You have gained substantial knowledge about handling arrays and matrices in Python, laying down a strong foundation for utilizing NumPy
in your data processing and analysis endeavors.
Mark tasks as Completed
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 1. Capitolo 7
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione