【问题标题】:SKlearn: KDTree how to return nearest neighbour based on threshold (Python)SKlearn:KDTree如何根据阈值返回最近邻(Python)
【发布时间】:2018-09-13 23:09:28
【问题描述】:

我有一个包含 300 个图像的数据库,我为每个图像提取了一个 BOVW。从查询图像(从同一字典中提取 query_BOVW)开始,我需要在我的训练数据集中找到相似的图像。

我在我的训练集kd_tree = KDTree(training) 上使用了 Sklearn KDTree,然后我用kd_tree.query(query_vector) 计算了与查询向量的距离。最后一个函数将要返回的最近邻居的数量作为第二个参数,但我寻求的是为欧几里得距离设置阈值,并基于此阈值有不同数量的最近邻居。

我查看了文档,但没有找到任何相关信息。我是否错误地寻找可能没有意义的东西?

感谢您的帮助。

【问题讨论】:

    标签: python tree scikit-learn threshold


    【解决方案1】:

    documentation,可以使用query_radius的方法:

    查询给定半径内的邻居:

    import numpy as np
    np.random.seed(0)
    X = np.random.random((10, 3))  # 10 points in 3 dimensions
    tree = KDTree(X, leaf_size=2)     
    print(tree.query_radius(X[0], r=0.3, count_only=True))
    ind = tree.query_radius(X[0], r=0.3) # indices of neighbors within distance 0.3 
    

    这适用于 sklearn 19.1 版

    【讨论】:

      【解决方案2】:

      你想在这里使用query_radius

      query_radius(self, X, r, count_only = False):

      在树中查询半径 r 内的邻居

      ...

      只是上面链接中的示例:

      import numpy as np
      np.random.seed(0)
      X = np.random.random((10, 3))  # 10 points in 3 dimensions
      tree = BinaryTree(X, leaf_size=2)     
      print(tree.query_radius(X[0], r=0.3, count_only=True))
      
      ind = tree.query_radius(X[0], r=0.3)  
      print(ind)  # indices of neighbors within distance 0.3
      

      【讨论】:

        猜你喜欢
        • 2018-04-04
        • 1970-01-01
        • 1970-01-01
        • 2019-04-02
        • 1970-01-01
        • 2018-04-11
        • 2016-10-08
        • 2017-12-11
        • 1970-01-01
        相关资源
        最近更新 更多