python教程—在给定稀疏矩阵数据的情况下，Python中计算余弦相似度最快的方法是什么?-Python实用宝典

# python教程—在给定稀疏矩阵数据的情况下，Python中计算余弦相似度最快的方法是什么?

```A=
[0 1 0 0 1
0 0 1 1 1
1 1 0 1 0]
```

```A =
0, 1
0, 4
1, 2
1, 3
1, 4
2, 0
2, 1
2, 3
```

```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
```

```array([[ 1.        ,  0.40824829,  0.40824829],
[ 0.40824829,  1.        ,  0.33333333],
[ 0.40824829,  0.33333333,  1.        ]])
```

```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))
```

```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
```

```A_sparse.transpose()
```

​Python实用宝典 (pythondict.com)