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]]
Python Cosine Similarity Calculation Error: Extra Parentheses in Print Statement

原文地址: https://www.cveoy.top/t/topic/mD5d 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录