Metrics silhouette scorefromdistances - CyrilB1531/lodestar GitHub Wiki
Development build. This page describes
main, not a released package. The latest published Lodestar.Metrics is 0.4.0 โ read its documentation.
Home โบ Metrics โบ Clustering metrics
The mean over every sample, from a distance matrix you already have.
public static double ScoreFromDistances(ReadOnlySpan<int> labels, ReadOnlySpan<double> distances)Parameters โ labels gives each sample its cluster. distances is the n ร n matrix
row-major, so the distance from sample i to sample j is distances[(i * n) + j].
Returns โ double in [-1, 1], the same number Silhouette.Score gives for the euclidean matrix of
the same samples.
Exceptions โ ArgumentException when the inputs disagree in size, when a distance is not
finite, when a diagonal entry exceeds 100ยทฮต โ "The precomputed distance matrix contains non-zero
elements on the diagonal. Use np.fill_diagonal(X, 0)." โ and when the number of
distinct labels falls outside [2, n - 1] โ scikit-learn's own bound, carried with its own
sentence: Number of labels is 1. Valid values are 2 to n_samples - 1 (inclusive). Last, a negative
distance, with pairwise_distances' sentence "Negative values in data passed to pairwise_distances.
Precomputed distance need to have non-negative values.." โ the double space and the two stops are
the reference's, and a matrix holding both a negative entry and a bad label count gets the label
sentence, as scikit-learn orders them (#1275).
Example โ the matrix of the two clusters above, passed in directly.
using Lodestar.Metrics;
double[] distances =
[
0.0000, 0.1414, 7.0711, 7.2242, 7.0007,
0.1414, 0.0000, 6.9296, 7.0824, 6.8593,
7.0711, 6.9296, 0.0000, 0.2236, 0.1000,
7.2242, 7.0824, 0.2236, 0.0000, 0.3162,
7.0007, 6.8593, 0.1000, 0.3162, 0.0000,
];
int[] labels = [0, 0, 1, 1, 1];
double score = Silhouette.ScoreFromDistances(labels, distances); // => 0.9737โฆRemarks โ the example's matrix is rounded to four decimals, which is why it scores
0.9737โฆ where Silhouette.Score on the same samples scores 0.9738โฆ. The difference is the
rounding, not the method.
A name of its own rather than an overload of Silhouette.Score, because a distance
matrix and a block of features are both a span of double and the two signatures would collide.
A variant a parameter cannot carry gets a name, as PerOutput does on the regression metrics โ
here for an input rather than a return type.
Nothing checks that the matrix is a metric โ symmetric, zero on the diagonal, positive elsewhere. scikit-learn does not either, and a caller who passes a similarity by mistake gets a number rather than an exception.
Applies to โ net10.0, netstandard2.0.
See also โ Silhouette.Score, Silhouette.PerSampleFromDistances, the Python equivalence table.