Best Practice to Calculate Cosine Distance Between Two Vectors in NumPy – NumPy Tutorial

By | September 15, 2019

Cosine distance between two vectors is defined as:

cosine distance equation

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:

cosine distance example

The smaller θ, the more similar x and 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

Leave a Reply

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