Previous topic

medpy.metric.histogram.correlate_1

Next topic

medpy.metric.histogram.cosine_1

This Page

medpy.metric.histogram.cosine

medpy.metric.histogram.cosine(h1, h2)[source]

Cosine simmilarity.

Compute the angle between the two histograms in vector space irrespective of their length. The cosine similarity between two histograms \(H\) and \(H'\) of size \(m\) is defined as:

\[d_{\cos}(H, H') = \cos\alpha = \frac{H * H'}{\|H\| \|H'\|} = \frac{\sum_{m=1}^M H_m*H'_m}{\sqrt{\sum_{m=1}^M H_m^2} * \sqrt{\sum_{m=1}^M {H'}_m^2}}\]

Attributes:

  • not a metric, a similarity

Attributes for normalized histograms:

  • \(d(H, H')\in[0, 1]\)
  • \(d(H, H) = 1\)
  • \(d(H, H') = d(H', H)\)

Attributes for not-normalized histograms:

  • \(d(H, H')\in[-1, 1]\)
  • \(d(H, H) = 1\)
  • \(d(H, H') = d(H', H)\)

Attributes for not-equal histograms:

  • not applicable
Parameters:

h1 : sequence

The first histogram.

h2 : sequence

The second histogram, same bins as h1.

Returns:

cosine : float

Cosine simmilarity.

Notes

The resulting similarity ranges from -1 meaning exactly opposite, to 1 meaning exactly the same, with 0 usually indicating independence, and in-between values indicating intermediate similarity or dissimilarity.