Numpy loadtxt() Explained with Examples

Python NumPy loadtxt() function is used to load the data from a text file and store them in a ndarray. The purpose of loadtxt() function is to be a fast reader for simple text files. Each row in the text file must have the same number of values.

In this article, I will explain python numpy.loadtxt() function syntax and how we can use its parameters while reading the data from the text files with examples.

1. Quick Examples of NumPy loadtxt() Function

If you are in a hurry, below are some quick examples of how to use loadtxt() function to get NumPy Array.


# Below are the quick examples

# Example 1: numpy read txt file using numpy.loadtxt() function
arr = StringIO("5 8 11 \n14 19 21 \n 24 32 36")
arr2 = np.loadtxt(arr) 

# Example 2: Use numpy.loadtxt() function to set dtype parameter
arr2 = np.loadtxt(arr,dtype="int")

# Example 3: Use set delimiter parameter in numpy.loadtxt() function
arr = StringIO("5, 8, 11 \n14, 19, 21 \n 24, 32, 36") 
arr2 = np.loadtxt(arr,dtype="int",delimiter=",")

# Example 4: Use Set usecols Parameter in Use numpy.loadtxt() Function
arr2 = np.loadtxt(arr,dtype="int",usecols =(1,2))

# Example 5: Use set unpack parameter in numpy.loadtxt() function
(x,y,z) = np.loadtxt(arr,dtype="int",unpack=True)

# Example 6: Set delimiter, usecols, unpack Parameter
arr = StringIO("5, 8, 11 \n14, 19, 21 \n 24, 32, 36") 
x,y,z = np.loadtxt(arr,delimiter =', ', usecols =(0,1,2), unpack = True)

2. Syntax of NumPy loadtxt()

Following is the syntax of loadtxt() Function


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

2.2 Parameters of loadtxt()

Following are the parameters of the loadtxt().

  • fname – File, filename, or generator to read. If the filename extension, Path of txt file to be imported.
  • dtype – Data-type of the resulting array.
  • comments – Characters or a list of characters are used to indicate the start of a comment.
  • delimiter – The string is used to separate values. By default, this is any whitespace. The delimiter to use for parsing the content of txt a file.
  • converters – A dictionary mapping column number to a function that will parse the column string into the desired value. Default: None.
  • skiprows – Skip the first skip rows lines, including comments; default: 0.
  • usecols – The column indices to be read.
  • unpack – If True, the returned array is transposed, so that arguments may be unpacked using x, y, z = loadtxt(…). [unpack=True]
  • ndmin – The minimum number of dimensions in the returned array.
  • encoding – Encoding used to decode the inputfile.
  • max_rows – The maximum number of rows to read after skiprows lines.

2.3 Return value of loadtxt()

It returns ndarray which is loaded with the data from the text file.

Note: This function aims to be a fast reader for simply formatted files. The genfromtxt() function provides more sophisticated handling of, e.g., lines with missing values.

3. Read Data from text File Use NumPy loadtxt()

numpy.loadtxt() is used to return the n-dimensional NumPy array by reading the data from the text file, with an aim to be a fast reader for simple text files. 

This function numpy.loadtxt() can be used with both absolute and relative paths. It loads the data from the text file into the NumPy array. For that we need to import two python modules one is NumPy, and the other is StringIO from the io module. Anything inside the StringIO is a File Object. The following example reads a text from a String object.

Note: The number of columns before the newline operator(\n) and after should be the same.


import numpy as np
# StringIO behaves like a file object
from io import StringIO    
arr = StringIO("5 8 11 \n14 19 21 \n 24 32 36") 

# Read data from text file 
arr2 = np.loadtxt(arr) 
print(arr2)

Yields below output.


# Output:
[[ 5.  8. 11.]
 [14. 19. 21.]
 [24. 32. 36.]]

4. Read Text Data with dtype

By default, it reads the data as a float. you can change this by using the dtype parameter. The following example loads all the elements into an array from the text as integers.


# Read data use numpy.loadtxt() with dtype parameter
arr2 = np.loadtxt(arr,dtype="int")
print(arr2)

# Output:
# [[ 5  8 11]
# [14 19 21]
# [24 32 36]]

5. Use loadtxt() along with delimiter

By default, it uses whitespace as a delimiter. You can manually set the delimiter using the delimiter parameter. The following example reads the text separated by a comma delimiter.


# Read data use numpy.loadtxt() with delimiter parameter
arr = StringIO("5, 8, 11 \n14, 19, 21 \n 24, 32, 36") 
arr2 = np.loadtxt(arr,dtype="int",delimiter=",")
print(arr2)

# Output:
# [[ 5  8 11]
# [14 19 21]
# [24 32 36]]

6. Read Text Data with usecols

Use usecols parameter to specify which columns to be read from the txt file. The following example reads only the second and third columns from the txt file into the array.


# Read data use numpy.loadtxt() with usecols parameter
arr2 = np.loadtxt(arr,dtype="int",usecols =(1,2))
print(arr2)

# Output:
# [[ 8 11]
# [19 21]
# [32 36]]

7. Using Numpy loadtxt() with unpack Param

You can also use transpose the array and unpacks the rows of the transposed array into specified variables.


# Read data use numpy.loadtxt() with unpack parameter
(x,y,z) = np.loadtxt(arr,dtype="int",unpack=True)
print(x)
print(y)
print(z)

# Output:
# [ 5 14 24]
# [ 8 19 32]
# [11 21 36]

8. Conclusion

In this article, I have explained Python NumPy loadtxt() function to load the data from the text file. Also covered using dtype, delimiter, usecols, and unpack Parameters with examples.

Happy Learning!!

References

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.
You are currently viewing Numpy loadtxt() Explained with Examples