Problem
An analyst has a customer table with numeric columns (age, balance) and categorical ones (job, marital status, education, loans). Scikit-learn's k-means accepts only numbers, so the categorical columns must be encoded and the dataset drastically altered. The resulting clusters overlap, and the usual metrics give no clear verdict. The business still needs segments it can name and act on.
Example from the article: a public Kaggle banking marketing dataset, 45,211 rows, first 8 columns (age, job, marital, education, default, balance, housing, loan).
Solution in brief
Turn each customer row into one sentence, embed it with a sentence-transformer model, and run k-means on the 384-dimensional vectors. On this dataset the embedding variant gave visibly better-separated clusters than plain k-means and K-Prototype, although the three internal metrics were close. Quality is judged by projections, surrogate-classifier importance and a per-cluster profile table.
Questions this case answers
- How to group entities described by mixed categorical and numeric attributes when k-means cannot take categorical inputs directly
- How to validate and explain an unsupervised grouping that has no ground truth and weak internal metrics
- How to segment customers whose table mixes numeric and categorical attributes?
- How to judge whether a clustering is any good when the metrics are weak and a 2D plot is all you have?
Tools and methods
- k-means — clustering on the one-hot version and on the embedding vectors
- K-Prototype — k-means variant for mixed data (from the
kmodespackage, initHuang) - Sentence Transformers
paraphrase-MiniLM-L6-v2— turns a text description of each row into a 384-dimensional vector - PyOD ECOD — removes outliers before clustering
- PCA, t-SNE, MCA — projections to check separation
- LightGBM + SHAP — surrogate classifier that shows which features define the clusters
Google or a one-time email code. No password.