Python及Numpy
和MatPlotLib
的新手。
我正在尝试从各种数据类型的2D
创建一个Numpy
CSV
数组,但我会将它们全部视为字符串。杀手是我需要能够使用tuple
索引访问它们,例如:[:,5]
以获取第5列,或[5]
获取第5行。
有没有办法做到这一点?
由于内存访问计算,这似乎是Numpy
的限制:
dataSet = np.loadtxt(open("adult.data.csv", "rb"), delimiter=" ,")
print dataSet[:, 4] <---results in IndexError: Invalid Index
我还尝试了loadfromgen
,dtype = str
和dtype = "a16"
以及dtype = object
。什么都行不通。我可以加载数据,也没有列访问权限,或者我根本无法加载数据。
答案 0 :(得分:1)
从评论行模拟你的文件 - 多次复制(即每行一个字符串):
In [8]: txt = b" 39, State-gov, 77516, Bachelors, 13, Never-married, Adm-clerical, Not-in-family, White, Male, 2174, 0, 40, United-States, <=50K"
In [9]: txt = [txt for _ in range(5)]
In [10]: txt
Out[10]:
[b' 39, State-gov, 77516, Bachelors, 13, Never-married, Adm-clerical, Not-in-family, White, Male, 2174, 0, 40, United-States, <=50K',
b' 39, State-gov, 77516, Bachelors, 13, Never-married, Adm-clerical, Not-in-family, White, Male, 2174, 0, 40, United-States, <=50K',
b' 39, State-gov, 77516, Bachelors, 13, Never-married, Adm-clerical, Not-in-family, White, Male, 2174, 0, 40, United-States, <=50K',
b' 39, State-gov, 77516, Bachelors, 13, Never-married, Adm-clerical, Not-in-family, White, Male, 2174, 0, 40, United-States, <=50K',
b' 39, State-gov, 77516, Bachelors, 13, Never-married, Adm-clerical, Not-in-family, White, Male, 2174, 0, 40, United-States, <=50K']
使用genfromtxt
加载分隔符。让它选择每列最佳的dtype:
In [12]: A=np.genfromtxt(txt, delimiter=',',dtype=None)
In [13]: A
Out[13]:
array([ (39, b' State-gov', 77516, b' Bachelors', 13, b' Never-married', b' Adm-clerical', b' Not-in-family', b' White', b' Male', 2174, 0, 40, b' United-States', b' <=50K'),
(39, b' State-gov', 77516, b' Bachelors', 13, b' Never-married', b' Adm-clerical', b' Not-in-family', b' White', b' Male', 2174, 0, 40, b' United-States', b' <=50K'),...],
dtype=[('f0', '<i4'), ('f1', 'S10'), ('f2', '<i4'), ('f3', 'S10'), ('f4', '<i4'), ('f5', 'S14'), ('f6', 'S13'), ('f7', 'S14'), ('f8', 'S6'), ('f9', 'S5'), ('f10', '<i4'), ('f11', '<i4'), ('f12', '<i4'), ('f13', 'S14'), ('f14', 'S6')])
带有复合dtype的5元素数组
In [14]: A.shape
Out[14]: (5,)
In [15]: A.dtype
Out[15]: dtype([('f0', '<i4'), ('f1', 'S10'), ('f2', '<i4'),
('f3', 'S10'), ('f4', '<i4'), ....])
使用字段名称(非列号)访问“列”
In [16]: A['f4']
Out[16]: array([13, 13, 13, 13, 13])
或者加载为dtype = str:
In [17]: A=np.genfromtxt(txt, delimiter=',',dtype=str)
In [18]: A
Out[18]:
array([['39', ' State-gov', ' 77516', ' Bachelors', ' 13',
' Never-married', ' Adm-clerical', ' Not-in-family', ' White',
' Male', ' 2174', ' 0', ' 40', ' United-States', ' <=50K'],
...
' Male', ' 2174', ' 0', ' 40', ' United-States', ' <=50K']],
dtype='<U14')
In [19]: A.dtype
Out[19]: dtype('<U14')
In [20]: A.shape
Out[20]: (5, 15)
In [21]: A[:,4]
Out[21]:
array([' 13', ' 13', ' 13', ' 13', ' 13'],
dtype='<U14')
现在它是15列2d数组,可以用列号索引。
使用错误的分隔符,每行加载一列
In [24]: A=np.genfromtxt(txt, delimiter=' ,',dtype=str)
In [25]: A
Out[25]:
array([ '39, State-gov, 77516, Bachelors, 13, Never-married, Adm-clerical, Not-in-family, White, Male, 2174, 0, 40, United-States, <=50K',
...],
dtype='<U127')
In [26]: A.shape
Out[26]: (5,)
带有长字符串dtype的1d数组。
CSV文件可能以各种方式加载,有些是有意的,有些则不是。您必须查看结果,并在盲目地尝试索引列之前尝试理解它们。