What is Python loadtxt() in Numpy?

Overview

In Python, we use the loadtxt() function to load data from a text file, provided that each row in the file has the same number of features or values.

Why should we use this function when we've got the genfromtxt() function to read files in a sophisticated manner, e.g., filling NA or missing values? The answer is simple. The loadtxt() function provides native Numpy arrays implementation for faster file loading and reading.

Syntax


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

Parameters

It takes the following argument values. These argument values help to process data values efficiently.

Parameters

Name

Description

Required/Optional

fname

Generator or file name to read. If the input file is of .gz or .bz2 types, it can decompress zip files first.

Required

dtype

Default=float, the data type of the resulting array. It can either be a dictionary, or a datatype name.

Optional

comments

Default='#', a character array or list used to skip data file comments.

Optional

delimiter

Default='_', a string used to separate values. By default, it is any whitespace.

Optional

converters

Default=None, dictionary to column values mapping. It can be used to process single column values.

Optional

skiprows

Default=0, the integer value used to skip rows including comments.

Optional

usecols

Default=0, an integer or sequence value used to select specified columns.

Optional

unpack

Default=False, if True, it returns ndarray transpose to unpack arguments or matrix features.

Optional

ndim

Default=0, it is used to limit the dimensions of returned ndarray.

Optional

encoding

Default='bytes', this parameter is used to decode the argument file.

Optional

max_rows

Default=None, limit to read max rows of text file after skiprows lines.

Optional

*args

Variable number of argument values.

Optional

like

Default=None, is used to create an output array that is not a Numpy array.

Optional

Return value

The loadtxt() function returns data from the text file in the form of an ndarray.

Explanation

In the example below, we're going to explain the working of the loadtxt() function in different scenarios. Let's have an in-depth coding explanation.

main.py
file.txt
0 1 2 7 8
3 4 5 6 6
7 8 9 0 -1
  • Line 5: We use the open() method to load the file.txt file in the current program and return a file descriptor fd.
  • Line 6: We use data.readlines() to create a pipeline to fetch file.txt attached to data file descriptor. The readlines() function will return an ndarray with each line of the file as a list.
  • Lines 8–10: We use the loadtxt(lines) to load data with same format.
  • Lines 12–15: We use the np.loadtxt() to load file data according to the provided dtype format.
  • Line 17: We close the file data streams with the descriptor fd associated to file.txt.

Free Resources