From 0b9c7823fac1eb920e4bda3cd7bb5667a7023601 Mon Sep 17 00:00:00 2001
From: Lingamuneni Santhosh Siddhardha
<103999924+Santhosh-Siddhardha@users.noreply.github.com>
Date: Sun, 19 May 2024 22:09:39 +0530
Subject: [PATCH 1/5] Create loading_arrays_from_files.md
Added Introduction
Added numpy.loadtxt method
Added numpy.genfromtxt method
Added numpy.fromfile method
Added numpy.load method
---
contrib/numpy/loading_arrays_from_files.md | 67 ++++++++++++++++++++++
1 file changed, 67 insertions(+)
create mode 100644 contrib/numpy/loading_arrays_from_files.md
diff --git a/contrib/numpy/loading_arrays_from_files.md b/contrib/numpy/loading_arrays_from_files.md
new file mode 100644
index 0000000..f1717c2
--- /dev/null
+++ b/contrib/numpy/loading_arrays_from_files.md
@@ -0,0 +1,67 @@
+# Loading Arrays From Files
+The ability to load data from various file formats is a critical feature for scientific computing and data analysis.
+NumPy provides several functions to read data from different file types and convert them into ndarrays.
+This section will cover how to load ndarrays from common file formats, including CSV, TSV, and binary files.
+
+### Here are the methods available:
+
+`numpy.loadtxt`: The loadtxt function allows you to load data from a text file.You can specify various parameters such as the file name, data type, delimiter,
+and more. It reads the file line by line, splits it at the specified delimiter, and converts the values into an array.
+
+- **Syntax:**
+ ```python
+ numpy.loadtxt(fname, dtype = float, delimiter=None, converters=None, skiprows=0, usecols=None)
+ ```
+
+ `fname` : Name of the file
+ `dtype` : Data type of the resulting array. (By default is float)
+ `delimiter`: String or character separating columns; default is any whitespace.
+ `converters`: Dictionary mapping column number to a function to convert that column's string to a float.
+ `skiprows`: Number of lines to skip at the beginning of the file.
+ `usecols`: Which columns to read starting from 0.
+
+- **Example for `loadtxt`:**
+
+ **example.txt**
+
+ 
+
+ **Code**
+ ```python
+ import numpy as np
+ arr = np.loadtxt("loadtxt.txt", dtype=int)
+ print(arr)
+ ```
+
+ **Output**
+ ```python
+ [1 2 3 4 5]
+ ```
+
+
+`numpy.genfromtxt`: The genfromtxt function is similar to loadtxt but provides more flexibility. It handles missing values (such as NaNs), allows custom converters
+for data parsing, and can handle different data types within the same file. It’s particularly useful for handling complex data formats.
+
+- **Syntax:**
+ ```python
+ numpy.genfromtxt(fname, dtype=float, delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None)
+ ```
+
+ `fname` : Name of the file
+ `dtype` : Data type of the resulting array. (By default is float)
+ `delimiter`: String or character separating columns; default is any whitespace.
+ `skip_header`: Number of lines to skip at the beginning of the file.
+ `skip_footer`: Number of lines to skip at the end of the file.
+ `converters`: Dictionary mapping column number to a function to convert that column's string to a float.
+ `missing_values`: Set of strings corresponding to missing data.
+ `filling_values`: Value used to fill in missing data. Default is NaN.
+ `usecols`: Which columns to read starting from 0.
+
+- **Examples for `genfromtxt`:**
+
+
+`numpy.fromfile`: The fromfile function reads binary data directly from a file into a NumPy array. It doesn’t assume any specific format or delimiter;
+instead, it interprets the raw binary data according to the specified data type.
+
+`numpy.load`: Load arrays saved in NumPy’s native binary format (.npy or .npz). These files preserve the array structure, data types, and metadata.
+It’s an efficient way to store and load large arrays.
From 811382bfca0d9097c78cff56b9e1fb10655b4088 Mon Sep 17 00:00:00 2001
From: Lingamuneni Santhosh Siddhardha
<103999924+Santhosh-Siddhardha@users.noreply.github.com>
Date: Sun, 19 May 2024 23:55:40 +0530
Subject: [PATCH 2/5] Update loading_arrays_from_files.md
Added syntax for all methods
Formatted Content
Added examples for all methods
Removed fromfile method
---
contrib/numpy/loading_arrays_from_files.md | 103 +++++++++++++++------
1 file changed, 75 insertions(+), 28 deletions(-)
diff --git a/contrib/numpy/loading_arrays_from_files.md b/contrib/numpy/loading_arrays_from_files.md
index f1717c2..bd3648f 100644
--- a/contrib/numpy/loading_arrays_from_files.md
+++ b/contrib/numpy/loading_arrays_from_files.md
@@ -1,26 +1,25 @@
# Loading Arrays From Files
-The ability to load data from various file formats is a critical feature for scientific computing and data analysis.
-NumPy provides several functions to read data from different file types and convert them into ndarrays.
-This section will cover how to load ndarrays from common file formats, including CSV, TSV, and binary files.
+Scientific computing and data analysis require the critical feature of being able to load data from different file formats. NumPy has several functionalities for reading data from various file types and converting them into arrays. This part of the content will show how one can load arrays from standard file formats.
-### Here are the methods available:
+## Here are the methods available:
-`numpy.loadtxt`: The loadtxt function allows you to load data from a text file.You can specify various parameters such as the file name, data type, delimiter,
+### 1. numpy.loadtxt():
+The loadtxt function allows you to load data from a text file.You can specify various parameters such as the file name, data type, delimiter,
and more. It reads the file line by line, splits it at the specified delimiter, and converts the values into an array.
-- **Syntax:**
+- #### Syntax:
```python
numpy.loadtxt(fname, dtype = float, delimiter=None, converters=None, skiprows=0, usecols=None)
```
- `fname` : Name of the file
- `dtype` : Data type of the resulting array. (By default is float)
- `delimiter`: String or character separating columns; default is any whitespace.
- `converters`: Dictionary mapping column number to a function to convert that column's string to a float.
- `skiprows`: Number of lines to skip at the beginning of the file.
- `usecols`: Which columns to read starting from 0.
+ **fname** : Name of the file
+ **dtype** : Data type of the resulting array. (By default is float)
+ **delimiter**: String or character separating columns; default is any whitespace.
+ **converters**: Dictionary mapping column number to a function to convert that column's string to a float.
+ **skiprows**: Number of lines to skip at the beginning of the file.
+ **usecols**: Which columns to read starting from 0.
-- **Example for `loadtxt`:**
+- #### Example for `loadtxt`:
**example.txt**
@@ -38,30 +37,78 @@ and more. It reads the file line by line, splits it at the specified delimiter,
[1 2 3 4 5]
```
+
-`numpy.genfromtxt`: The genfromtxt function is similar to loadtxt but provides more flexibility. It handles missing values (such as NaNs), allows custom converters
+### 2. numpy.genfromtxt:
+The `genfromtxt` function is similar to loadtxt but provides more flexibility. It handles missing values (such as NaNs), allows custom converters
for data parsing, and can handle different data types within the same file. It’s particularly useful for handling complex data formats.
-- **Syntax:**
+- #### Syntax:
```python
numpy.genfromtxt(fname, dtype=float, delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None)
```
- `fname` : Name of the file
- `dtype` : Data type of the resulting array. (By default is float)
- `delimiter`: String or character separating columns; default is any whitespace.
- `skip_header`: Number of lines to skip at the beginning of the file.
- `skip_footer`: Number of lines to skip at the end of the file.
- `converters`: Dictionary mapping column number to a function to convert that column's string to a float.
- `missing_values`: Set of strings corresponding to missing data.
- `filling_values`: Value used to fill in missing data. Default is NaN.
- `usecols`: Which columns to read starting from 0.
+ **fname** : Name of the file
+ **dtype** : Data type of the resulting array. (By default is float)
+ **delimiter**: String or character separating columns; default is any whitespace.
+ **skip_header**: Number of lines to skip at the beginning of the file.
+ **skip_footer**: Number of lines to skip at the end of the file.
+ **converters**: Dictionary mapping column number to a function to convert that column's string to a float.
+ **missing_values**: Set of strings corresponding to missing data.
+ **filling_values**: Value used to fill in missing data. Default is NaN.
+ **usecols**: Which columns to read starting from 0.
-- **Examples for `genfromtxt`:**
+- #### Example for `genfromtxt`:
+
+ **example.txt**
+
+ 
-`numpy.fromfile`: The fromfile function reads binary data directly from a file into a NumPy array. It doesn’t assume any specific format or delimiter;
-instead, it interprets the raw binary data according to the specified data type.
+ **Code**
+ ```python
+ import numpy as np
+ arr = np.genfromtxt("example.txt", dtype='str', usecols=1)
+ print(arr)
+ ```
-`numpy.load`: Load arrays saved in NumPy’s native binary format (.npy or .npz). These files preserve the array structure, data types, and metadata.
+ **Output**
+ ```python
+ ['Name' 'Kohli' 'Dhoni' 'Rohit']
+ ```
+
+
+
+
+### 3. numpy.load
+`load` method is used to load arrays saved in NumPy’s native binary format (.npy or .npz). These files preserve the array structure, data types, and metadata.
It’s an efficient way to store and load large arrays.
+
+- #### Syntax:
+ ```python
+ numpy.load(fname, mmap_mode=None, encoding='ASCII')
+ ```
+
+ **fname** : Name of the file
+ **mmap_mode** : Memory-map the file using the given mode (r, r+, w+, c).(By Default None)
+ **encoding**:Encoding is used when reading Python2 strings only. (By Default ASCII)
+
+- #### Example for `load`:
+
+ **Code**
+ ```python
+ import numpy as np
+ arr = np.array(['a','b','c'])
+ np.savez('data.npz', array=arr)
+ # stores arr in data.npz in NumPy's native binary format
+ data = np.load('data.npz')
+ print(data['array'])
+ ```
+
+ **Output**
+ ```python
+ ['a' 'b' 'c']
+ ```
+
+
+These methods empower users to seamlessly integrate data into their scientific workflows, whether from text files or binary formats.
From efb68d962d83cda93705e6e2c35fefb7622d54a4 Mon Sep 17 00:00:00 2001
From: Lingamuneni Santhosh Siddhardha
<103999924+Santhosh-Siddhardha@users.noreply.github.com>
Date: Sun, 19 May 2024 23:57:34 +0530
Subject: [PATCH 3/5] Update index.md
Added Loading Arrays from Files Section
Linked it to its md file
---
contrib/numpy/index.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contrib/numpy/index.md b/contrib/numpy/index.md
index 82596a2..5c8cfa6 100644
--- a/contrib/numpy/index.md
+++ b/contrib/numpy/index.md
@@ -1,3 +1,3 @@
# List of sections
-- [Section title](filename.md)
+- [Loading Arrays from Files](loading_arrays_from_files.md)
From 53269a5ad335eb89bb768ca26165f447a3f46d22 Mon Sep 17 00:00:00 2001
From: Lingamuneni Santhosh Siddhardha
<103999924+Santhosh-Siddhardha@users.noreply.github.com>
Date: Sun, 19 May 2024 23:58:31 +0530
Subject: [PATCH 4/5] Update loading_arrays_from_files.md
changed format
---
contrib/numpy/loading_arrays_from_files.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contrib/numpy/loading_arrays_from_files.md b/contrib/numpy/loading_arrays_from_files.md
index bd3648f..85105a3 100644
--- a/contrib/numpy/loading_arrays_from_files.md
+++ b/contrib/numpy/loading_arrays_from_files.md
@@ -4,7 +4,7 @@ Scientific computing and data analysis require the critical feature of being abl
## Here are the methods available:
### 1. numpy.loadtxt():
-The loadtxt function allows you to load data from a text file.You can specify various parameters such as the file name, data type, delimiter,
+The `loadtxt` function allows you to load data from a text file.You can specify various parameters such as the file name, data type, delimiter,
and more. It reads the file line by line, splits it at the specified delimiter, and converts the values into an array.
- #### Syntax:
From cfc731c2acb0b85011b47f3a4b3fe3fc2ea0b91b Mon Sep 17 00:00:00 2001
From: Lingamuneni Santhosh Siddhardha
<103999924+Santhosh-Siddhardha@users.noreply.github.com>
Date: Mon, 20 May 2024 09:50:49 +0530
Subject: [PATCH 5/5] Update loading_arrays_from_files.md
Changed introduction content
formatted code
formatted content
---
contrib/numpy/loading_arrays_from_files.md | 27 +++++++++++-----------
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/contrib/numpy/loading_arrays_from_files.md b/contrib/numpy/loading_arrays_from_files.md
index 85105a3..c001121 100644
--- a/contrib/numpy/loading_arrays_from_files.md
+++ b/contrib/numpy/loading_arrays_from_files.md
@@ -14,7 +14,7 @@ and more. It reads the file line by line, splits it at the specified delimiter,
**fname** : Name of the file
**dtype** : Data type of the resulting array. (By default is float)
- **delimiter**: String or character separating columns; default is any whitespace.
+ **delimiter**: String or character separating columns. (By default is whitespace)
**converters**: Dictionary mapping column number to a function to convert that column's string to a float.
**skiprows**: Number of lines to skip at the beginning of the file.
**usecols**: Which columns to read starting from 0.
@@ -27,8 +27,9 @@ and more. It reads the file line by line, splits it at the specified delimiter,
**Code**
```python
- import numpy as np
- arr = np.loadtxt("loadtxt.txt", dtype=int)
+ import numpy as np
+
+ arr = np.loadtxt("example.txt", dtype=int)
print(arr)
```
@@ -39,20 +40,18 @@ and more. It reads the file line by line, splits it at the specified delimiter,
-### 2. numpy.genfromtxt:
+### 2. numpy.genfromtxt():
The `genfromtxt` function is similar to loadtxt but provides more flexibility. It handles missing values (such as NaNs), allows custom converters
for data parsing, and can handle different data types within the same file. It’s particularly useful for handling complex data formats.
- #### Syntax:
```python
- numpy.genfromtxt(fname, dtype=float, delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None)
+ numpy.genfromtxt(fname, dtype=float, delimiter=None, converters=None, missing_values=None, filling_values=None, usecols=None)
```
**fname** : Name of the file
**dtype** : Data type of the resulting array. (By default is float)
**delimiter**: String or character separating columns; default is any whitespace.
- **skip_header**: Number of lines to skip at the beginning of the file.
- **skip_footer**: Number of lines to skip at the end of the file.
**converters**: Dictionary mapping column number to a function to convert that column's string to a float.
**missing_values**: Set of strings corresponding to missing data.
**filling_values**: Value used to fill in missing data. Default is NaN.
@@ -67,7 +66,8 @@ for data parsing, and can handle different data types within the same file. It
**Code**
```python
- import numpy as np
+ import numpy as np
+
arr = np.genfromtxt("example.txt", dtype='str', usecols=1)
print(arr)
```
@@ -80,7 +80,7 @@ for data parsing, and can handle different data types within the same file. It
-### 3. numpy.load
+### 3. numpy.load():
`load` method is used to load arrays saved in NumPy’s native binary format (.npy or .npz). These files preserve the array structure, data types, and metadata.
It’s an efficient way to store and load large arrays.
@@ -90,7 +90,7 @@ It’s an efficient way to store and load large arrays.
```
**fname** : Name of the file
- **mmap_mode** : Memory-map the file using the given mode (r, r+, w+, c).(By Default None)
+ **mmap_mode** : Memory-map the file using the given mode (r, r+, w+, c)(By Default None).Memory-mapping only works with arrays stored in a binary file on disk, not with compressed archives like .npz.
**encoding**:Encoding is used when reading Python2 strings only. (By Default ASCII)
- #### Example for `load`:
@@ -98,10 +98,11 @@ It’s an efficient way to store and load large arrays.
**Code**
```python
import numpy as np
+
arr = np.array(['a','b','c'])
- np.savez('data.npz', array=arr)
- # stores arr in data.npz in NumPy's native binary format
- data = np.load('data.npz')
+ np.savez('example.npz', array=arr) # stores arr in data.npz in NumPy's native binary format
+
+ data = np.load('example.npz')
print(data['array'])
```