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] 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**
+ + ![image](https://github.com/Santhosh-Siddhardha/learn-python/assets/103999924/a0148d29-5fba-45fa-b3f4-058406b3016b) + + **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.