logo_learn_stats

Methods to Calculate Cosine Similarity in Python

Posted on
banner 336x280

Cosine Similarity is a measure of the similarity between two vectors of an inside product field.

For 2 vectors, A and B, the Cosine Similarity is calculated as:

banner 468x60

Cosine Similarity = ΣAiBi / (√ΣAi2√ΣBi2)

This educational explains methods to calculate the Cosine Similarity between vectors in Python the use of purposes from the NumPy library.

Cosine Similarity Between Two Vectors in Python

Please see code presentations methods to calculate the Cosine Similarity between two arrays in Python:

from numpy import dot
from numpy.linalg import norm

#outline arrays
a = [23, 34, 44, 45, 42, 27, 33, 34]
b = [17, 18, 22, 26, 26, 29, 31, 30]

#calculate Cosine Similarity
cos_sim = dot(a, b)/(norm(a)*norm(b))

cos_sim

0.965195008357566

The Cosine Similarity between the 2 arrays seems to be 0.965195.

Word that this mode will paintings on two arrays of any space:

import numpy as np
from numpy import dot
from numpy.linalg import norm

#outline arrays
a = np.random.randint(10, measurement=100)
b = np.random.randint(10, measurement=100)

#calculate Cosine Similarity
cos_sim = dot(a, b)/(norm(a)*norm(b))

cos_sim

0.7340201613960431

Then again, it handiest works if the 2 arrays are of equivalent space:

import numpy as np
from numpy import dot
from numpy.linalg import norm

#outline arrays
a = np.random.randint(10, measurement=90) #space=90
b = np.random.randint(10, measurement=100) #space=100

#calculate Cosine Similarity
cos_sim = dot(a, b)/(norm(a)*norm(b))

cos_sim

ValueError: shapes (90,) and (100,) no longer aligned: 90 (dark 0) != 100 (dark 0)

Notes

1. There are a couple of tactics to calculate the Cosine Similarity the use of Python, however as this Stack Flooding anecdote explains, the mode defined on this publish seems to be the quickest.

2. The following this Wikipedia web page to be informed extra information about Cosine Similarity.

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *