`E. Zhu et al. `_. Args: threshold (float): Containment阈值 取值范围[0.0, 1.0]。 num_perm (int, optional): Minhash中所使用的排列函数的个数。 num_part (int, optional): LSH Ensemble分区的个数。 m (int, optional): 特殊超参数: LSH Ensemble使用大约比相同
| 45 | |
| 46 | |
| 47 | class MinHashLSHEnsemble(object): |
| 48 | ''' |
| 49 | `E. Zhu et al. <http://www.vldb.org/pvldb/vol9/p1185-zhu.pdf>`_. |
| 50 | |
| 51 | Args: |
| 52 | threshold (float): Containment阈值 取值范围[0.0, 1.0]。 |
| 53 | num_perm (int, optional): Minhash中所使用的排列函数的个数。 |
| 54 | num_part (int, optional): LSH Ensemble分区的个数。 |
| 55 | m (int, optional): 特殊超参数: LSH Ensemble使用大约比相同数量的MinHash LSH多出m倍的内存空间。 |
| 56 | 另外,m越大,精确度越高。 |
| 57 | weights (tuple, optional): 在优化参数设定时,对fp和fn重要性的权衡考量。 |
| 58 | storage_config (dict, optional): 存储相关参数,不重要 |
| 59 | prepickle (bool, optional): 默认值由`storage_config`确定,不重要. |
| 60 | |
| 61 | Note: |
| 62 | 更多的分区(`num_part`)可以取得更好的准确性。 |
| 63 | ''' |
| 64 | |
| 65 | def __init__(self, threshold=0.9, num_perm=128, num_part=16, m=8, |
| 66 | weights=(0.5,0.5), storage_config=None, prepickle=None): |
| 67 | if threshold > 1.0 or threshold < 0.0: |
| 68 | raise ValueError("threshold must be in [0.0, 1.0]") |
| 69 | if num_perm < 2: |
| 70 | raise ValueError("Too few permutation functions") |
| 71 | if num_part < 1: |
| 72 | raise ValueError("num_part must be at least 1") |
| 73 | if m < 2 or m > num_perm: |
| 74 | raise ValueError("m must be in the range of [2, num_perm]") |
| 75 | if any(w < 0.0 or w > 1.0 for w in weights): |
| 76 | raise ValueError("Weight must be in [0.0, 1.0]") |
| 77 | if sum(weights) != 1.0: |
| 78 | raise ValueError("Weights must sum to 1.0") |
| 79 | self.threshold = threshold |
| 80 | self.h = num_perm |
| 81 | self.m = m |
| 82 | rs = self._init_optimal_params(weights) |
| 83 | # 对于r的每个可能取值,对LSHEnsemble中每个分区初始化一个LSH,索引初始化完成 |
| 84 | storage_config = {'type': 'dict'} if not storage_config else storage_config |
| 85 | basename = storage_config.get('basename', _random_name(11)) |
| 86 | self.indexes = [ |
| 87 | dict((r, MinHashLSH( |
| 88 | num_perm=self.h, |
| 89 | params=(int(self.h/r), r), |
| 90 | # 不同的分区存储方式可能不同 |
| 91 | storage_config=self._get_storage_config( |
| 92 | basename, storage_config, partition, r), |
| 93 | prepickle=prepickle)) for r in rs) |
| 94 | for partition in range(0, num_part)] |
| 95 | self.lowers = [None for _ in self.indexes] |
| 96 | self.uppers = [None for _ in self.indexes] |
| 97 | |
| 98 | # 给出一系列可能存在的xq,预处理得到使fp和fn带权和最优的参数b与r |
| 99 | def _init_optimal_params(self, weights): |
| 100 | false_positive_weight, false_negative_weight = weights |
| 101 | self.xqs = np.exp(np.linspace(-5, 5, 10)) |
| 102 | self.params = np.array([_optimal_param(self.threshold, self.h, self.m, |
| 103 | xq, |
| 104 | false_positive_weight, |
no outgoing calls
no test coverage detected