SkLearn estimator error in NearestNeighbors

时间:2016-03-04 18:11:14

标签: python scikit-learn

Here is my code:

for k in range(1, 50):
  neigh = NearestNeighbors(n_neighbors=k)
  neigh.fit(data, dataClass) 
  a = sklearn.cross_validation.cross_val_score(neigh, data, y=dataClass, cv=kf)

Error

If no scoring is specified, the estimator passed should have a 'score' method. The estimator NearestNeighbors(algorithm='auto', leaf_size=30, metric='minkowski',
     metric_params=None, n_jobs=1, n_neighbors=1, p=2, radius=1.0) does not.

If I try to pass parameter scoring='accuracy' it gives me the following error:

  'NearestNeighbors' object has no attribute 'predict'

What do I do? Documentaition says NearestNeighbors have .score and .predict.

1 个答案:

答案 0 :(得分:1)

NearestNeighbors只会给你邻居,我想你需要尝试KNeighborsClassifier。试试这个:

from sklearn import neighbors, cross_validation

for k in range(1, 50):
  neigh = neighbors.KNeighborsClassifier(n_neighbors=k)
  neigh.fit(data, dataClass) 
  a = cross_validation.cross_val_score(neigh, data, y=dataClass, cv=kf)