记录下minhash计算流程

以下是从教科书截取过来的讲解的非常清晰,记录一下:


Now, let us simulate the algorithm for computing the signature matrix.
Initially, this matrix consists of all ∞’s:


First, we consider row 0 of Fig. 3.4. We see that the values of h1(0) and h2(0) are both 1. The row numbered 0 has 1’s in the columns for sets S1 and S4, so only these columns of the signature matrix can change. As 1 is less than ∞, we do in fact change both values in the columns for S1 and S4. The current estimate of the signature matrix is thus:



Now, we move to the row numbered 1 in Fig. 3.4. This row has 1 only in S3, and its hash values are h1(1) = 2 and h2(1) = 4. Thus, we set SIG(1, 3) to 2 and SIG(2, 3) to 4. All other signature entries remain as they are because their columns have 0 in the row numbered 1. The new signature matrix:






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MinHash是一种用于近似集合相似度计算的技术。下面是一个用Python实现MinHash的示例代码: ```python import numpy as np import hashlib class MinHash: def __init__(self, num_perm): self.num_perm = num_perm self.permutations = self._generate_permutations() def _generate_permutations(self): np.random.seed(0) minhash_permutations = np.random.randint(low=0, high=np.iinfo(np.int64).max, size=(self.num_perm, 2), dtype=np.int64) return minhash_permutations def _hash_value(self, value): return hashlib.sha1(value.encode()).hexdigest() def compute_hash(self, value): hash_value = self._hash_value(value) hash_code = int(hash_value, 16) return hash_code def compute_signature(self, document): signature = np.inf * np.ones(self.num_perm, dtype=np.int64) for word in document.split(): hash_code = self.compute_hash(word) for i in range(self.num_perm): a, b = self.permutations[i] hash_value = (a * hash_code + b) % np.iinfo(np.int64).max signature[i] = min(signature[i], hash_value) return signature def compute_similarity(self, signature1, signature2): return np.mean(signature1 == signature2) # 示例用法 document1 = "This is a document about cats" document2 = "This is a document about dogs" minhash = MinHash(num_perm=128) signature1 = minhash.compute_signature(document1) signature2 = minhash.compute_signature(document2) similarity = minhash.compute_similarity(signature1, signature2) print(f"Similarity between the documents: {similarity}") ``` 在上述示例代码中,我们首先定义了一个MinHash类,它接受参数`num_perm`,表示要使用的哈希函数数量。在初始化时,我们生成了一组随机排列用于哈希计算。 `_hash_value`方法使用SHA1算法对输入值进行哈希计算,并返回哈希值的十六进制表示。 `compute_hash`方法将字符串值转换为哈希码。 `compute_signature`方法计算给定文档的MinHash签名。对于文档中的每个词,我们计算其哈希值,并将其与每个哈希函数的参数相乘并取模。然后,我们将每个哈希函数的最小值作为文档的签名。 `compute_similarity`方法计算两个文档的相似度。它简单地计算两个签名之间相等哈希函数的比例。 在示例用法中,我们创建了两个文档,并使用MinHash计算它们的签名。然后,我们计算了两个签名之间的相似度,并打印了结果。 请注意,此处的示例代码是简化版的MinHash实现,并且可能不适用于大规模数据集。在实际应用中,您可能需要使用更高效的数据结构和算法来处理大量数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值