from numpy import dot
from numpy.linalg import normcos_sim = dot(a, b)/(norm(a)*norm(b)) # 值越大越相似
或者
import numpy as npdef cosine_similarity(a, b):dot_product = np.dot(a, b)norm_a = np.linalg.norm(a)norm_b = np.linalg.norm(b)similarity = dot_product / (norm_a * norm_b)return similarity # 值越大越相似