pub fn silhouette_scores_per_cluster<F, S1, S2, D>(
x: &ArrayBase<S1, Ix2>,
labels: &ArrayBase<S2, D>,
metric: &str,
) -> Result<HashMap<usize, F>>Expand description
Calculate silhouette scores per cluster
Returns the mean silhouette score for each cluster, allowing you to identify which clusters are more cohesive than others.
§Arguments
x- Array of shape (n_samples, n_features) - The datalabels- Array of shape (n_samples,) - Predicted labels for each samplemetric- Distance metric to use. Currently only ‘euclidean’ is supported.
§Returns
- HashMap mapping cluster labels to their mean silhouette scores
§Examples
use scirs2_core::ndarray::{array, Array2};
use scirs2_metrics::clustering::silhouette_scores_per_cluster;
// Create a small dataset with 3 clusters
let x = Array2::from_shape_vec((9, 2), vec![
1.0, 2.0, 1.5, 1.8, 1.2, 2.2, // Cluster 0
5.0, 6.0, 5.2, 5.8, 5.5, 6.2, // Cluster 1
9.0, 10.0, 9.2, 9.8, 9.5, 10.2, // Cluster 2
]).unwrap();
let labels = array![0, 0, 0, 1, 1, 1, 2, 2, 2];
let cluster_scores = silhouette_scores_per_cluster(&x, &labels, "euclidean").unwrap();
assert_eq!(cluster_scores.len(), 3);
assert!(cluster_scores[&0] > 0.5);
assert!(cluster_scores[&1] > 0.5);
assert!(cluster_scores[&2] > 0.5);