selector.measures.similarity#
Similarity Module.
This module provides functions to calculate similarity metrics between vectors.
Available Similarity Metrics:#
Metric |
Similarity Formula (S) |
|---|---|
Cosine |
\(S_{cos} = \frac{a \cdot b}{\|a\| \|b\|}\) |
Dice (Continuous) |
\(S_{dice} = \frac{2(a \cdot b)}{\|a\|^2+\|b\|^2}\) |
Continuous Dice (cDC) |
\(S_{cDC} = \frac{2(a \cdot b)}{c\sum_i a_i + \sum_i b_i}\) |
Tanimoto |
\(\frac{a \cdot b}{\|a\|^2+\|b\|^2 - a \cdot b}\) |
Note: The Dice similarity implemented here is a continuous vector extension.
- selector.measures.similarity.continuous_dice(a: array, b: array) float#
Compute the continuous Dice coefficient (cDC).
The continuous Dice coefficient extends the Dice coefficient to the comparison of a binary reference \(a\) with a continuous (probabilistic) array \(b\), with values typically in \([0, 1]\) 1. The size of \(a\) is weighted by a coefficient \(c\), so that the coefficient is 1 for complete overlap:
\[cDC(a, b) = \frac{2 \sum_i a_i b_i}{c \sum_i a_i + \sum_i b_i}\]where \(c\) is the mean value of \(b\) over the positions where both \(a\) and \(b\) are positive:
\[c = \frac{\sum_i a_i b_i}{\sum_i a_i \, \mathrm{sign}(b_i)}\]This measure is not symmetric in its arguments. If \(a\) and \(b\) have no positive overlap, the coefficient is defined as 0.0.
Parameters#
- andarray of shape (n_features,)
The 1D binary (reference) array.
- bndarray of shape (n_features,)
The 1D continuous (probabilistic) array.
Returns#
- coefffloat
Continuous Dice coefficient between arrays \(a\) and \(b\).
References#
- 1
Shamir, R. R.; Duchin, Y.; Kim, J.; Sapiro, G.; Harel, N. Continuous Dice Coefficient: a Method for Evaluating Probabilistic Segmentations. bioRxiv 2018, 306977.
- selector.measures.similarity.cosine(a: array, b: array) float#
Compute Cosine similarity coefficient.
For two vectors \(a\) and \(b\), Cosine similarity is defined as the dot product of the vectors divided by the product of their lengths:
\[S_{cos}(a, b) = \frac{a \cdot b}{\|a\| \|b\|}\]If either vector has a norm of zero, the similarity is defined as 0.0.
Parameters#
- andarray of shape (n_features,)
The 1D feature array of sample \(a\) in an n_features dimensional space.
- bndarray of shape (n_features,)
The 1D feature array of sample \(b\) in an n_features dimensional space.
Returns#
- coefffloat
Cosine similarity coefficient between feature arrays \(a\) and \(b\).
- selector.measures.similarity.dice(a: array, b: array) float#
Compute Dice (Sørensen-Dice) similarity coefficient.
For two vectors \(a\) and \(b\), Dice similarity is defined as:
\[S_{dice}(a, b) = \frac{2 (a \cdot b)}{\|a\|^2 + \|b\|^2}\]This is a continuous vector extension of the binary Dice similarity. If the norm of both vectors is zero, the similarity is defined as 0.0. For vectors containing negative values, the output may fall outside the [0, 1] range.
Parameters#
- andarray of shape (n_features,)
The 1D feature array of sample \(a\) in an n_features dimensional space.
- bndarray of shape (n_features,)
The 1D feature array of sample \(b\) in an n_features dimensional space.
Returns#
- coefffloat
Dice similarity coefficient between feature arrays \(a\) and \(b\).
- selector.measures.similarity.modified_tanimoto(a: array, b: array) float#
Compute the modified tanimoto coefficient from bitstring vectors of data points A and B.
Adjusts calculation of the Tanimoto coefficient to counter its natural bias towards shorter vectors using a Bernoulli probability model.
\[{mt} = \frac{2-p}{3} T_1 + \frac{1+p}{3} T_0\]where \(p\) is success probability of independent trials, \(T_1\) is the number of common ‘1’ bits between data points (\(T_1 = | A \cap B |\)), and \(T_0\) is the number of common ‘0’ bits between data points (\(T_0 = |(1-A) \cap (1-B)|\)).
Parameters#
- andarray of shape (n_features,)
The 1D bitstring feature array of sample \(A\) in an n_features dimensional space.
- bndarray of shape (n_features,)
The 1D bitstring feature array of sample \(B\) in an n_features dimensional space.
Returns#
- mtfloat
Modified tanimoto coefficient between bitstring feature arrays \(A\) and \(B\).
Notes#
The equation above has been derived from
\[{mt}_{\alpha} = {\alpha}T_1 + (1-\alpha)T_0\]where \(\alpha = \frac{2-p}{3}\). This is done so that the expected value of the modified tanimoto, \(E(mt)\), remains constant even as the number of trials \(p\) grows larger.
Fligner, M. A., Verducci, J. S., and Blower, P. E.. (2002) A Modification of the Jaccard-Tanimoto Similarity Index for Diverse Selection of Chemical Compounds Using Binary Strings. Technometrics 44, 110-119.
- selector.measures.similarity.pairwise_similarity_bit(X: array, metric: str) ndarray#
Compute pairwise similarity coefficient matrix.
Parameters#
- Xndarray of shape (n_samples, n_features)
Feature matrix of n_samples samples in n_features dimensional space.
- metricstr
The metric used when calculating similarity coefficients between samples in a feature array. Method for calculating similarity coefficient. Options: “tanimoto”, “modified_tanimoto”, “cosine”, “dice”.
Returns#
- sndarray of shape (n_samples, n_samples)
A symmetric similarity matrix between each pair of samples in the feature matrix. The diagonal elements are directly computed instead of assuming that they are 1.
- selector.measures.similarity.scaled_similarity_matrix(X: array) ndarray#
Compute the scaled similarity matrix.
\[X(i,j) = \frac{X(i,j)}{\sqrt{X(i,i)X(j,j)}}\]Parameters#
- Xndarray of shape (n_samples, n_samples)
Similarity matrix of n_samples.
Returns#
- sndarray of shape (n_samples, n_samples)
A scaled symmetric similarity matrix.
- selector.measures.similarity.tanimoto(a: array, b: array) float#
Compute Tanimoto coefficient or index (a.k.a. Jaccard similarity coefficient).
For two binary or non-binary arrays \(A\) and \(B\), Tanimoto coefficient is defined as the size of their intersection divided by the size of their union:
\[T(A, B) = \frac{| A \cap B|}{| A \cup B |} = \frac{| A \cap B|}{|A| + |B| - | A \cap B|} = \frac{A \cdot B}{\|A\|^2 + \|B\|^2 - A \cdot B}\]where \(A \cdot B = \sum_i{A_i B_i}\) and \(\|A\|^2 = \sum_i{A_i^2}\).
Parameters#
- andarray of shape (n_features,)
The 1D feature array of sample \(A\) in an n_features dimensional space.
- bndarray of shape (n_features,)
The 1D feature array of sample \(B\) in an n_features dimensional space.
Returns#
- coefffloat
Tanimoto coefficient between feature arrays \(A\) and \(B\).
Bajusz, D., Rácz, A., and Héberger, K.. (2015) Why is Tanimoto index an appropriate choice for fingerprint-based similarity calculations?. Journal of Cheminformatics 7.