python实现e2lsh高维数据集k近邻搜索——实现流程

  • lsh学习链接:
    LSH(Locality Sensitive Hashing)原理与实现
    对高维数据查询最近邻,推荐使用p-stable LSH;
    minLSH是针对文档查询最近邻得方法;
  • python学习与使用:
    python入门之类
  • 常用函数:
    random.gauss(mu, sigma):均值为mu且标准偏差为sigma的高斯分布
    random.uniform(x, y):将随机生成下一个实数,它在 [x, y] 范围内。
    numpy.inner():返回一维数组的向量内积。对于更高的维度,它返回最后一个轴上的和的乘积。
    字典(Dictionary) update() :函数把字典dict2的键/值对更新到dict里。
  • lsh参数计算学习链接:
    参考论文:2004 Locality-sensitive hashing using stable distributions
    E2LSH的原理与实现
    LSH在欧式空间的应用(1)–碰撞概率分析
    LSH在欧式空间的应用(2)–工作原理
    LSH在欧式空间的应用(3)–参数选择
    以上三个链接是用R语言举例;
    正态分布就是高斯分布;
  • knn搜索
    在使用lsh做近似近邻搜索后,得到近邻索引,通过近邻索引对近邻数据做k近邻检索,得到精确的k个最近邻。
    python做 knn可以调用库函数实现:
from sklearn.neighbors import NearestNeighbors
# 函数功能:对数据集x的前1000个数据做10近邻搜索;
# 搜索范围:数据集x内全体数据;
def knn_search(X):
    # 查询点y:x内前一千个数;
    Y = X[0:1000]
    # 查询范围:x内全体数据;查询11近邻;
    nbs = NearestNeighbors(n_neighbors=11, algorithm='ball_tree').fit(X)
    # distances:最近邻距离;indices:最近邻索引号;
    distances, indices = nbs.kneighbors(Y)
    # knn近邻搜索范围包含自己,返回结果去除自己(即去除查询结果第一列),得到查询点的十近邻;
    return indices[:, 1:]

综上,完成lsh对数据集k近邻查询;
查询结果(对数据集做两遍查询,一次e2lsh,一次knn,对比查询结果):
lsh近邻搜索

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LSH(局部敏感哈希)是一种用于高维数据的近似最近邻搜索的技术。以下是Python实现LSH算法的步骤: 1. 安装必要的库:numpy和bitarray。可以使用以下命令进行安装: ```shell pip install numpy bitarray ``` 2. 导入库: ```python import numpy as np from bitarray import bitarray ``` 3. 定义哈希函数: ```python def hash_vector(v, planes): return "".join(['1' if np.dot(v, plane) > 0 else '0' for plane in planes]) ``` 4. 定义LSH类: ```python class LSH: def __init__(self, num_planes, input_dim): self.num_planes = num_planes self.input_dim = input_dim self.planes = np.random.normal(size=(num_planes, input_dim)) self.hash_tables = {} for i in range(num_planes): self.hash_tables[i] = {} def add(self, v, label): hash_val = hash_vector(v, self.planes) for i in range(self.num_planes): if hash_val[i] not in self.hash_tables[i]: self.hash_tables[i][hash_val[i]] = bitarray(self.input_dim) self.hash_tables[i][hash_val[i]].setall(0) self.hash_tables[i][hash_val[i]][label] = 1 def query(self, v): hash_val = hash_vector(v, self.planes) candidates = None for i in range(self.num_planes): if hash_val[i] not in self.hash_tables[i]: return None if candidates is None: candidates = self.hash_tables[i][hash_val[i]] else: candidates = candidates & self.hash_tables[i][hash_val[i]] return candidates ``` 5. 创建LSH对象并添加数据: ```python lsh = LSH(num_planes=10, input_dim=100) for i in range(1000): v = np.random.normal(size=100) lsh.add(v, i) ``` 6. 查询最近邻: ```python query_v = np.random.normal(size=100) candidates = lsh.query(query_v) if candidates is not None: distances = np.linalg.norm(query_v - candidates, axis=1) nearest_neighbor = np.argmin(distances) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值