【发布时间】:2018-09-27 09:24:06
【问题描述】:
我有一个从文件加载的数据集 -
np.array([(5.1, 3.5, 1.4, 0.2, 'Iris-setosa'),
(4.9, 3., 1.4, 0.2, 'Iris-setosa'),
(4.7, 3.2, 1.3, 0.2, 'Iris-setosa'),
(4.6, 3.1, 1.5, 0.2, 'Iris-setosa'),
(5., 3.6, 1.4, 0.2, 'Iris-setosa')])
如何获得前 4 列的矩阵 -
np.array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3., 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5., 3.6, 1.4, 0.2]])
并将标签放入另一个数组-
np.array(['Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa'])
【问题讨论】:
-
您的输入不是有效的 Python。请提供正确的输入定义。
-
这个数组的
dtype是什么?对于(),我猜它是一个结构化数组,可能由genfromtxt生成。如果是这样,字符串 'column' 很容易通过名称访问,arr['f4']。