【发布时间】:2018-05-20 17:27:53
【问题描述】:
我用 pip 安装了 sklearn,当我使用 .predict 函数时,它返回了这个错误:
Traceback (most recent call last):
File "C:/Users/Roberto/PycharmProjects/AI projects/New.py", line 8, in <module>
print(clf.predict([150, 0]))
File "C:\Users\Roberto\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sklearn\tree\tree.py", line 412, in predict
X = self._validate_X_predict(X, check_input)
File "C:\Users\Roberto\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\sklearn\tree\tree.py", line 373, in _validate_X_predict
X = check_array(X, dtype=DTYPE, accept_sparse="csr")
File "C:\Users\Roberto\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\sklearn\utils\validation.py", line 441, in check_array
"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[ 150. 0.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
我的代码是:
from sklearn import tree
features = [[140, 1], [130, 1], [150, 0], [170, 0]]
labels = [0, 0, 1, 1]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(features, labels)
print(clf.predict([150, 0]))
这是怎么回事?
【问题讨论】:
-
您是否阅读了错误消息并尝试按照他们告诉您的操作?
标签: python scikit-learn