8
$\begingroup$

I am using below code to compute cosine similarity between the 2 vectors. It returns a matrix instead of a single value 0.8660254.

[[ 1. 0.8660254]

[ 0.8660254 1. ]]

from sklearn.metrics.pairwise import cosine_similarity
vec1 = [1,1,0,1,1]
vec2 = [0,1,0,1,1]
print(cosine_similarity([vec1, vec2]))
$\endgroup$
11
$\begingroup$

Based on the documentation cosine_similarity(X, Y=None, dense_output=True) returns an array with shape (n_samples_X, n_samples_Y). Your mistake is that you are passing [vec1, vec2] as the first input to the method. Also your vectors should be numpy arrays:

from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
vec1 = np.array([[1,1,0,1,1]])
vec2 = np.array([[0,1,0,1,1]])
#print(cosine_similarity([vec1, vec2]))
print(cosine_similarity(vec1, vec2))

X : ndarray or sparse array, shape: (n_samples_X, n_features) Input data.

So you have to specify the dimension.

np.array([1, 2]).shape

has funny shape:

(2, )

$\endgroup$
0
$\begingroup$
vec1 = [1, 1, 0, 1, 1]
vec2 = [0, 1, 0, 1, 1]
print(cosine_similarity([vec1], [vec2]))

I passed the 2nd vec2 as Y and I got the output as a scalar.

New contributor
mamuni is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.