Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

python - Difference Between numpy.genfromtxt and numpy.loadtxt, and Unpack

I am curious to know the difference between the two functions alluded to in the title of this thread. From the website containing the documentation, it says, "numpy.loadtxt [is] [an] equivalent function when no data is missing." What exactly is meant by this? Does this mean, for instance, if I have a csv file that has a blank column between two columns containing data, I should not numpy.loadtxt.

Also, what does this mean,

"unpack : bool, optional
If True, the returned array is transposed, so that arguments may be unpacked using x, y, z = loadtxt(...)"

I am not quite certain as to what this means.

I'd appreciate your help, thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are correct. Using np.genfromtxt gives you some options like the parameters missing_values, filling_values that can help you dealing with an incomplete csv. Example:

1,2,,,5
6,,8,,
11,,,,

Could be read with:

filling_values = (111, 222, 333, 444, 555) # one for each column
np.genfromtxt(filename, delimiter=',', filling_values=filling_values) 
#array([[   1.,    2.,  333.,  444.,    5.],
#       [   6.,  222.,    8.,  444.,  555.],
#       [  11.,  222.,  333.,  444.,  555.]])

The parameter unpack is useful when you want to put each column of the text file in a different variable. Example, you have the text file with columns x, y, z, then:

x, y, z = np.loadtxt(filename, unpack=True)

Note that this works the same as

x, y, z = np.loadtxt(filename).T

By default iterating over a 2-D array means iterating over the lines, that's why you have to transpose or use unpack=True in this example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...