I've tried to call predict function of nearest neighbor and got the following error:
AttributeError: 'NearestNeighbors' object has no attribute 'predict'
from sklearn.neighbors import NearestNeighbors
samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
neigh = NearestNeighbors()
neigh.predict([[1., 1., 1.]]) # this cause error
Your are confusing the
NearestNeighbors class and the
KNeighborsClassifier class. Only the second one has the
Note the example from the link you posted:
X = [, , , ] y = [0, 0, 1, 1] from sklearn.neighbors import KNeighborsClassifier neigh = KNeighborsClassifier(n_neighbors=3) neigh.fit(X, y) print(neigh.predict([[1.1]])) print(neigh.predict_proba([[0.9]]))
NearestNeighbors class is unsupervised and can not be used for classification but only for nearest neighbour searches.