pairwise_distances函数是计算两个矩阵之间的余弦相似度,参数需要两个矩阵
cosine_similarity函数是计算多个向量互相之间的余弦相似度,参数一个二维列表
话不多说,上代码
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
m1 = np.mat([[0, 3, 2], [2, 5, 0], [3, 1, 4]])
m1_similarity = cosine_similarity(m1)
print m1_similarity
out:
[[1. 0.7725393 0.59832112]
[0.7725393 1. 0.40059637]
[0.59832112 0.40059637 1. ]]