Cosine distance

The Cosine distance is a measure of distance between two non-zero vectors of an inner product space.


Computational methods



Mathematical formulation


Given a set \(U\) of \(n\) feature vectors \(u_1 = (u_{11}, \ldots, u_{1k}), \ldots, u_n = (u_{n1}, \ldots, u_{nk})\) of dimension \(k\) and a set \(V\) of \(m\) feature vectors \(v_1 = (v_{11}, \ldots, v_{1k}), \ldots, v_m = (v_{m1}, \ldots, v_{mk})\) of dimension \(k\), the problem is to compute the Cosine distance \(D_{cos}(u_i, v_j)\) for any pair of input vectors:

\[D_{cos}(u_i, v_j) = 1 - \frac{\sum_{l=1}^{k}u_{il}v_{jl}}{\sqrt{\sum_{l=1}^{k}u_{il}^{2}}\sqrt{\sum_{l=1}^{k}v_{jl}^{2}}},\]

where \(\quad 1 \leq i \leq n, \quad 1 \leq j \leq m\).

Computation method: dense

The method defines Cosine distance metric, which is used in other algorithms for the distance computation. There is no separate computation mode to compute the distance manually.

Programming Interface

Refer to API Reference: Cosine distance.