Skip to content

Clustering, density estimation, and anomaly detection · Partition clustering · UCL012 · Algorithm

K-prototypes

Combine numerical and categorical attributes in one prototype-based partition. Numerical coordinates use means; categorical coordinates use modes, allowing customer or survey tables to retain both data types.

Runs in CodeSplash Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through discover.kmodes. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Combine numerical and categorical attributes in one prototype-based partition. Numerical coordinates use means; categorical coordinates use modes, allowing customer or survey tables to retain both data types.

Question / hypotheses

Which partition best represents the chosen distance-based notion of within-group similarity? This entry defines an estimation or optimization objective, not an intrinsic null/alternative hypothesis test.

Architecture / computational structure

A joint numeric/categorical objective with means and modes stored per cluster.

Assumptions

The relative balance is a modeling choice, not an invariant property of the data. Several correlated numerical measurements can overwhelm a single categorical variable. Euclidean numerical geometry still favors compact clusters.

Formula / computational definition

Minimize ∑i[∑j∈N(xij−μzi,j)2+γ∑j∈C1(xij≠qzi,j)]\sum_i[\sum_{j\in N}(x_{ij}-\mu_{z_i,j})^2+\gamma\sum_{j\in C}\mathbf1(x_{ij}\ne q_{z_i,j})], where N,CN,C are numeric and categorical feature sets and γ\gamma balances their contributions. Alternate assignment and prototype updates.

Parameters and notation

Learned or fitted quantities: Numeric means, categorical modes, and assignments. User-set hyperparameters/design choices: Specify KK, γ\gamma, numeric scaling, initial prototypes, and missing-value handling. Tune these against the stated objective and stability/validation criteria; do not select settings on the final evaluation set.

Fitting, fine-tuning, and adaptation

Neural fine-tuning is not applicable. Refit after changing the metric or scaling. Where the objective permits, warm-start from previous representatives, then compare against independent restarts; assign new cases using the same stored representation and distance.

How to use and calibrate

Show separate numerical and categorical objective components, cluster sizes, and sensitivity to γ\gamma. Inspect whether one variable family drives the whole partition; compare with Gower-distance medoids when mixed-type distances have a stronger scientific justification.

Example application

Group household records containing income and nominal household attributes. Define the reference population, measurement units, and success criterion before fitting; investigate whether discovered groups or alerts reflect the intended phenomenon.

Interpretation / pitfalls

Watch out

The reported result is conditional on the representation and objective. A stable partition can reflect nuisance structure such as batch, collection site, or missingness. Confirm the interpretation in original variables and an independent sample.

Python and R code examples

R clustMixType::kproto supplies a native implementation. Python can reproduce the objective with NumPy and categorical mode updates or call that implementation through a bridge. Apply the same training-derived scaling to later assignments.

Component: Mixed numeric and categorical prototype distance. Python: executed successfully; R: not executed (Rscript unavailable). These small fixtures establish the arithmetic operation, not full-method accuracy or reproduction of paper results.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Mixed numeric and categorical prototype distance. · needs numpy

import numpy as np
numeric=np.array([1.,3.,5.]); category=np.array(['A','A','B'])
center=2.; mode='A'; gamma=4.
result=(numeric-center)**2+gamma*(category!=mode)
print(result)
R

Component demo, not a trained model — Mixed numeric and categorical prototype distance.

numeric=c(1,3,5); category=c('A','A','B'); center=2; mode='A'; gamma=4
result=(numeric-center)^2+gamma*(category!=mode)
print(result)

Sources

From the CodeSplash reference, entry UCL012.