python计算余弦相似度最高的,给定稀疏矩阵数据,Python中最快的计算余弦相似度的方法是什么?...

Given a sparse matrix listing, what's the best way to calculate the cosine similarity between each of the columns (or rows) in the matrix? I would rather not iterate n-choose-two times.

Say the input matrix is:

A=

[0 1 0 0 1

0 0 1 1 1

1 1 0 1 0]

The sparse representation is:

A =

0, 1

0, 4

1, 2

1, 3

1, 4

2, 0

2, 1

2, 3

In Python, it's straightforward to work with the matrix-input format:

import numpy as np

from sklearn.metrics import pairwise_distances

from scipy.spatial.distance import cosine

A = np.array(

[[0, 1, 0, 0, 1],

[0, 0, 1, 1, 1],

[1, 1, 0, 1, 0]])

dist_out = 1-pairwise_distances(A, metric="cosine")

dist_out

Gives:

array([[ 1. , 0.40824829, 0.40824829],

[ 0.40824829, 1. , 0.33333333],

[ 0.40824829, 0.33333333, 1. ]])

That's fine for a full-matrix input, but I really want to start with the sparse representation (due to the size and sparsity of my matrix). Any ideas about how this could best be accomplished? Thanks in advance.

解决方案

You can compute pairwise cosine similarity on the rows of a sparse matrix directly using sklearn. As of version 0.17 it also supports sparse output:

from sklearn.metrics.pairwise import cosine_similarity

from scipy import sparse

A = np.array([[0, 1, 0, 0, 1], [0, 0, 1, 1, 1],[1, 1, 0, 1, 0]])

A_sparse = sparse.csr_matrix(A)

similarities = cosine_similarity(A_sparse)

print('pairwise dense output:\n {}\n'.format(similarities))

#also can output sparse matrices

similarities_sparse = cosine_similarity(A_sparse,dense_output=False)

print('pairwise sparse output:\n {}\n'.format(similarities_sparse))

Results:

pairwise dense output:

[[ 1. 0.40824829 0.40824829]

[ 0.40824829 1. 0.33333333]

[ 0.40824829 0.33333333 1. ]]

pairwise sparse output:

(0, 1) 0.408248290464

(0, 2) 0.408248290464

(0, 0) 1.0

(1, 0) 0.408248290464

(1, 2) 0.333333333333

(1, 1) 1.0

(2, 1) 0.333333333333

(2, 0) 0.408248290464

(2, 2) 1.0

If you want column-wise cosine similarities simply transpose your input matrix beforehand:

A_sparse.transpose()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要明确随机游走概率的定义。在一个有向图,随机游走是指从当前节点开始,以一定的概率跳转到相邻节点,直到到达目标节点或者达到最大步数为止。而随机游走概率就是从一个节点开始,经过若干次随机游走后到达目标节点的概率。 在本题,我们需要计算的是从任意一个节点出发,在经过若干次随机游走后,最终到达每个节点的概率。同时,我们还需要引入一个重启概率,表示在某些情况下,游走会重新从起点开始。因此,我们可以使用迭代计算方法计算随机游走概率。 具体来说,我们可以将随机游走概率表示为一个向量 p,其 p[i] 表示从节点 i 出发,最终到达每个节点的概率。同时,我们可以将重启概率表示为一个向量 r,其 r[i] 表示在第一步游走时,重新从节点 i 开始的概率。则随机游走概率可以通过以下公式计算: p = (1 - α) * M * p + α * r 其,M 是给定的有向图对应的矩阵(方阵),α 是一个参数,表示重启概率。在每次迭代,我们都将当前的 p 向量乘以矩阵 M,并加上重启向量 r 乘以一个常数 α。通过反复迭代,我们最终可以得到每个节点的随机游走概率。 下面是 Python 代码实现: ```python import numpy as np # 读取矩阵文件 M = np.loadtxt('in_matrix.txt') n = M.shape[0] # 设置参数 alpha = 0.85 # 重启概率 max_iter = 100 # 最大迭代次数 epsilon = 1e-8 # 收敛阈值 # 初始化随机游走概率向量和重启向量 p = np.ones(n) / n r = np.ones(n) / n # 迭代计算随机游走概率 for i in range(max_iter): new_p = (1 - alpha) * np.dot(M, p) + alpha * r if np.linalg.norm(new_p - p) < epsilon: break p = new_p # 输出结果 for i in range(n): print(f'节点{i}的随机游走概率为{p[i]:.4f}') ``` 需要注意的是,在实际应用,矩阵 M 可能非常大,因此我们需要使用稀疏矩阵表示,以减少内存占用和计算时间。同时,为了避免数值精度问题,我们还需要对迭代过程进行差分处理,即将每次迭代的结果与上一次迭代的结果进行比较,并在达到一定的阈值后停止迭代。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值