Skip to content

Commit e734ba6

Browse files
Update saving_numpy_arrays_to_files.md
Added savez method
1 parent 0a2ca67 commit e734ba6

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

contrib/numpy/saving_numpy_arrays_to_files.md

+54
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,57 @@ print(arr1)
3838
```python
3939
[1,2,3,4,5]
4040
```
41+
### 2. numpy.savez():
42+
43+
The `np.savez` function saves multiple NumPy arrays into a single file with a `.npz` extension. Each array is stored with a unique name.
44+
45+
#### Syntax :
46+
47+
```python
48+
numpy.savez(file, *args, **kwds)
49+
```
50+
- **file** : Name of the file.
51+
- **args** : Arrays to be saved.( If arrays are unnamed, they are stored with default names like arr_0, arr_1, etc.)
52+
- **kwds** : Named arrays to be saved.
53+
54+
#### Example :
55+
56+
```python
57+
import numpy as np
58+
59+
arr1 = np.array([1,2,3,4,5])
60+
arr2 = np.array(['a','b','c','d'])
61+
arr3 = np.array([1.2,3.4,5])
62+
np.savez('example.npz', a1=arr1, a2=arr2, a3 = arr3) #saves arrays in npz format
63+
64+
```
65+
66+
Inorder to load the array from example.npz
67+
68+
```python
69+
70+
arr = np.load('example.npz')
71+
print(arr['a1'])
72+
print(arr['a2'])
73+
print(arr['a3'])
74+
75+
```
76+
**Output** :
77+
```python
78+
[1 2 3 4 5]
79+
['a' 'b' 'c' 'd']
80+
[1.2 3.4 5. ]
81+
```
82+
83+
### 3. np.savetxt()
84+
85+
The `np.savetxt` function saves a NumPy array to a text file, such as `.txt` or `.csv`. This format is human-readable and can be used for interoperability with other tools.
86+
87+
#### Syntax :
88+
89+
```python
90+
numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ', encoding=None)
91+
```
92+
- **fname** : Name of the file.
93+
- **X** : Array to be saved.
94+
- **kwds** : Named arrays to be saved.

0 commit comments

Comments
 (0)