KNeighborsClassifier score中的奇怪值错误



我想绘制 K 最近邻分类器的学习曲线。我有以下代码:

X_train = #training data
Y_train = #target variables
best_neighbors = #number of neighbors which gave highest score (3)
idx = len(X_train)/5000
scores = pd.DataFrame(np.zeros((idx+1, 2)), index=np.arange(1, len(X_train), 5000), columns=['Train Score', 'CV Score'])
for i in range(1, len(X_train), 5000):
    X_train_set = X_train[:i]
    Y_train_set = Y_train[:i]
    neigh = KNeighborsClassifier(n_neighbors = best_neigbors)
    neigh.fit(X_train_set, Y_train_set)
    train_score = neigh.score(X_train, Y_train)
    cv_score = neigh.score(X_test, Y_test)
    scores['Train Score'][i] = train_score
    scores['CV Score'][i] = cv_score

这段代码以前在决策树或随机森林中完美运行,但在这里我得到以下奇怪的错误:

      ValueError                                Traceback (most recent call last)
<ipython-input-6-95e645e75971> in <module>()
     10     neigh.fit(X_train_set, Y_train_set)
     11 
---> 12     train_score = neigh.score(X_train, Y_train)
     13     cv_score = neigh.score(X_test, Y_test)
     14 
//anaconda/lib/python2.7/site-packages/sklearn/base.pyc in score(self, X, y, sample_weight)
    289         """
    290         from .metrics import accuracy_score
--> 291         return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
    292 
    293 
//anaconda/lib/python2.7/site-packages/sklearn/neighbors/classification.pyc in predict(self, X)
    145         X = atleast2d_or_csr(X)
    146 
--> 147         neigh_dist, neigh_ind = self.kneighbors(X)
    148 
    149         classes_ = self.classes_
//anaconda/lib/python2.7/site-packages/sklearn/neighbors/base.pyc in kneighbors(self, X, n_neighbors, return_distance)
    316                                           **self.effective_metric_params_)
    317 
--> 318             neigh_ind = argpartition(dist, n_neighbors - 1, axis=1)
    319             neigh_ind = neigh_ind[:, :n_neighbors]
    320             # argpartition doesn't guarantee sorted order, so we sort again
//anaconda/lib/python2.7/site-packages/numpy/core/fromnumeric.pyc in argpartition(a, kth, axis, kind, order)
    689     except AttributeError:
    690         return _wrapit(a, 'argpartition',kth, axis, kind, order)
--> 691     return argpartition(kth, axis, kind=kind, order=order)
    692 
    693 
ValueError: kth(=2) out of bounds (1)

知道这意味着什么以及如何解决这个问题吗?

编辑:将scikit-learn更新到0.16版本后,我收到以下错误:

ValueError                                Traceback (most recent call last)
<ipython-input-66-21f434a289fc> in <module>()
     10     neigh.fit(X_train_set, Y_train_set)
     11 
---> 12     train_score = neigh.score(X_train, Y_train)
     13     cv_score = neigh.score(X_test, Y_test)
     14 
//anaconda/lib/python2.7/site-packages/sklearn/base.pyc in score(self, X, y, sample_weight)
    293         """
    294         from .metrics import accuracy_score
--> 295         return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
    296 
    297 
//anaconda/lib/python2.7/site-packages/sklearn/neighbors/classification.pyc in predict(self, X)
    136         X = check_array(X, accept_sparse='csr')
    137 
--> 138         neigh_dist, neigh_ind = self.kneighbors(X)
    139 
    140         classes_ = self.classes_
//anaconda/lib/python2.7/site-packages/sklearn/neighbors/base.pyc in kneighbors(self, X, n_neighbors, return_distance)
    337             raise ValueError(
    338                 "Expected n_neighbors <= %d. Got %d" %
--> 339                 (train_size, n_neighbors)
    340             )
    341         n_samples, _ = X.shape
ValueError: Expected n_neighbors <= 1. Got 3

您正在尝试拟合只有一个数据点的 3 个最近邻分类器。那行不通。顺便说一下,scikit-learn中有用于学习曲线和验证曲线的函数。

相关内容

  • 没有找到相关文章

最新更新