Distance metrics#
Every Thetan function that takes a metric accepts the same names. They
are implemented in C in thetan.metrics and oriented so that a smaller
value always means two rows are closer. θ is compared with that value.
Name |
Value for rows |
|---|---|
|
|
|
|
|
|
|
same as |
|
|
|
|
|
|
|
the angle between |
|
|
The negative dot product#
dot_prod flips the sign of the dot product so that it behaves like a
distance. θ is then negative: theta=-0.9 means “join a centroid when the
dot product is above 0.9”. The dot product also grows with the length of
the vectors, so normalise rows to unit length first; on unit vectors it is
the cosine similarity.
Computing distances directly#
distance_matrix evaluates any of
these metrics between every row of two arrays:
import numpy as np
from thetan import distance_matrix
A = np.ascontiguousarray(np.random.rand(5, 3))
B = np.ascontiguousarray(np.random.rand(4, 3))
D = distance_matrix(A, B, metric="cosine") # shape (5, 4)
See it#
Clustering directions with angular metrics clusters 3D directions with the angular metrics and shows why the Euclidean metric fails on them.