我正在尝试使用带有以下代码的KNeighborsClassifier进行KNN-
X_train, X_test, y_train, y_test = train_test_split(X_bow, y, test_size=0.30, random_state=42)
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X_train, y_train)
但是我收到以下错误
ValueError: Unknown label type: 'unknown'
X_train的类型为scipy.sparse.csr.csr_matrix,y_train的类型为numpy.ndarray。
这是我得到的详细错误。为什么会出现此错误?
ValueError Traceback (most recent call last)
<ipython-input-278-97b47c930597> in <module>
10
11 neigh = KNeighborsClassifier(n_neighbors=3)
---> 12 neigh.fit(X_train, y_train)
c:\users\kishore\appdata\local\programs\python\python36\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:\users\kishore\appdata\local\programs\python\python36\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'
编辑1:
我的Y是-array([0,1,0,...,1,1,1],dtype = object)
我的X是类型为的<5600x6031稀疏矩阵 带有压缩稀疏行格式的586188个存储元素>
答案 0 :(得分:1)
因此,问题在于您的y是object类型,而sklearn无法识别。
在将变量传递到分类器之前,可以使用y=y.astype('int')