Numpy loadtxt Step by Step Implementation with Examples

Numpy loadtxt Step by Step Implementation with Examples

Popular Domains for just 99 Cents at Namecheap!

Numpy loadtxt is a method to load text files faster as compared to the default python interpreter. You can load simple text files and manipulate them easily. In this entire tutorial, you will learn how to implement the NumPy loadtxt method.

Before going to the coding part let’s first learn the syntax for it.

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

The most generally used parameters explanation are below.

fname: File, filename, or generator to read. If the filename extension is .gz or .bz2, the file is first decompressed.

dtype: Data-type of the resulting array; default is of float type.

delimiter : The string used to separate values. The default is whitespace.

converters: A dictionary mapping column number to a function that will parse the column string into the desired value. E.g., if column 0 is a date string: converters = {0: datestr2num}. The default values is None.

skiprows: It allows you to skip the lines of text. Default is 0.

 

Step by Step to implement numpy loadtxt

For more understanding follow the steps given here.

Step 1: Import all the necessary libraries.

In our example, We are importing two python modules one is NumPy, and the other is StringIO from the io module. Anything inside the StringIO acts as a File Object. Let’s import all of them.

import numpy as np
from io import StringIO

Step 2: Execute the following Examples.

Example 1: Directly loading File Object inside the numpy.loadtxt() method.

Let’s create a text file. You should note that the number of columns before the newline operator(\n) and after should be the same.

str = StringIO("10 11 12 13 \n14 15 16 17")
np.loadtxt(str)

Output

Directly loading File Object inside the numpy.loadtxt() method
Directly loading File Object inside the numpy.loadtxt() method

Example 2: Using the delimiter as an argument.

In this example, We will create a String file separated with comma (,). Then I will load the text file with the delimiter = ‘,’ as an argument.

str2 = StringIO("10, 11, 12, 13, \n14, 15, 16, 17")
np.loadtxt(str2,delimiter=", ",usecols =(0, 1, 2,3),unpack=True )

The usecols =(0, 1, 2,3) will group the 0,1,2 and 3 element before and after the “\n” character. And to retrieve each element We will set unpack to True.

The output of the above code is below.

Using the delimiter as an argument in numpy loadtxt method
Using the delimiter as an argument in NumPy loadtxt method

 

Example 3: Categorizing the text file.

Suppose you have text data you want to categorize. Then you can use the dtype as an argument. For example, I have a text file with M and F abbreviation as Male and Female respectively.

str3 = StringIO("M 100 200\nF 300 400")
np.loadtxt(str3, dtype={'names': ('gender', 'age', 'weight'),
'formats': ('S1', 'i4', 'f4')})

You will get the following output

Categorizing the text file using numpy loadtxt method
Categorizing the text file using numpy loadtxt method

Example 4: Retrieving data from a text file.

In this example, I will load a text file to the loadtxt() method. Then after will print out the values. The text will have two columns of rollNo and its corresponding age.

RollNo Age
1      26
2      25
3      39
4      27 

 

roll,age = np.loadtxt('text.data.txt', # name of file
    skiprows = 1, # skip first row
    unpack = True # make each column an array
)
print(roll)
print(age)

Output

Retrieving data from a text file
Retrieving data from a text file

Conclusion

You will often use NumPy loadtxt method for loading text files. It is not even faster but also manipulates data easily. These are the methods that I have described will be often applied. So understand it properly.

If you have any queries regarding this post, then you can contact us for more information.

 

Source:

Official Numpy loadtxt Documentation

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner