我是python的新手,试图运行KNN,但是当我输入代码时,出现错误ValueError:Unknown标签类型:'unknown'。 我已经对所有分类数据进行了编码,并丢弃了我不需要避免的虚拟陷阱。 我还需要做什么来清除此问题?
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import fbeta_score
training_accuracy = []
test_accuracy = []
neighbors_settings = range(1, 11)
for n_neighbors in neighbors_settings:
knn = KNeighborsClassifier(n_neighbors=n_neighbors)
knn.fit(x_train, y_train)
train_pred=knn.predict(x_train)
test_pred=knn.predict(x_test)
training_accuracy.append(fbeta_score(y_train, train_pred, beta=1))
test_accuracy.append(fbeta_score(y_test, test_pred, beta=1))
plt.plot(neighbors_settings, training_accuracy, label="training accuracy")
plt.plot(neighbors_settings, test_accuracy, label="test accuracy")
plt.ylabel("Accuracy")
plt.xlabel("n_neighbors")
plt.legend()
plt.savefig('knn_compare_model')
我希望有一张图可以显示测试和训练的准确性,但是我在下面得到了这个
ValueError Traceback (most recent call last)
<ipython-input-22-8a3a1f3c5c24> in <module>
11 # build the model
12 knn = KNeighborsClassifier(n_neighbors=n_neighbors)
---> 13 knn.fit(x_train, y_train)
>
14
15 # if accuracy of prediction on training set is high but it is low
on test set: So overfitting
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\neighbors\base.py in fit(self, X, y)
903 self.outputs_2d_ = True
904
--> 905 check_classification_targets(y)
906 self.classes_ = []
907 self._y = np.empty(y.shape, dtype=np.int)
>
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in
check_classification_targets(y)
169 if y_type not in ['binary', 'multiclass', 'multiclass- multioutput',
>
170 'multilabel-indicator', 'multilabel-sequences']:
--> 171 raise ValueError("Unknown label type: %r" % y_type)
>
172
173
ValueError: Unknown label type: 'unknown'
答案 0 :(得分:0)
您的y_train
可能是对象类型,可能会导致此错误,因此请添加该行
y_train = y_train.astype('int')
之前
knn.fit(x_train, y_train)
也对您的y_test
做同样的事情。
希望这会有所帮助!