import numpy as npfrom sklearnmetricspairwise import cosine_similarityvec1 = nparray1 2 3 4vec2 = nparray5 6 7 8cos_sim = cosine_similarityvec1reshape1 -1 vec2reshape1 -1printcos_sim
There is an extra pair of parentheses at the end of the print statement. It should be:
print(cos_sim)
The corrected code is:
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)
Output:
[[0.96886393]]
原文地址: https://www.cveoy.top/t/topic/bdrY 著作权归作者所有。请勿转载和采集!