【发布时间】:2014-06-26 02:46:28
【问题描述】:
我有一个文本文件data.txt,其中包含:
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
5.8,2.7,4.1,1.0,Iris-versicolor
6.2,2.2,4.5,1.5,Iris-versicolor
6.4,3.1,5.5,1.8,Iris-virginica
6.0,3.0,4.8,1.8,Iris-virginica
如何使用numpy.loadtxt() 加载这些数据,以便在加载后得到一个NumPy 数组,例如[['5.1' '3.5' '1.4' '0.2' 'Iris-setosa'] ['4.9' '3.0' '1.4' '0.2' 'Iris-setosa'] ...]?
我试过了
np.loadtxt(open("data.txt"), 'r',
dtype={
'names': (
'sepal length', 'sepal width', 'petal length',
'petal width', 'label'),
'formats': (
np.float, np.float, np.float, np.float, np.str)},
delimiter= ',', skiprows=0)
【问题讨论】:
标签: python python-2.7 python-3.x numpy