utd*_*ant 10 python numpy scipy locality-sensitive-hash
我有一个大的稀疏numpy/scipy矩阵,其中每一行对应于高维空间中的一个点.我想要进行以下类型的查询:
给定一个点P(矩阵中的行)和距离小量,找到距离的所有点,最小量从P.
我使用的距离度量是Jaccard相似度,因此应该可以使用MinHash等Locality Sensitive Hashing技巧.
在某处(我似乎无法找到一个)稀疏numpy数组是否有MinHash的实现,或者有一种简单的方法可以做到这一点?
我不仅仅是为Github上的非稀疏数组提取内容的原因是scipy中的稀疏数据结构可能会导致时间复杂度的爆炸.
如果你有非常大的稀疏数据集太大而不能以非稀疏格式保存在内存中,我会尝试围绕Scipy的CSR稀疏矩阵假设构建的这个LSH实现:
https://github.com/brandonrobertz/SparseLSH
如果你不能将表放在内存中,它还支持对像LevelDB这样的基于磁盘的键值存储的支持.来自文档:
from sparselsh import LSH
from scipy.sparse import csr_matrix
X = csr_matrix( [
[ 3, 0, 0, 0, 0, 0, -1],
[ 0, 1, 0, 0, 0, 0, 1],
[ 1, 1, 1, 1, 1, 1, 1] ])
# One class number for each input point
y = [ 0, 3, 10]
X_sim = csr_matrix( [ [ 1, 1, 1, 1, 1, 1, 0]])
lsh = LSH( 4,
X.shape[1],
num_hashtables=1,
storage_config={"dict":None})
for ix in xrange(X.shape[0]):
x = X.getrow(ix)
c = y[ix]
lsh.index( x, extra_data=c)
# find points similar to X_sim
lsh.query(X_sim, num_results=1)
Run Code Online (Sandbox Code Playgroud)
如果你肯定只想使用MinHash,你可以尝试https://github.com/go2starr/lshhdc,但我没有亲自测试那个与稀疏矩阵的兼容性.