2017-12-06 6 views
-3

오류 파이썬를 반환하고 나는이 기능을 사용할 때 날이 오류를 반환 .predict .predict :Sklearn : 나는 핍과 sklearn을 설치

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])) 

은 무엇입니까 이걸로 잘못 됐어?

+2

오류 메시지를 읽고 그들이 말하는 것을 시도 했습니까? – WNG

답변

1

문제는 당신이 [[150,]가이 같은 문제가있을 수 없습니다 전달할 경우, 하나의 샘플을 전달하는 것입니다 :

print(clf.predict([[150, 0]])) 
1

힌트 오류 메시지에 " ... 그것이 하나의 샘플을 포함한다면 ... " 일반적으로 예측할 샘플 목록은 일반적으로 하나가 아니라 예상되는 것입니다. predict에 전달 된 데이터는 fit에 전달 된 features과 동일한 형식, 즉 2 차원 배열이어야합니다. 시도해보십시오.

clf.predict([[150, 0]])