来自LSHForest的例子,结果不相信

时间:2015-06-24 00:16:59

标签: nearest-neighbor locality-sensitive-hash

图书馆和相应的文档如下 - 是的,我阅读了所有内容,并能够“运行”我自己的代码。

http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LSHForest.html

但结果对我来说并没有多大意义,所以我通过了这个例子(也包含在以前的网页中)

    >>> from sklearn.neighbors import LSHForest
    >>> X_train = [[5, 5, 2], [21, 5, 5], [1, 1, 1], [8, 9, 1], [6, 10, 2]]
    >>> X_test = [[9, 1, 6], [3, 1, 10], [7, 10, 3]]
    >>> lshf = LSHForest()
    >>> lshf.fit(X_train)  
    LSHForest(min_hash_match=4, n_candidates=50, n_estimators=10,
              n_neighbors=5, radius=1.0, radius_cutoff_ratio=0.9,
              random_state=None)
    >>> distances, indices = lshf.kneighbors(X_test, n_neighbors=2)
    >>> distances                                        
        array([[ 0.069...,  0.149...],
               [ 0.229...,  0.481...],
               [ 0.004...,  0.014...]])
    >>> indices
        array([[1, 2],
               [2, 0],
               [4, 0]])

所以我试着通过找到三个测试集[9,1,6],[3,1,10],[7,10,3]的最近邻居来验证这个例子

比较搜索最近的邻居[9,1,6](通过使用欧几里德距离),最近的训练点是[5,5,2]和[6,10,2](我认为指数会[ 0.4]) - 这与结果[1,2]

显着不同

通过简单的数学计算,距离也完全脱离主题,我的Excel工作表是attached

再次感谢您的时间和帮助

1 个答案:

答案 0 :(得分:1)

没错,因为LSHForest实现了ANN(近似近邻),也许这就是我们需要考虑的差异。 ANN结果不是最近的邻居,而是最近邻居的近似值。

例如,2最近邻居结果如下:

from sklearn.neighbors import NearestNeighbors

X_train = [[5, 5, 2], [21, 5, 5], [1, 1, 1], [8, 9, 1], [6, 10, 2]]
X_test = [[9, 1, 6], [3, 1, 10], [7, 10, 3]]

nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X_train)
distances, indices = nbrs.kneighbors(X_test)

并返回

indices
Out[2]: 
array([[0, 2],
       [0, 2],
       [4, 3]], dtype=int64)

distances
Out[3]: 
array([[ 6.92820323,  9.43398113],
       [ 9.16515139,  9.21954446],
       [ 1.41421356,  2.44948974]])

如果有帮助,请结帐this并注意提及:

  

给定一个查询点q,如果距离q在距离r内存在一个点,则它报告距离q的距离为q的点。这里c是算法的近似因子。

距离'r'处的点和返回的点不必相同。

希望这有帮助。