Cosine distance between two vectors is defined as:

It is often used as evaluate the similarity of two vectors, the bigger the value is, the more similar between these two vectors.

Cosine distance is also can be defined as:

The smaller ** θ**, the more similar

**and**

*x*

*y.*In this tutorial, we will introduce how to calculate the cosine distance between two vectors using numpy, you can refer to our example to learn how to do.

**Import library**

import numpy as np

**Create two vectors**

vector_1 = np.array([1, 5, 1, 4, 0, 0, 0, 0, 0]) vector_2 = np.array([2, 4, 1, 1, 1, 1, 0, 0, 0])

**Calculate cosine distance**

def cos_sim(a, b): """Takes 2 vectors a, b and returns the cosine similarity """ dot_product = np.dot(a, b) # x.y norm_a = np.linalg.norm(a) #|x| norm_b = np.linalg.norm(b) #|y| return dot_product / (norm_a * norm_b)

**How to use?**

print(cos_sim(vector_1, vector_2))

The output is:

0.840473288592332