Details of the use of loadtxt in numpy

  • 2020-12-07 04:09:13
  • OfStack

There are two functions in numpy that can be used to read files, mainly txt files, and the following describes the usage of these two functions

The first is loadtxt, whose general usage is

numpy.loadtxt(fname, dtype=, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)

Given all the keyword parameters of loadtxt above, here we can explain 11 and give examples

Here we use jupyter notebook to achieve interactive interface operations


%%writefile test.txt #  This is the code to write to the file 
1 2 3 4 
2 3 4 5
3 4 5 6
4 5 6 7

Start with the simplest loadtxt code


import numpy as np
a = np.loadtxt('test.txt')# The most common loadtxt
print(a)

In fact, you just write the file name. All other keyword arguments are default. The output is

[

[[1. 2. 3. 4.]
[2. 3. 4. 5.]
[3. 4. 5. 6.]
[4. 5. 6. 7.]]

]

The reason a is a floating point number is that the default data type of Python is a double precision floating point number


%%writefile test.txt
A B C
1 2 3
4 5 6
7 8 9

a = np.loadtxt('test1.txt', skiprows=1, dtype=int)
print(a)

skiprows here means to skip the first line. If skiprows=2 is set, the first two lines are skipped, and the output here is

[

[[1 2 3]
[4 5 6]
[7 8 9]]

]

%%writefile test.txt
A B C
1 2 3
# AAA
4 5 6
7 8 9

a = np.loadtxt('test2.txt', dtype=int, skiprows=1, comments='#')
print(a)

comment here means that if the line begins with #, the line is skipped, and output is

[

[[1 2 3]
[4 5 6]
[7 8 9]]

]

%%writefile test.txt
A B C
1, 2, 3
# AA AAA
4, 5, 6
7, 8, 9

(a, b) = np.loadtxt('test.txt', dtype=int, skiprows=1, comments='#', delimiter=',', usecols=(0, 2), unpack=True)
print(a, b)

usecols here means to use only columns 0,2, and unpack means to output each column as a vector, rather than merging at 1.

[

[1 4 7] [3 6 9]

]

Finally, we introduce the converters parameter, which is the parameter to preprocess the data. We can define a function first. Here, converters is a dictionary, indicating that the zeroth column uses the function add_one to preprocess the data


def add_one(x):
return int(x)+1# Notice the data structure of the characters used here 
(a, b) = np.loadtxt('test.txt', dtype=int, skiprows=1, converters={0:add_one}, comments='#', delimiter=',', usecols=(0, 2), unpack=True)
print(a, b)

The output result is:

[

[2 5 8] [3 6 9]

]

Add 1 GitHub ES96en-ES97en link...

https://github.com/ChangChunHe/PythonLearning/blob/master/Numpy/8.loadtxt_and_genfromtxt.ipynb


Related articles: