algorithm - sklearn k最近邻居问题

标签 algorithm python-2.7 duplicates scikit-learn nearest-neighbor

我想知道是否有一种方法可以强制 sklearn NearestNeighbors 算法,以在存在重复点时考虑输入数组中点的顺序。

举例说明:

>>> 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]])

最佳答案

我认为你不能这样做,因为从 ref我们得到了:

Warning: Regarding the Nearest Neighbors algorithms, if two neighbors, neighbor k+1 and k, have identical distances but different labels, the results will depend on the ordering of the training data.

关于algorithm - sklearn k最近邻居问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37229407/

相关文章:

算法及时计算但快速验证计算结果

algorithm - 用于查找严格子集的快速数据结构(从给定列表中)

python - 模拟 CSV DictReader

java - 从字符串中删除重复的字符

python - 如何在 Pandas : count, 删除列、删除重复项中更好地执行此步骤

sql - 修复 SQL 中的重复客户

java - 如何计算卢卡斯数?

python - 使用 "import dateutil"和 "dateutil.parser.parse()"时出现 AttributeError 但使用 "from dateutil import parser"时没有问题

python-2.7 - 修改列表副本,修改原始列表

algorithm - 选择一对不重叠的回文子串的方法数