TypeError:期望的序列或类似数组的估计量KNeighborsClassifier

时间:2019-02-16 21:53:30

标签: python scikit-learn knn nearest-neighbor

我正在尝试使用scikit-learn实现K-NN算法。 这是我的代码的样子:

X = df.drop(columns=['Purchased'])
y = df['Purchased'].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, 
test_size=0.3,stratify=y)
from sklearn.preprocessing import StandardScaler  
scaler = StandardScaler()  
scaler.fit(X_train)
X_train = scaler.transform(X_train)  
X_test = scaler.transform(X_test)  
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors = 3)
knn.fit(X_train,y_train)
knn.predict(X_test)
knn.score(X_test, y_test)
from sklearn.metrics import classification_report, confusion_matrix  
print(confusion_matrix(y_test, knn))  
print(classification_report(y_test, knn)) 

运行最后两个输出后,我得到以下消息:

TypeError: Expected sequence or array-like, got estimator KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
           metric_params=None, n_jobs=None, n_neighbors=3, p=2,
           weights='uniform')

有人知道是什么问题吗? 谢谢!

2 个答案:

答案 0 :(得分:1)

print(confusion_matrix(y_test, knn.predict(X_test))
print(classification_report(y_test, knn.predict(X_test))

在这种情况下,knn是一个类的对象,未实现 str 。因此,您必须用重新存储knn.predict来替换它。

答案 1 :(得分:0)

如错误提示所示,confusion_matrix函数需要一个序列或类似数组的对象(例如,列表),而是获得一个KNeighborsClassifier对象,在您的情况下为{{1} }。

您可以通过保存knn函数的输出来解决此问题,如下所示:

predict

然后您可以在下一行中将此变量用作:

my_result = knn.predict(X_test)