From 0a2ca67628da43f74aa1dda7278b11fbadc4a2ac Mon Sep 17 00:00:00 2001 From: Lingamuneni Santhosh Siddhardha <103999924+Santhosh-Siddhardha@users.noreply.github.com> Date: Thu, 23 May 2024 23:07:21 +0530 Subject: [PATCH 1/4] Create saving_numpy_arrays_to_files.md Added introduction Added save method --- contrib/numpy/saving_numpy_arrays_to_files.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 contrib/numpy/saving_numpy_arrays_to_files.md diff --git a/contrib/numpy/saving_numpy_arrays_to_files.md b/contrib/numpy/saving_numpy_arrays_to_files.md new file mode 100644 index 0000000..8856a0c --- /dev/null +++ b/contrib/numpy/saving_numpy_arrays_to_files.md @@ -0,0 +1,40 @@ +# Saving NumPy Arrays to Files + +- Saving arrays in NumPy is important due to its efficiency in storage and speed, maintaining data integrity and precision, and offering convenience and interoperability. +- NumPy provides several methods to save arrays efficiently, either in binary or text formats. +- The primary methods are `save`, `savez`, and `savetxt`. + +### 1. numpy.save(): + +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. + +#### Syntax : + + ```python + numpy.save(file, arr, allow_pickle=True, fix_imports=True) + ``` +- **file** : Name of the file. +- **arr** : Array to be saved. +- **allow_pickle** : This is an Optional parameter, Allows saving object arrays using Python pickles.(By Default True) +- **fix_imports** : This is an Optional parameter, Fixes issues for Python 2 to Python 3 compatibility.(By Default True) + +#### Example : + +```python +import numpy as np + +arr = np.array([1,2,3,4,5]) +np.save("example.npy",arr) #saves arr into example.npy file in binary format +``` + +Inorder to load the array from example.npy + +```python +arr1 = np.load("example.npy") +print(arr1) +``` +**Output** : + +```python +[1,2,3,4,5] +``` From e734ba6a3ef7eb4e6a3fa746be6b2211cf724380 Mon Sep 17 00:00:00 2001 From: Lingamuneni Santhosh Siddhardha <103999924+Santhosh-Siddhardha@users.noreply.github.com> Date: Fri, 24 May 2024 01:09:23 +0530 Subject: [PATCH 2/4] Update saving_numpy_arrays_to_files.md Added savez method --- contrib/numpy/saving_numpy_arrays_to_files.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/contrib/numpy/saving_numpy_arrays_to_files.md b/contrib/numpy/saving_numpy_arrays_to_files.md index 8856a0c..0d45e5d 100644 --- a/contrib/numpy/saving_numpy_arrays_to_files.md +++ b/contrib/numpy/saving_numpy_arrays_to_files.md @@ -38,3 +38,57 @@ print(arr1) ```python [1,2,3,4,5] ``` +### 2. numpy.savez(): + +The `np.savez` function saves multiple NumPy arrays into a single file with a `.npz` extension. Each array is stored with a unique name. + +#### Syntax : + + ```python +numpy.savez(file, *args, **kwds) + ``` +- **file** : Name of the file. +- **args** : Arrays to be saved.( If arrays are unnamed, they are stored with default names like arr_0, arr_1, etc.) +- **kwds** : Named arrays to be saved. + +#### Example : + +```python +import numpy as np + +arr1 = np.array([1,2,3,4,5]) +arr2 = np.array(['a','b','c','d']) +arr3 = np.array([1.2,3.4,5]) +np.savez('example.npz', a1=arr1, a2=arr2, a3 = arr3) #saves arrays in npz format + +``` + +Inorder to load the array from example.npz + +```python + +arr = np.load('example.npz') +print(arr['a1']) +print(arr['a2']) +print(arr['a3']) + +``` +**Output** : +```python +[1 2 3 4 5] +['a' 'b' 'c' 'd'] +[1.2 3.4 5. ] +``` + +### 3. np.savetxt() + +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. + +#### Syntax : + + ```python +numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ', encoding=None) + ``` +- **fname** : Name of the file. +- **X** : Array to be saved. +- **kwds** : Named arrays to be saved. From f715f2751c58659a254e7a18905e38c89e1558af Mon Sep 17 00:00:00 2001 From: Lingamuneni Santhosh Siddhardha <103999924+Santhosh-Siddhardha@users.noreply.github.com> Date: Fri, 24 May 2024 09:06:55 +0530 Subject: [PATCH 3/4] Update saving_numpy_arrays_to_files.md Added examples Added savetxt method --- contrib/numpy/saving_numpy_arrays_to_files.md | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/contrib/numpy/saving_numpy_arrays_to_files.md b/contrib/numpy/saving_numpy_arrays_to_files.md index 0d45e5d..c8935b8 100644 --- a/contrib/numpy/saving_numpy_arrays_to_files.md +++ b/contrib/numpy/saving_numpy_arrays_to_files.md @@ -87,8 +87,40 @@ The `np.savetxt` function saves a NumPy array to a text file, such as `.txt` or #### Syntax : ```python -numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ', encoding=None) +numpy.savetxt(fname, X, delimiter=' ', newline='\n', header='', footer='', encoding=None) ``` - **fname** : Name of the file. - **X** : Array to be saved. -- **kwds** : Named arrays to be saved. +- **delimiter** : It is a Optional parameter,This is a character or string that is used to separate columns.(By Default it is " ") +- **newline** : It is a Optional parameter, Character for seperating lines.(By Default it is "\n") +- **header** : It is a Optional parameter, String that is written at beginning of the file. +- **footer** : It is a Optional parameter, String that is written at ending of the file. +- **encoding** : It is a Optional parameter, Encoding of the output file. (By Default it is None) + +#### Example : + +```python +import numpy as np + +arr = np.array([1.1,2.2,3,4.4,5]) +np.savetxt("example.txt",arr) #saves the array in example.txt + +``` + +Inorder to load the array from example.txt + +```python + +arr1 = np.loadtxt("example.txt") +print(arr1) + +``` +**Output** : +```python +[1.1 2.2 3. 4.4 5. ] +``` + + +By using these methods, you can efficiently save and load NumPy arrays in various formats suitable for your needs. + + From 1306c8e37ec17ba45166587f781b9036b5e03b2b Mon Sep 17 00:00:00 2001 From: Lingamuneni Santhosh Siddhardha <103999924+Santhosh-Siddhardha@users.noreply.github.com> Date: Fri, 24 May 2024 09:19:01 +0530 Subject: [PATCH 4/4] Update index.md Added saving numpy arrays into files section --- contrib/numpy/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/numpy/index.md b/contrib/numpy/index.md index f784798..18ed17a 100644 --- a/contrib/numpy/index.md +++ b/contrib/numpy/index.md @@ -6,3 +6,4 @@ - [Basic Mathematics](basic_math.md) - [Operations on Arrays in NumPy](operations-on-arrays.md) - [Loading Arrays from Files](loading_arrays_from_files.md) +- [Saving Numpy Arrays into FIles](saving_numpy_arrays_to_files.md)