Clustering always returns clusters, so the first question is whether the data has structure worth
finding, and the second is what kind: convex segments a business can act on (k-means, GMM, Ward), or
arbitrary dense groups with noise left out (HDBSCAN, DBSCAN). The features and their scaling decide more than
the algorithm. scripts/cluster_tendency.py measures cluster tendency (Hopkins statistic, isolation of
points, correlated-feature caveat); scripts/select_clustering.py ranks algorithms for the data and the goal,
with starting parameters and cautions.
Trigger Conditions
Activate this skill when the user:
- Wants to segment customers, users, products, stores, documents or embeddings
- Asks which clustering algorithm to use, or k-means vs DBSCAN vs HDBSCAN vs hierarchical vs GMM
- Has mixed numeric and categorical data to cluster
- Got clusters and wants to know whether they are real, stable or useful
- Needs to assign new records to existing segments
Choosing k for k-means: kmeans-cluster-count-finder; DBSCAN parameters: dbscan-parameter-tuner; reducing dimensions first: pca-dimensionality-reducer and umap-tsne-embedding-visualizer; scaling: feature-scaling-advisor.
Workflow
1. GOAL -> what decision do the segments drive? fixed k? must every row be assigned?
2. FEATURES -> behaviour features, no IDs/labels; transform skew; scale; handle categoricals
3. TENDENCY -> python scripts/cluster_tendency.py data.csv --drop id (H > 0.75 clustered, ~0.5 random)
4. SELECT -> python scripts/select_clustering.py --csv data.csv --drop id [--k-known 5 --noise ...]
5. FIT 2-3 -> top candidates; choose k / density parameters with the dedicated skills
6. VALIDATE -> internal index matching the algorithm, bootstrap stability (ARI), profiles, outcome differences
7. OPERATE -> naming, assignment of new rows (predict or a classifier), refresh cadence, drift of segment sizesIs There Anything to Cluster?
Hopkins H = 0.856 (blobs fixture: 3 groups + noise) -> clustered
Hopkins H = 0.535 (uniform fixture) -> no clear structure (close to random)
Hopkins H = 0.728 + 13 pairs with |r| > 0.7 (wide) -> correlated features, not necessarily clusters- H measures departure from uniform randomness. Strongly correlated features (points on a low-dimensional manifold) raise H without forming groups; the script warns when many column pairs have |r| > 0.7. Re-check on a few principal components.
- A high share of isolated points (> 3x the median nearest-neighbour distance) favours density methods that can label noise.
Selection Table
| Situation | First choice | Alternatives |
|---|---|---|
| k fixed by the business, numeric, convex segments | k-means (MiniBatchKMeans if large) | GMM, Ward |
| Overlapping groups, need probabilities | Gaussian mixture (BIC for k) | HDBSCAN soft clustering |
| Arbitrary shapes, noise, unknown k | HDBSCAN | DBSCAN (single density), OPTICS |
| Varying densities | HDBSCAN | OPTICS |
| Need a hierarchy / drill-down | Agglomerative + dendrogram | HDBSCAN condensed tree |
| Non-convex, small data, k known | Spectral clustering | HDBSCAN |
| Streaming / huge data | MiniBatchKMeans, BIRCH | HDBSCAN on a sample + classifier |
| Mixed numeric + categorical | k-prototypes | Gower + agglomerative / HDBSCAN |
| Categorical only | k-modes | Gower / Jaccard + agglomerative |
| Embeddings / text | UMAP -> HDBSCAN | k-means on normalised embeddings |
| Geographic points | HDBSCAN / DBSCAN with haversine | grid aggregation |
Assumptions, scaling limits and typical pipelines: algorithm-guide.md.
Preprocessing Decides the Result
- Choose features that describe the differences you want segments to capture.
- log1p skewed amounts and counts; standardise (or robust-scale) everything.
- Categoricals: a dedicated method (k-prototypes, Gower) or carefully down-weighted one-hot.
- Remove redundant copies of the same signal (they double its weight).
- Reduce dimensions above ~20-50 features (PCA for centroid methods, UMAP for density methods); never cluster on a 2-D t-SNE map.
Details and metric choice per data type: preprocessing-for-clustering.md.
Validating Clusters
| Check | How | Good sign |
|---|---|---|
| Internal index | silhouette / CH / DB for centroid methods; DBCV for density methods | clear optimum, not just "higher with k" |
| Stability | refit on 80% subsamples / new seeds; adjusted Rand index | ARI > 0.8 |
| Profiles | feature means per cluster relative to the population | nameable, distinct segments |
| Outcomes | churn / spend / response by cluster | real differences |
| Actionability | one action per segment | yes |
| Size | share per cluster | no trivial (< 1-2%) segments unless they are the point |
Code: validating-clusters.md. Verified recipes for every algorithm (k-means, GMM/BIC, HDBSCAN, DBSCAN, agglomerative + dendrogram, spectral, k-prototypes, Gower, k-modes) and how to assign new points: clustering-recipes.md. Note from the fixture: with 5% uniform noise, GMM's BIC keeps adding components (minimum at k = 8 vs k = 3 without noise); handle noise first.
Output Format
When recommending a clustering approach, deliver:
- Goal and constraints - decision, k fixed or free, noise allowed, soft or hard, hierarchy.
- Tendency - Hopkins H, isolated share, correlation caveat (
cluster_tendency.py). - Preprocessing - features, transforms, scaling, categorical handling, dimensionality reduction.
- Ranked algorithms - top 2-3 with starting parameters (
select_clustering.py). - Validation plan - index, stability, profiling, outcome comparison.
- Operational plan - assigning new rows, refresh cadence, monitoring segment sizes.
Scripts
python scripts/cluster_tendency.py data.csv --drop customer_id
python scripts/cluster_tendency.py data.csv --columns x1 x2 x3 --sample 300 --repeats 10 --json
python scripts/cluster_tendency.py data.csv --power d # dimension-adjusted Hopkins variantStandardises numeric columns, averages the Hopkins statistic over repeats (uniform points in the bounding box vs sampled data points), reports the nearest-neighbour distance CV and isolated-point share, and lists column pairs with |r| > 0.7. Exit codes: 0 clustered (H > 0.7), 1 no clear structure, 2 usage or read error.
python scripts/select_clustering.py --csv customers.csv --drop customer_id
python scripts/select_clustering.py --rows 2000000 --dims 12 --k-known 5 --streaming
python scripts/select_clustering.py --rows 5000 --dims 3 --shapes arbitrary --noise --varying-density --jsonScores 11-13 algorithms (k-means, GMM, HDBSCAN, DBSCAN, Ward, spectral, BIRCH, OPTICS, mean shift, plus k-prototypes / Gower for mixed data and k-modes for categorical data) from rows, dimensions, categorical count and the goal flags, and prints why, a starting configuration and cautions for each. Exit codes: 0 printed, 2 usage or read error.
Additional Resources
| File | Content |
|---|---|
| algorithm-guide.md | Assumptions, k, noise, shapes, scale and soft assignment per algorithm; decision questions; typical pipelines; limits of clustering |
| preprocessing-for-clustering.md | Feature choice, scaling, categorical handling, dimensionality reduction, distance metrics, outliers |
| validating-clusters.md | Internal indices and their biases, bootstrap stability, external labels, profiling, business validation |
| clustering-recipes.md | Verified code for each algorithm, mixed-type options, assigning new points |
anomaly-detection-model-builder
Build and evaluate anomaly detection for tabular, sensor and transaction data with few or no labels: define the anomaly type (point, contextual, collective), choose detectors (robust z-score/MAD and IQR rules, Mahalanobis with robust covariance, k-NN distance, LOF, Isolation Forest, one-class SVM, PCA reconstruction / autoencoders, forecasting residuals for time series), combine them by rank, explain each alert by its deviating features, set thresholds from an alert budget or the score distribution, and evaluate with precision@k, PR-AUC and recall on known incidents, or detector agreement when no labels exist. Includes pure-Python detectors (robust z, Mahalanobis, kNN, Isolation Forest, ensemble) and an evaluation script.
catboost-model-builder
Build CatBoost models that use its strengths correctly: raw categorical columns as cat_features (ordered target statistics, no manual encoding), text_features, Pool construction with NaN-safe categories, ordered boosting and has_time for temporal data, symmetric (oblivious) trees, overfitting detector with eval_set and use_best_model, losses for classification, regression (Quantile, Poisson, Tweedie) and ranking (YetiRank), class weights, monotone constraints, feature importance (PredictionValuesChange, LossFunctionChange) and SHAP values, GPU training and model export. Plans cat/text/numeric columns from a CSV and generates a runnable training script, and checks parameter sets for the conflicts CatBoost rejects (iterations vs n_estimators, Bayesian bootstrap with subsample, multiple class-weight options, depth > 16, use_best_model without eval_set).
categorical-encoding-selector
Choose and implement the right encoding for every categorical column given the model family and the data: one-hot (with infrequent-level grouping and handle_unknown), ordinal with an explicit order, cross-fitted target encoding (scikit-learn TargetEncoder, CatBoost ordered statistics, category_encoders inside CV), frequency/count encoding, feature hashing, native categorical support in LightGBM, HistGradientBoosting, XGBoost and CatBoost, prefix levels for codes (zip3, NAICS-2), and embeddings. Measures cardinality, rare-level share, unseen-level rate on a time-ordered holdout and target signal per column, emits the ColumnTransformer, and quantifies target-encoding leakage (in-sample vs out-of-fold vs holdout AUC).
classification-model-selector
Choose the right classification algorithm for tabular, text or mixed data and prove the choice with a fair bake-off: logistic regression (L2, L1, elastic net), linear SVM, naive Bayes, k-NN, RBF SVM, decision trees, random forest, extra trees, HistGradientBoosting, LightGBM, XGBoost, CatBoost, explainable boosting machines (EBM/GAM), points scorecards and MLPs, matched to data size, width, categorical and high-cardinality share, missingness, class imbalance, text, and constraints such as interpretability, monotonic constraints, calibrated probabilities, latency budgets and tiny deployable models. Ranks candidates with starting hyperparameters and cautions, then generates a bake-off script running every candidate on identical CV folds with per-fold metrics, fit time and prediction latency.
collaborative-filtering-implementer
Implement collaborative filtering recommenders that beat popularity: item-based kNN with cosine, Jaccard or asymmetric similarity, shrinkage, neighbourhood size, positive-only or mean-centred rating signals and 'because you liked X' explanations; implicit ALS with confidence weighting, BPR pairwise ranking and the closed-form EASE model in plain NumPy, tuned on a temporal validation split by recall@k and NDCG@k and refit on all data; plus user-kNN, explicit matrix factorisation, the implicit/LightFM libraries, hyperparameter ranges, cold-start fallbacks, scaling with sparse matrices and approximate nearest neighbours. Produces top-k lists in the format of the recommendation evaluator.
counterfactual-explanation-generator
Generate counterfactual explanations and algorithmic recourse: the smallest realistic change to a row's features that changes the model's decision. Solves exact minimal-cost recourse for linear and logistic models (MAD-scaled L1 cost, immutable features, increase/decrease directions, bounds, integer and binary features, diverse alternatives, features every counterfactual must change, and a no-recourse finding when immutable features decide), generates DiCE counterfactuals for any scikit-learn model with constraints turned into permitted ranges and every result re-predicted, and validates counterfactuals from any method for constraint violations, implausible values, off-manifold rows, broken derived-feature relations and failure to flip the prediction.