Skip to content

Commit 0a2ca67

Browse files
Create saving_numpy_arrays_to_files.md
Added introduction Added save method
1 parent 3319bc1 commit 0a2ca67

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Saving NumPy Arrays to Files
2+
3+
- Saving arrays in NumPy is important due to its efficiency in storage and speed, maintaining data integrity and precision, and offering convenience and interoperability.
4+
- NumPy provides several methods to save arrays efficiently, either in binary or text formats.
5+
- The primary methods are `save`, `savez`, and `savetxt`.
6+
7+
### 1. numpy.save():
8+
9+
The `np.save` function saves a single NumPy array to a binary file with a `.npy` extension. This format is efficient and preserves the array's data type and shape.
10+
11+
#### Syntax :
12+
13+
```python
14+
numpy.save(file, arr, allow_pickle=True, fix_imports=True)
15+
```
16+
- **file** : Name of the file.
17+
- **arr** : Array to be saved.
18+
- **allow_pickle** : This is an Optional parameter, Allows saving object arrays using Python pickles.(By Default True)
19+
- **fix_imports** : This is an Optional parameter, Fixes issues for Python 2 to Python 3 compatibility.(By Default True)
20+
21+
#### Example :
22+
23+
```python
24+
import numpy as np
25+
26+
arr = np.array([1,2,3,4,5])
27+
np.save("example.npy",arr) #saves arr into example.npy file in binary format
28+
```
29+
30+
Inorder to load the array from example.npy
31+
32+
```python
33+
arr1 = np.load("example.npy")
34+
print(arr1)
35+
```
36+
**Output** :
37+
38+
```python
39+
[1,2,3,4,5]
40+
```

0 commit comments

Comments
 (0)