numpy
  1. numpy-numpyloadtxt

numpy.loadtxt()

The numpy.loadtxt() function in NumPy is used to load data from a text file into an array. This function reads numeric data from a text file and returns the data in the form of an array.

Syntax

The basic syntax of the numpy.loadtxt() function is as follows:

numpy.loadtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None)
  • fname: This is the name of the text file to be read.
  • dtype: This is the type of data to be used. By default, it is set to float.
  • comments: This defines the comment character. Any lines starting with this character are ignored.
  • delimiter: This is the character used to separate values in the text file. By default, it is set to any whitespace.
  • converters: This is a dictionary containing functions to convert the values in certain columns.
  • skiprows: This is the number of rows to skip from the beginning of the text file.
  • usecols: This specifies wich columns to be read from the text file.
  • unpack: This is used to transpose rows and columns of the loaded matrix as per the value of unpack.
  • ndmin: This specifies the minimum number of dimensions for the output array.
  • encoding: This specifies the encoding of the text file.
  • max_rows: This specifies the maximum number of rows to read from the beginning of the text file.

Example

Consider the following example, where a text file named data.txt containing comma-separated values is to be read and loaded into a NumPy array:

import numpy as np

# specify the file name with path
file_name = "/path/to/data.txt"

# load the text file into a NumPy array
data = np.loadtxt(file_name, delimiter=",")

print(data)

In this example, the numpy.loadtxt() function is used to read the data from the text file separated by comma, and store it in the data variable.

Output

The output of the numpy.loadtxt() function will be the data from the text file loaded into a NumPy array.

[[ 0.1  0.2  0.3]
 [ 1.1  1.2  1.3]
 [ 2.1  2.2  2.3]]

Explanation

The numpy.loadtxt() function is used to load data from a text file into a NumPy array. In this example, the function reads the comma-separated values from the text file named data.txt, and returns the data in the form of a NumPy array.

Use

The numpy.loadtxt() function can be used to read and load data from various types of text files into NumPy arrays. This function can be useful in data analysis applications where data needs to be imported and processed.

Important Points

  • The numpy.loadtxt() function assumes that the input text file contains only numeric data.
  • The delimiter in the text file should be specified before loading the data.
  • The numpy.loadtxt() function can read data from both ASCII and Unicode text files.
  • The converter parameter can be used to provide custom converters to convert the values in certain columns.
  • The skiprows parameter can be used to skip a specific number of rows at the beginning of the file.

Summary

The numpy.loadtxt() function in NumPy is used to load data from a text file into a NumPy array. This function can be used to read and load data from various types of text files into NumPy arrays. It is important to specify the delimiter, skiprows, and other parameters correctly while loading data using this function. The numpy.loadtxt() function can be a useful utility in data analysis and processing applications.

Published on: