← All notes

Statistical note

Cosine similarity

Cosine similarity: theory, interpretation, Python computation, and a scientific-imaging case study.

6 min read

Clustering metrics either measure internal geometry without references or compare assignments with known labels. Similarity functions define the geometry on which many algorithms depend.

Concept figure for Cosine similarity

Mathematical definition

The central quantity is

cos(x,y)=xdoty/(xy).cos(x,y)=x dot y/(||x|| ||y||).

This expression states what is being counted, averaged, ranked, or compared. Its scale and direction must be interpreted in the context of the data and sampling design.

Compute it in Python

import numpy as np
from sklearn import metrics

X = np.array([[0,0], [0,1], [5,5], [5,6], [9,1], [9,2]], dtype=float)
labels = np.array([0, 0, 1, 1, 2, 2])
reference = np.array([1, 1, 0, 0, 2, 2])
from sklearn.metrics.pairwise import cosine_similarity
print(cosine_similarity(X[:1], X[1:]))

The function is sklearn.metrics.pairwise.cosine_similarity. Inspect its current signature and return object in the official documentation for the version installed in your environment.

Interpretation and cautions

Internal scores encode shape assumptions and cannot prove biological validity. External scores require defensible reference labels. Compare stability across resampling, batches, representations, and cluster counts.

A numerical value is not self-interpreting. Compare it with a baseline, uncertainty interval, operational threshold, or competing model, and retain the underlying observations or confusion counts.

Scientific-imaging case study

For angular similarity independent of vector magnitude in phenotype embeddings, inspect cluster stability and independent biological markers rather than optimizing one index alone.

Split train, validation, and test data at the specimen or experimental level. Report variability across independent repeats so that the metric describes generalization rather than leakage.