w3resource

A Complete Guide to numpy.loadtxt for Data Loading


Mastering numpy.loadtxt: Loading Data from Text Files in Python

numpy.loadtxt is a powerful function to read data from text files into Numpy arrays. It is commonly used to load numerical data for scientific computation, machine learning, and data analysis tasks. It supports customization options like skipping headers, handling delimiters, and specifying data types.


Syntax:

numpy.loadtxt(fname, dtype=<type 'float'>, delimiter=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes')

Parameters:

    1. fname (str or file-like object): Name of the file or a file-like object.

    2. dtype (data-type): Type of the resulting array. Default is float.

    3. delimiter (str): String used to separate values. Default is whitespace.

    4. skiprows (int): Number of lines to skip at the beginning.

    5. usecols (int or sequence): Specifies which columns to read.

    6. unpack (bool): If True, the returned array is transposed.

    7. ndmin (int): The minimum number of dimensions for the output.

    8. encoding (str): Encoding of the file.


Examples:

Example 1: Loading a Basic Text File

Code:

import numpy as np

# Assume data.txt contains:
# 1.0 2.0 3.0
# 4.0 5.0 6.0
# 7.0 8.0 9.0

# Load the text file into a Numpy array
data = np.loadtxt("data.txt")

# Print the array
print("Loaded data:\n", data)

Output:

Loaded data:
 [[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]]

Explanation:

  • This loads all numerical data from the file data.txt into a 2D Numpy array.
  • Default delimiter is whitespace.

Example 2: Specifying a Delimiter

Code:

import numpy as np

# Assume data.csv contains:
# 1,2,3
# 4,5,6
# 7,8,9

# Load the text file with a comma as a delimiter
data = np.loadtxt("data.csv", delimiter=",")

# Print the array
print("Loaded data:\n", data)

Output:

Loaded data:
 [[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]]

Explanation:

  • By specifying delimiter=",", the function reads CSV files where values are separated by commas.

Example 3: Skipping Rows

Code:

import numpy as np

# Assume data_with_header.txt contains:
# Header Line
# Another Header
# 1.0 2.0 3.0
# 4.0 5.0 6.0

# Load data, skipping the first two header lines
data = np.loadtxt("data_with_header.txt", skiprows=2)

# Print the array
print("Loaded data:\n", data)

Output:

Loaded data:
 [[1. 2. 3.]
 [4. 5. 6.]]

Explanation:

  • The skiprows parameter ignores the first two lines, which contain non-data information.


Example 4: Loading Specific Columns

Code:

import numpy as np

# Assume data.txt contains:
# 1.0 2.0 3.0
# 4.0 5.0 6.0
# 7.0 8.0 9.0

# Load only the first and third columns
data = np.loadtxt("data.txt", usecols=(0, 2))

# Print the array
print("Loaded specific columns:\n", data)

Output:

Loaded specific columns:
 [[1. 3.]
 [4. 6.]
 [7. 9.]]

Explanation:

  • The usecols parameter allows selecting specific columns by their indices.

Example 5: Unpacking Data

Code:

import numpy as np

# Assume data.txt contains:
# 1.0 2.0 3.0
# 4.0 5.0 6.0
# 7.0 8.0 9.0

# Unpack columns into separate arrays
col1, col2, col3 = np.loadtxt("data.txt", unpack=True)

# Print the unpacked columns
print("Column 1:", col1)
print("Column 2:", col2)
print("Column 3:", col3)

Output:

Column 1: [1. 4. 7.]
Column 2: [2. 5. 8.]
Column 3: [3. 6. 9.]

Explanation:

  • The unpack=True option transposes the array and assigns each column to a separate variable.

Additional Notes:

    1. File Formats:
    numpy.loadtxt is ideal for simple text files. For more complex formats like CSVs with headers, consider numpy.genfromtxt or pandas.read_csv.

    2. Error Handling:
    If the file contains non-numerical values or malformed rows, it may raise an error. Use numpy.genfromtxt for more flexibility.

Practical Guides to NumPy Snippets and Examples.



Follow us on Facebook and Twitter for latest update.