Skip to content

Clustering: cicada_tools.clustering

from cicada_analysis.cicada_tools.clustering.<module> import <function>


clustering.clustering_utils (K-means)

  • find_best_kmeans

find_best_kmeans(data, n_cluster, n_tests=100, scoring_fn=np.median, seed=0) -> (KMeans, float)

Runs KMeans n_tests times and returns the fit with the best silhouette score (per-cluster silhouettes reduced via scoring_fn; pass np.mean for the original Malvache-paper behavior). seed (int or np.random.Generator) sets the initialisations: the same seed gives the same fit.

Parameter Type Default Description
data np.ndarray Data to cluster
n_cluster int Number of clusters
n_tests int 100 Number of tests
scoring_fn callable np.median Scoring function for silhouettes
seed int 0 Random seed

Returns: (KMeans, float) — Best KMeans fit and its score


clustering.clustering_plots (Plots)

  • plot_silhouettes

plot_silhouettes(ax, kmeans, data, n_clusters, surrogate_threshold=None) -> None

Draws a per-cluster silhouette bar chart on ax; clusters whose mean silhouette exceeds surrogate_threshold get a white outline.

Parameter Type Default Description
ax Axes Matplotlib axes
kmeans KMeans KMeans fit
data np.ndarray Original data
n_clusters int Number of clusters
surrogate_threshold float None Significance threshold

Returns: None