如果您有非常大的稀疏数据集,无法在非稀疏格式中保存在内存中,我建议尝试一下这个基于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] ])
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)
lsh.query(X_sim, num_results=1)
如果你确定只想使用MinHash,可以尝试使用https://github.com/go2starr/lshhdc,但我个人还没有测试过它与稀疏矩阵的兼容性。