1.6. Nearest Neighbors
sklearn框架
1.6.1. Unsupervised Nearest Neighbors
1.6.1.1. Finding the Nearest Neighbors
from sklearn.neighbors import NearestNeighbors
import numpy as np
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X)
distances, indices = nbrs.kneighbors(X)
indices
distances
输出
>>> indices
array([[0, 1],
[1, 0],
[2, 1]