19

我正在尝试寻找可以用于我的工作的 minhash 开源实现。

我需要的功能非常简单,给定一个集合作为输入,实现应该返回它的 minhash。

最好使用 python 或 C 实现,以防万一我需要破解它来为我工作。

任何指针都会有很大帮助。

问候。

4

4 回答 4

13

You should have a look at the following open source libraries, in order. All of them are in Python, and show how you can calculate document similarity using LSH/MinHash:

lsh
LSHHDC : Locality-Sensitive Hashing based High Dimensional Clustering
MinHash

于 2013-05-10T21:07:13.110 回答
12

看看数据草图库。它支持序列化和合并。它是在纯python中实现的,没有外部依赖。Go 版本具有完全相同的功能。

于 2015-04-04T13:03:07.650 回答
10

如果您有兴趣研究 minhash 算法,这里有一个非常简单的实现和一些讨论。

为了为集合生成 MinHash 签名,我们创建了一个长度向量,$N$其中所有值都设置为正无穷大。我们还创建$N$了接受输入整数并置换该值的函数。该$i^{th}$函数将单独负责更新$i^{th}$向量中的值。给定这些值,我们可以通过将集合中的每个值传递给每个$N$置换函数来计算集合的 minhash 签名。如果$i^{th}$置换函数的输出低于$i^{th}$向量的值,我们将值替换为置换函数的输出(这就是为什么哈希被称为“ min -hash”)。让我们在 Python 中实现它:

from scipy.spatial.distance import cosine
from random import randint
import numpy as np

# specify the length of each minhash vector
N = 128
max_val = (2**32)-1

# create N tuples that will serve as permutation functions
# these permutation values are used to hash all input sets
perms = [ (randint(0,max_val), randint(0,max_val)) for i in range(N)]

# initialize a sample minhash vector of length N
# each record will be represented by its own vec
vec = [float('inf') for i in range(N)]

def minhash(s, prime=4294967311):
  '''
  Given a set `s`, pass each member of the set through all permutation
  functions, and set the `ith` position of `vec` to the `ith` permutation
  function's output if that output is smaller than `vec[i]`.
  '''
  # initialize a minhash of length N with positive infinity values
  vec = [float('inf') for i in range(N)]

  for val in s:

    # ensure s is composed of integers
    if not isinstance(val, int): val = hash(val)

    # loop over each "permutation function"
    for perm_idx, perm_vals in enumerate(perms):
      a, b = perm_vals

      # pass `val` through the `ith` permutation function
      output = (a * val + b) % prime

      # conditionally update the `ith` value of vec
      if vec[perm_idx] > output:
        vec[perm_idx] = output

  # the returned vector represents the minimum hash of the set s
  return vec

这里的所有都是它的!为了演示我们如何使用这个实现,让我们举一个简单的例子:

import numpy as np

# specify some input sets
data1 = set(['minhash', 'is', 'a', 'probabilistic', 'data', 'structure', 'for',
        'estimating', 'the', 'similarity', 'between', 'datasets'])
data2 = set(['minhash', 'is', 'a', 'probability', 'data', 'structure', 'for',
        'estimating', 'the', 'similarity', 'between', 'documents'])

# get the minhash vectors for each input set
vec1 = minhash(data1)
vec2 = minhash(data2)

# divide both vectors by their max values to scale values {0:1}
vec1 = np.array(vec1) / max(vec1)
vec2 = np.array(vec2) / max(vec2)

# measure the similarity between the vectors using cosine similarity
print( ' * similarity:', 1 - cosine(vec1, vec2) )

这将返回 ~.9 作为这些向量之间相似性的度量。

虽然我们只比较了上面的两个 minhash 向量,但我们可以通过使用“Locality Sensitive Hash”更简单地比较它们。为此,我们可以构建一个字典,将 $W$ MinHash 向量分量的每个序列映射到构造 MinHash 向量的集合的唯一标识符。例如,如果W = 4我们有一个S1从中派生 MinHash 向量的集合[111,512,736,927,817...],我们会将S1标识符添加到该向量中四个 MinHash 值的每个序列中:

d[111-512-736-927].append('S1')
d[512-736-927-817].append('S1')
...

一旦我们对所有集合执行此操作,我们就可以检查字典中的每个键,如果该键具有多个不同的集合 ID,我们有理由相信这些集合是相似的。实际上,一对集合 id 在字典中的单个值中出现的次数越多,这两个集合之间的相似性就越大。以这种方式处理我们的数据,我们可以将识别所有相似集合对的复杂度降低到大致线性时间!

于 2018-09-27T10:14:16.487 回答
2

我会建议你这个库,特别是如果你需要持久性。在这里,您可以使用 redis 存储/检索所有数据。

您可以选择一个 redis 数据库,或者简单地使用内置的内存中 python 字典。

使用 redis 的性能,至少如果 redis 服务器在您的本地计算机上运行,​​几乎与通过标准 python 字典实现的性能相同。

您只需要指定一个配置字典,例如

config = {"redis": {"host": 'localhost', "port": '6739', "db": 0}}

并将其作为参数传递给LSHash类构造函数。

于 2014-03-10T15:08:57.050 回答