我正在使用以下python程序来实现基本的决策树分类器。
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
import numpy as np
features = [[140,1],[130,1],[150,0],[170,0]]
labels = [0,0,1,1]
clf = DecisionTreeClassifier()
model = clf.fit(features, labels)
a = model.predict ([160,0])
print (a)
打印出预测值,但会发出警告,
DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and
willraise ValueError in 0.19. Reshape your data either using X.reshape(-1,
1) if your data has a single feature or X.reshape(1, -1) if it contains a
single sample.
我试图用它来修复它,
features = np.array(features).reshape(-1, 2)
labels = np.array(labels).reshape(-1, 1)
但这显示出同样的警告。有什么建议吗?
答案 0 :(得分:3)
问题出在model.predict上。这有效:
a = model.predict ([[160,0]])