python向量运算为什么快_如何在Python中快速计算大量向量的余弦相似度?

我有一组10万个向量,我需要根据余弦相似度检索前25个最近的向量。在

Scipy和Sklearn有计算余弦距离/相似性2矢量的实现,但我需要计算100k X 100k大小的余弦Sim,然后去掉top-25。在python计算中有没有什么快速实现?在

根据@Silmathoron的建议,这就是我要做的-#vectors is a list of vectors of size : 100K x 400 i.e. 100K vectors each of dimenions 400

vectors = numpy.array(vectors)

similarity = numpy.dot(vectors, vectors.T)

# squared magnitude of preference vectors (number of occurrences)

square_mag = numpy.diag(similarity)

# inverse squared magnitude

inv_square_mag = 1 / square_mag

# if it doesn't occur, set it's inverse magnitude to zero (instead of inf)

inv_square_mag[numpy.isinf(inv_square_mag)] = 0

# inverse of the magnitude

inv_mag = numpy.sqrt(inv_square_mag)

# cosine similarity (elementwise multiply by inverse magnitudes)

cosine = similarity * inv_mag

cosine = cosine.T * inv_mag

k = 26

box_plot_file = file("box_data.csv","w+")

for sim,query in itertools.izip(cosine,queries):

k_largest = heapq.nlargest(k, sim)

k_largest = map(str,k_largest)

result = query + "," + ",".join(k_largest) + "\n"

box_plot_file.write(result)

box_plot_file.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值