【发布时间】:2017-05-08 13:45:45
【问题描述】:
我是 scikit learn 的新手,我正在尝试训练一个分类器来预测在给定特定输入的情况下最有可能使用哪种类型的汽车:
我的数据如下所示:
18.0 8 307.0 130.0 3504.12.0 70 1 雪佛兰
15.0 8 350.0 165.0 3693. 11.5 70 1 别克
18.0 8 318.0 150.0 3436. 11.0 70 1 普利茅斯
17.0 8 302.0 140.0 3449. 10.5 70 1 福特都灵
15.0 8 429.0 198.0 4341. 10.0 70 1 福特银河500
14.0 8 454.0 220.0 4354.9.0 70 1 雪佛兰黑斑羚
14.0 8 440.0 215.0 4312. 8.5 70 1 普利茅斯之怒 iii
每列数据都是汽车的特定特征:mpg、气缸、马力、加速度等。
我以数字形式表示汽车:
cars = [0, 1, 2, 3, 3, 0, 2]
其中 0 = 雪佛兰,1 = 别克等。
这是我的程序代码:
data = np.loadtxt("my_data")
mpg = data[:,0]
cylinders = data[:,1]
displacement = data[:,2]
horsepower = data[:,3]
weight = data[:,4]
acceleration = data[:,5]
modelyear = data[:,6]
origin = data[:,7]
X = [mpg, cylinders, displacement, horsepower, weight,
acceleration, acceleration, modelyear, origin]
car_type = [1, 2, 3, 2, 6, 1, 0, 2, 5, 4, 2, 0, 3, 3, 2, 1, 0]
clf = tree.DecisionTreeClassifier()
clf.fit(X, car_type)
但是当我尝试运行它时,我得到了这个错误:
Traceback (most recent call last):
File "scikitlearn_practice.py", line 21, in <module>
clf.fit(X, car_type)
File "/Library/Python/2.7/site-packages/sklearn/tree/tree.py",
line 739, in fit
X_idx_sorted=X_idx_sorted)
File "/Library/Python/2.7/site-packages/sklearn/tree/tree.py", line
240, in fit
"number of samples=%d" % (len(y), n_samples))
ValueError: Number of labels=17 does not match number of samples=8
如何修复此错误以使标签与样本数量匹配?
谢谢
【问题讨论】:
标签: python machine-learning scikit-learn