k = 9
# importing NearestNeighbors class
from sklearn.neighbors import NearestNeighbors
# initialize model
neigh = NearestNeighbors(n_neighbors=k, n_jobs=-1)
# train for getting nearest neighbour
neigh.fit(X)
dist, ind = neigh.kneighbors(X)
# We have no use of indices here
# dist is a 2 dimensional array of shape (10000, 9) in which each row is list of length 9. This row contain distances to all 9 nearest points. But we need distance to only 9th nearest point. So
distances = [dist[i][k - 1] for i in range(len(dist))]
好的,我用sklearn库找到解决方案,最近邻使用下面的代码
相关问题 更多 >
编程相关推荐