我正在进行多类音频分类,
1)指向记录单个单词的每个.wav文件的路径 2)每个路径的MFCC向量 3)训练集中每个路径的标签(实际单词)
显然,我的算法无法识别任何邻居。任何原因?
我的场景如下:
X_train, X_test, y_train, y_test = train_test_split(new_X, y, test_size=0.4, random_state=5)
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
(56894,99) (56894,) (37930,99) (37930,)
我的模特是:
k_range = list(range(1))
scores = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
scores.append(metrics.accuracy_score(y_test, y_pred))
然后错误提示:
ValueError Traceback (most recent call last)
<ipython-input-12-d67236a4ac61> in <module>()
3 for k in k_range:
4 knn = KNeighborsClassifier(n_neighbors=k)
----> 5 knn.fit(X_train, y_train)
6 y_pred = knn.predict(X_test)
7 scores.append(metrics.accuracy_score(y_test, y_pred))
1 frames
/usr/local/lib/python3.6/dist-packages/sklearn/neighbors/base.py in fit(self, X, y)
915 self._y = self._y.ravel()
916
--> 917 return self._fit(X)
918
919
/usr/local/lib/python3.6/dist-packages/sklearn/neighbors/base.py in _fit(self, X)
266 raise ValueError(
267 "Expected n_neighbors > 0. Got %d" %
--> 268 self.n_neighbors
269 )
270 else:
ValueError: Expected n_neighbors > 0. Got 0
答案 0 :(得分:2)
这并不是无法识别邻居,而是在抱怨您正在使用n_neighbours = 0
您的循环使用k_range
,即[0,1]
,因此在循环的第一次迭代中,失败的调用结果为:
knn = KNeighborsClassifier(n_neighbors=0)
您需要在以下范围内更改范围:
k_range = list(range(1))
不包括零。