numpy.genfromtxt和numpy.loadtxt之间的区别,以及Unpack [英] Difference Between numpy.genfromtxt and numpy.loadtxt, and Unpack
问题描述
我对Python非常陌生 - 实际上,对于编程来说一般都是新手,尽管我恐怕我不能永远使用这个借口 - 并且很想知道标题中提到的两个函数之间的区别这个线程。从包含文档的网站上,它说:当没有数据丢失时,numpy.loadtxt [是] [等同]功能。究竟是什么意思?这是否意味着,例如,如果我有一个csv文件,其中包含数据的两列之间有一个空白列,我不应该numpy.loadtxt。
I am rather new to python--actually, new to programming in general, though I am afraid I can't use that excuse forever--, and 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!
推荐答案
你是对的。使用 np.genfromtxt
给出一些选项,如参数 missing_values
, filling_values
可以帮助你处理一个不完整的 csv
。例如:
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.]])
参数 unpack
当你想将文本文件的每一列放入一个不同的变量时非常有用。例如,您的文本文件中包含 x,y,z
列,则:
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)
这与
x, y, z = np.loadtxt(filename).T
默认情况下,遍历二维数组意味着迭代行,这就是为什么您必须转置或使用 unpack = True
在这个例子中。
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.
这篇关于numpy.genfromtxt和numpy.loadtxt之间的区别,以及Unpack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!