Thursday, May 7, 2026

NumPy functions for dot product and cosine similarity

 To calculate dot products and cosine similarity in NumPy, you primarily use np.dot() and np.linalg.norm(). While NumPy has a direct function for the dot product, cosine similarity is typically calculated by combining several operations. 

1. Dot Product
The dot product of two vectors is the sum of the products of their corresponding elements. 
  • np.dot(a, b): The standard function for computing the dot product.
  • a @ b: A more modern and readable operator for matrix multiplication and dot products introduced in Python 3.5.
  • np.inner(a, b): Computes the inner product, which for 1D arrays is identical to the dot product. 
2. Cosine Similarity
NumPy does not have a single "cosine_similarity" function, so you must implement the formula:
similarity  = A(dot)B/Magnitude(A).Magnitude(B)
You can implement this using:
Example Implementation:
python
import numpy as np
from numpy.linalg import norm

def cosine_similarity(a, b):
    return np.dot(a, b) / (norm(a) * norm(b))
Quick Comparison
Metric NumPy Function(s)Result Range
Dot Productnp.dot(a, b) or a @ b-infinity to +infinity
Cosine Similaritynp.dot(a, b) / (norm(a) * norm(b)) -1 to 1 
Note: For a direct, single-function implementation, many developers use the Scikit-learn cosine_similarity function or SciPy's spatial.distance.cosine (which returns cosine distance, or 1 - similarity)

No comments:

Post a Comment

Normalization Algorithms in Machine Learning

1. Feature Scaling (Traditional ML) Technique Description Formula / Key Point ...