use*_*551 6 python machine-learning scikit-learn
我试图调用最近邻居的预测函数并得到以下错误:
AttributeError: 'NearestNeighbors' object has no attribute 'predict'
Run Code Online (Sandbox Code Playgroud)
代码是:
from sklearn.neighbors import NearestNeighbors
samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
neigh = NearestNeighbors()
neigh.fit(samples)
neigh.predict([[1., 1., 1.]]) # this cause error
Run Code Online (Sandbox Code Playgroud)
我已经阅读了文档,它有预测功能:http: //scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html
怎么做预测?
你的NearestNeighbors班级和KNeighborsClassifier班级令人困惑.只有第二个具有该predict功能.
请注意您发布的链接中的示例:
X = [[0], [1], [2], [3]]
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]]))
Run Code Online (Sandbox Code Playgroud)
本NearestNeighbors类是无监督,不能用于分类,但只针对最近邻搜索.