Python

Numpy Loadtxt

In real-life scenarios, the data often lines up in the file system, so the NumPy python module provides a function load txt that loads data from a text file in an efficient way. The function loadtxt is primarily used for reading and writing arrays or matrices to the text file.

This article will discuss the fundamentals of NumPy load txt that could save large data sets, the data you want to read over a serial, or you want to write those to a text file or load them in the form of a simple formatted text file.

Let’s get started with the example code implementation of this NumPy load txt function.

Syntax of NumPy load text in Python

The general syntax we use for the NumPy load txt function in python looks in this way.

>>numpy.loadtxt(fname,dtype=float,comments='#',delimiter=None,converters=None,skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None)

Parameters passed in Numpy load txt

The following are the parameters passed in Numpy load txt.

fName: It represents a filename or file that has to be read.

Dtype: It’s an optional parameter that defines the float data type of the resulting array.

Delimiter: It is considered as a string that is used to distinct values. By default, it will be whitespace Comments. List of strings or a string that indicates the state of the comment. By default, it will be “#”.

Converters: This parameter is taken as dictionary mapping for matching column index to a function that will convert a mapped column into a float value. The default value is taken as none.

Skip rows: The skip rows parameter is used to skip the first specified number of lines, including comments.

Usecol: It is also an optional parameter that defines the column which is to be read from beginning with zero first.

unpack: It defines the Boolean value as true or false. If set as true, then it will return the transpose of the array individually.

ndim: An optional parameter that will return the minimum array number of dimension arrays.

Encoding: When the input file is used for encoding and decoding the data. The default value is in bytes.

Max-row: After the skip rows line, it reads the maximum number of rows, or by default, it will read all lines.

Return value in numpy loadtxt

It returns an n-dimensional(array) array with imported data when the read file path is specified in the first argument.

Example 1:

We are using the numpy loadtxt function in the example below. We have imported the numpy module and also the StringIO from the io function. This StringIo behaves like a file object.

In a variable “A1” we have passed stringIO with the different values, which are like a file path. In a variable “A2” we have called loadtxt function and passed the parameter “A1” which returns the file path.

You can verify the expected output by the print statement which gives the loadtxt return value.

import numpy

from io import StringIO

 

A1 = StringIO("3 1 7\n 2 8 6")

A2 = numpy.loadtxt(A1)

 

print(A2)

The output in the below images shows the loaded array values from numpy.loadtxt function.

Example 2:

In the example, we use the dtype parameter in the loadtxt function, which represents the data type of returned array. Here, we have a variable represented as “Var_i” which is initialized with the stringIO.

In stringIO, we have recorded the file information. We have another variable as “Var_j” for initializing the loadtxt function. In the loadtxt function, we have set the parameter “dtype” which records the fields. The field names are “Gender_Name”, “Age_values”, and the “Weight_value” and also construct the format in which they display.

Then we have a print statement taking a parameter as “var_j” that will return the loaded values as an output.

import NumPy

from io import StringIO

 

Var_i = StringIO("Male 35 54.09 \n Female 29 40.99")

Var_j=numpy.loadtxt(Var_i, dtype={'names':('Gender_name','Age_value','Weight_value'), 'formats': ('S10','i4','f4')})

 

print(Var_j)

The output displays the loaded values of the field assigned to them.

Example 3:

The example program takes “usecols” as a parameter in the loadtxt function. The parameter “usecols” specifies which column is to be read. Here in the example program, we have a variable defined as “S1” and assigned a StringIO value and passed a data in the function.

Then we have another variable declared as “S2” which is assigned to the loadtxt function. The loadtxt function will take a parameter of a “filename”, “datatype”, and set “usecols” as (0,1). The print statement will show the returned value of the loadtxt function.

import NumPy

from io import StringIO

 

S1 = StringIO("5 4.67 \n 12 6.9 \n 4 1 ")

S2 = numpy.loadtxt(S1, dtype="float", usecols =(0, 1))

 

print("The Loaded array values are:")

print(S2)

The output shows the content of the loaded file in a form of ndarray.

Example 4:

In another example of the loadtxt function, we have set the parameter as “unpack” which takes a true Boolean value. It will transpose the array and then unpack the transposed array into the given variables.

So at first, we declared a variable “Var_a” and initialized it with the StringIO function. The StringIO has data in it. Secondly, we call the loadtxt function, which has parameters as filename, dtype, and unpack to true.

The loadtxt function returns the unpacked array which is specified with the variables as “l”, “m”, and “n”. Through a print statement, we can have the loaded array.

import NumPy as np

from io import StringIO

 

Var_a = StringIO("2 4 1\n 5 8 3 \n 0 9 6")

(l,m,n) = np.loadtxt(Var_a,dtype="int",unpack=True)

 

print("The loaded array output is:")

 

print(l)

print(m)

print(n)

As shown, the output loaded array on the console screen of the Spyder terminal.

Example 5:

Here, we have an example program in which we have manually set a delimiter parameter in the loadtxt function. Firstly, we defined a variable “var1” with file data in it and passed it into a stringIO function.

Then we have called loadtxt function in a specified variable “p”, “q”, and “r”. The loadtxt function takes the “filename” as “var1”, the delimiter is set as comma (,) which separates the values while reading the text in the file.

We have another parameter “usecols” which reads the columns by given values and the parameter “unpack” is set to true. Lastly, we have a print function that displays the return value of numpy loadtxt function.

import numpy

from io import StringIO

 

var1 = StringIO("0, 9, 6\n4, 8, 10")

p, q, r = numpy.loadtxt(var1, delimiter =', ', usecols =(0, 1, 2), unpack = True)

 

print("p = ", p)

print("q = ", q)

print("r = ", r)

In the output below, we have the content of a file in the form of a loaded array.

Conclusion

All in all, we have discussed the numpy loadtxt function through its syntax and examples. We have distinguished the parameters passed in the numpy load.txt with the example program. However, the numpy loadtxt function allows reading data from the file by specifying the loadtxt function options.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content