Numpy Loadtxt
If you are working with data in Python, chances are that you will need to read and write data from text files. This can be accomplished by using the “numpy.loadtxt()” function of the “numpy” library. This function allows you to load data from a text file into a “NumPy” array, with various options to customize the data parsing and formatting.
This Python write-up will present a comprehensive tutorial on the “numpy.loadtxt()” function using numerous examples by covering the following content areas:
- What is the “loadtxt()” Function in Python?
- Reading Data From the Text File.
- Reading Data From the Text File With “dtype” Parameter.
- Reading Data From the Text File With “delimiter” Parameter.
- Reading Data From the Text File With “unpack” Parameter.
What is the “numpy.loadtxt()” Function in Python?
The “numpy.loadtxt()” function in Python is utilized to load the data from a particular text file and retrieve it as an “ndarray”. This function is designed to read simple text files quickly and there must be the same number of values in each row of the text file.
Syntax
Parameters
According to the above syntax:
- The “fname” parameter indicates a file or filename that needs to be read.
- The not mandatory “dtype” parameter indicates the data type of the returned/resulting array.
- The “comments” parameter specifies the characters or a list of characters that indicate the beginning of a comment.
- The “delimiter” parameter is used to separate values in a text file. By default, its value is set to “whitespace”.
- The “converters” parameter maps column numbers to functions that convert them to floats.
- The “skiprows”, “usecols” and “unpack” parameters are used to skip the specific number of lines, and indexes of the columns to be read, and retrieve the transpose of the array based on the boolean value, respectively.
- The “ndmin”, “encoding” and “max_rows” optional parameters specify the returned array’s min number of dimensions, the encoding utilized to decode, and the max number of rows to get/read after the particular skiprows.
Return Value
The retrieved value of “numpy.loadtxt()” function is a “ndarray” containing the data from the text file.
Example 1: Reading/Getting Data From the Specified “Text” File
The code given below is utilized to read/get data from the input text file:
import io
arr = io.StringIO("10 20 30 \n40 50 60 \n 70 80 90")
print(numpy.loadtxt(arr))
In the above code:
- The “numpy” and “io” libraries are imported.
- The “StringIO()” is used to create a string object that contains three lines of integers separated by spaces and newlines. In this case, “StringIO” behaves as if it was a file object.
- The “loadtxt()” function is utilized to read the string object as an array of numbers. The default delimiter is “whitespace”, so it will split the string by spaces and newlines.
Output
The data from the text file has been read successfully.
Now, we can use the “numpy.loadtxt()” function to read the data from the below text file:
Here is an example code:
print(numpy.loadtxt(r'C:\Users\p\Documents\program\filename.txt'))
In the above code, the “numpy.loadtxt()” function takes the complete path of the text file as an argument, reads the data from the text file, and returns a NumPy array.
Output
The above snippet verified that the data has been read successfully.
Example 2: Reading Data From the Text File With the “dtype” Parameter
In the below code, the data from the text file will be loaded with the help of the “dtype” parameter:
import io
arr = io.StringIO("10 20 30 \n40 50 60 \n 70 80 90")
print(numpy.loadtxt(arr, dtype="int"))
Here, the “dtype” parameter value “int” is passed to the “numpy.loadtxt()” function and returns the NumPy array with integer values.
Output
Based on the above snippet, all the content is loaded as integers into an array.
Example 3: Reading Data From the Text File With the “delimiter” Parameter
The following code is used to read data from a text file using the “delimiter” parameter value:
import io
arr = io.StringIO("10@ 20@ 30 \n40@ 50@ 60 \n 70@ 80@ 90")
print(numpy.loadtxt(arr, dtype="int", delimiter="@"))
In the above code, the “numpy.loadtxt()” function reads the text data separated by the “@” delimiter. By default, the delimiter is “whitespace”.
Output
Example 4: Reading Data From the Text File With the “unpack” Parameter
In the below code, we unpack the transposed array rows into particular variables utilizing the “unpack” parameter:
import io
arr = io.StringIO("10@ 20@ 30 \n40@ 50@ 60 \n 70@ 80@ 90")
(x1, y1, z1) = numpy.loadtxt(arr, dtype="int", delimiter="@", unpack=True)
print(x1)
print(y1)
print(z1)
According to the above code:
- The “loadtxt()” function is utilized to read the string or file-like object data and store it in three arrays.
- The “loadtxt()” function takes three parameters: “dtype”, “delimiter” and “unpack”, respectively.
- The “dtype” parameter specifies the data type as integers and the “delimiter” parameter specifies the character that separates the numbers in each line.
- Lastly, the “unpack” parameter specifies that the columns of the data should be returned as separate arrays.
Output
As analyzed, the array has been unpacked and assigned to three different variables.
Conclusion
In Python, the “numpy.loadtxt()” function of the “numpy” library is utilized to load the data from the text file and retrieve it as “ndarray”. The “numpy.loadtxt()” function is used to accept various parameters such as “dtype”, “delimiter” and “unpack” and perform certain operations on the text data accordingly This write-up delivered a thorough guide on Python “numpy.loadtxt()” function using numerous examples.
Source: linuxhint.com