擅长:python、mysql、java
<p>看这个问题:<a href="https://stackoverflow.com/questions/17627219/whats-the-fastest-way-in-python-to-calculate-cosine-similarity-given-sparse-mat">What's the fastest way in Python to calculate cosine similarity given sparse matrix data?</a></p>
<p>具有:</p>
<pre><code>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
</code></pre>
<p>结果:</p>
<pre><code>array([[ 1. , 0.40824829, 0.40824829],
[ 0.40824829, 1. , 0.33333333],
[ 0.40824829, 0.33333333, 1. ]])
</code></pre>
<p>但这适用于稠密矩阵。对于稀疏,你必须开发你的解决方案。</p>