|
| 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 | +``` |
| 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, delimiter=' ', newline='\n', header='', footer='', encoding=None) |
| 91 | + ``` |
| 92 | +- **fname** : Name of the file. |
| 93 | +- **X** : Array to be saved. |
| 94 | +- **delimiter** : It is a Optional parameter,This is a character or string that is used to separate columns.(By Default it is " ") |
| 95 | +- **newline** : It is a Optional parameter, Character for seperating lines.(By Default it is "\n") |
| 96 | +- **header** : It is a Optional parameter, String that is written at beginning of the file. |
| 97 | +- **footer** : It is a Optional parameter, String that is written at ending of the file. |
| 98 | +- **encoding** : It is a Optional parameter, Encoding of the output file. (By Default it is None) |
| 99 | + |
| 100 | +#### Example : |
| 101 | + |
| 102 | +```python |
| 103 | +import numpy as np |
| 104 | + |
| 105 | +arr = np.array([1.1,2.2,3,4.4,5]) |
| 106 | +np.savetxt("example.txt",arr) #saves the array in example.txt |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | +Inorder to load the array from example.txt |
| 111 | + |
| 112 | +```python |
| 113 | + |
| 114 | +arr1 = np.loadtxt("example.txt") |
| 115 | +print(arr1) |
| 116 | + |
| 117 | +``` |
| 118 | +**Output** : |
| 119 | +```python |
| 120 | +[1.1 2.2 3. 4.4 5. ] |
| 121 | +``` |
| 122 | + |
| 123 | + |
| 124 | +By using these methods, you can efficiently save and load NumPy arrays in various formats suitable for your needs. |
| 125 | + |
| 126 | + |
0 commit comments