In order to check similarity between 2 vectors, we can check the distance between them. There are a few different metrics to measure distance between 2 vectors.
import numpy as np
Measures the angle between 2 non-zero vectors. It ranges between
def cosine_similarity(p, q):
dot_prod = np.dot(p, q)
norm1 = np.linalg.norm(p)
norm2 = np.linalg.norm(q)
return dot_prod / (norm1 * norm2)
Measures the straight line distance between 2 vectors
def euclidean_distance(p, q):
return np.linalg.norm(p - q)
Measures the distance between two points in a grid based on the sum of the absolute differences of their coordinates.
def manhattan_distance(p, q):
return np.sum(np.abs(p - q))
Cosine distance is related to cosine similarity, but is defined as 1 - cosine_similarity
def cosine_distance(p, q):
return 1 - np.dot(p, q) / (np.linalg.norm(p) * np.linalg.norm(q))