2
我想知道是否有強制sklearn NearestNeighbors算法的方法,以便在存在重複點時考慮輸入數組中某個點的順序。sklearn k最近鄰居問題
爲了說明:
>>> 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
>>>> array([[0, 1],
[1, 0],
[2, 1],
[3, 4],
[4, 3],
[5, 4]])
由於查詢集合中的訓練集相匹配時,每個點的近鄰是點本身,在零的距離。但是,如果我允許在X重複點,算法,可以理解的,不重複的區分:
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1],[3, 2],[-1,-1],[-1,-1]])
nbrs = NearestNeighbors(n_neighbors=2, algorithm='auto').fit(X)
distances, indices = nbrs.kneighbors(X)
indices
>>>> array([[6, 0],
[1, 0],
[2, 1],
[3, 4],
[4, 3],
[5, 4],
[6, 0],
[6, 0]])
理想情況下,我想最後的輸出是這樣的:
>>>> array([[0, 6],
[1, 0],
[2, 1],
[3, 4],
[4, 3],
[5, 4],
[6, 0],
[7, 6]])