Python NumPy loadtxt 数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19767190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:33:17  来源:igfitidea点击:

NumPy loadtxt data type

pythonnumpy

提问by Anastasia

I am trying to load a data set that looks like this:

我正在尝试加载一个如下所示的数据集:

Algeria,73.131000,6406.8166213983,0.1
Angola,51.093000,5519.1831786593,2
Argentina,75.901000,15741.0457726686,0.5
Armenia,74.241000,4748.9285847709,0.1

etc. At the end, I will need only columns 1 and 2. I won't need country names and the last column. Essentially, I need to extract two matrices with dimensions nx1. I know that I need to specify the data type:

等等。最后,我只需要第 1 列和第 2 列。我不需要国家/地区名称和最后一列。本质上,我需要提取两个维度为 nx1 的矩阵。我知道我需要指定数据类型:

data=np.loadtxt('file.txt',delimiter=',',dtype=[('f0',str),('f1',float),('f2',float),('f3',float)])

However, this produces a list of tuples,

然而,这会产生一个元组列表,

array([('', 73.131, 6406.8166213983, 0.1),
   ('', 51.093, 5519.1831786593, 2.0),`

instead of

代替

array(['',73.131,6406.8166213983,0.1],
      ['',51.093, 5519.1831786593, 2.0],

Where is the mistake?

错误在哪里?

采纳答案by atomh33ls

If you just want the first two columns you could use genfromtxt:

如果你只想要前两列,你可以使用genfromtxt

import numpy as np
col1 = np.genfromtxt('yourfile.txt',usecols=(1),delimiter=',',dtype=None)
col2 = np.genfromtxt('yourfile.txt',usecols=(2),delimiter=',',dtype=None)

or both together:

或两者兼而有之:

np.genfromtxt('yourfile.txt',usecols=(1,2),delimiter=',',dtype=None)

回答by mamalos

Your "mistake" is that you set your own dtype. If you don't want the dtype you've set (where I see no reason why you wouldn't want it), you can use skiprows and usecols parameters of np.loadtxt() to ONLY load the columns you wish.

您的“错误”是您设置了自己的 dtype。如果您不想要您设置的 dtype(我看不出您为什么不想要它),您可以使用 np.loadtxt() 的 skiprows 和 usecols 参数来仅加载您希望的列。

Your result will be a NumPy array with a shape of (n, 2), not (n, 3) that you thought you'd have (where n is your number of rows).

您的结果将是一个形状为 (n, 2) 的 NumPy 数组,而不是您认为应该拥有的 (n, 3)(其中 n 是您的行数)。

回答by moenad

Check NumPy'sdocumentation.

检查NumPy 的文档。

x, y = np.loadtxt(c, delimiter=',', usecols=(1, 2), unpack=True)

The usecolsparameter should get your job done.

usecols参数应该可以完成您的工作。