. Advertisement .

..3..

. Advertisement .

..4..

Python is an object-oriented programming language that is widely used. It can create software, websites, games, and mobile applications. When developing machine learning applications, calculating similarity can be extremely useful.

“**How to cosine similarity in python**” is a fairly common question every programmer will encounter. So, what are our options? Everything will be explained to you.

**How to cosine similarity in Python**

Cosine Similarity calculates the similarity of two vectors by taking the dot product and dividing it by the magnitudes of each vector.

We can use Python to convert text and images to vectors and apply the same logic! Scikit-learn, PIL, and Numpy simplify the process even further. In this article, we’ll go over a few examples of using cosine similarity and calculating it in Python.

**Option 1: Use The numpy.norm()**

The easiest way to cosine similarity in Python is using numpy.norm(). Look at this example to understand more clearly.

```
import numpy as np
List1 = np.array([[8,54,6,9],
[9,8,7,5]])
List2 = np.array([ 42, 4, 3, 5])
result = List1.dot(List2)/ (np.linalg.norm(List1, axis=1) * np.linalg.norm(List2))
print(result)
```

Output:

`[0.25946924 0.72347603]`

We can now reassure users that the problem is simple to resolve.

**Option 2: Use the numpy**

You need to use numpy. When you use it, the vector norm is returned by the numpy.norm() method. Let’s learn more about this through the following example:

```
from numpy import dot
from numpy.linalg import norm
List1 = [8,54,6,9]
List2 = [9,8,7,5]
result = dot(List1, List2)/(norm(List1)*norm(List2))
print(result)
```

Output:

`0.717620473957404`

### Option 3: Use the scipy

In order to calculate the distance rather than the cosine similarity, we can use the spatial.cosine.distance() function from the scipy module. However, to do so we must take 1 minus the distance’s value. For instance:

```
from scipy import spatial
List1 = [4, 47, 8, 3]
List2 = [3, 52, 12, 16]
result = 1 - spatial.distance.cosine(List1, List2)
print(result)
```

Output:

`0.9720951480078084`

### Option 4: Use the sklearn

The cosine similarity can be determined using a built-in function in the sklearn module called cosine similarity(). Let’s see the following program:

```
from sklearn.metrics.pairwise import cosine_similarity,cosine_distances
A=np.array([10,3])
B=np.array([8,7])
result=cosine_similarity(A.reshape(1,-1),B.reshape(1,-1))
print(result)
```

Output:

`[[0.91005765]]`

**Conclusion**

Python lends itself well to high-level systems. Nonetheless, performance would be marginally inferior to that of the native language. You quickly gain safety, range of motion, and maintenance, however.

Individual solutions provided in these tools are some of the most fundamental for anyone faced with the question “**How to cosine similarity in Python**“.

If you still need assistance or have basic Python questions, a growing community of people are usually willing to assist you. In addition, we anticipate a more creative day filled with new ideas and code.

**Read more**

## Leave a comment