Python Cosine Similarity Calculation Using NumPy and Scikit-learn
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
vec1 = np.array([1, 2, 3, 4])
vec2 = np.array([5, 6, 7, 8])
cos_sim = cosine_similarity(vec1.reshape(1, -1), vec2.reshape(1, -1))
print(cos_sim[0][0])
The output of this code is:
0.9688639316269661
This indicates that the cosine similarity between the two vectors is approximately 0.969, suggesting a high degree of similarity between them.
原文地址: https://www.cveoy.top/t/topic/mD49 著作权归作者所有。请勿转载和采集!