Skip to content

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

Trimmed K-means

Fit compact clusters while deliberately excluding a specified fraction of the largest residuals. This provides a robust clustering objective when a small contamination fraction is plausible.

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.kmeans. 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

Fit compact clusters while deliberately excluding a specified fraction of the largest residuals. This provides a robust clustering objective when a small contamination fraction is plausible.

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

Alternating center updates and selection of the h smallest assignment residuals.

Assumptions

Trimming assumes unwanted cases have large residuals relative to meaningful clusters; a legitimate small cluster can be discarded. Conversely, a dense anomalous group may acquire its own center. Estimated contamination is not a test with a controlled false-positive rate.

Formula / computational definition

Minimize ∑i∈Hmin⁡k∥xi−μk∥2\sum_{i\in H}\min_k\|x_i-\mu_k\|^2 over centers and a retained set HH of size h=⌊(1−α)n⌋h=\lfloor(1-\alpha)n\rfloor. The trimming fraction α\alpha determines how many observations do not contribute to fitting. Alternating assignment, trimming, and centroid updates requires multiple initializations.

Parameters and notation

Learned or fitted quantities: Retained subset, cluster centers, and assignments. User-set hyperparameters/design choices: Choose KK, α\alpha, scaling, and the search budget. 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

Plot retained cost, excluded cases, and cluster stability over plausible α\alpha. Audit excluded subgroups before interpreting them as errors. Compare to untrimmed K-means and robust medoid alternatives.

Example application

Fit operating modes while allowing a prespecified fraction of corrupted rows. 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

Implement a transparent trimming loop in NumPy or R for teaching; robust R clustering is catalogued in CRAN's Cluster task view, including tclust. General trimmed likelihood routines need their constraints chosen to reproduce trimmed K-means exactly.

Component: Trim the largest supplied squared residuals. 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 — Trim the largest supplied squared residuals. · needs numpy

import numpy as np
residual=np.array([.1,.2,.4,2.,30.]); alpha=.2
h=int(np.floor((1-alpha)*len(residual)))
result=np.sort(residual)[:h].sum()
print(result)
R

Component demo, not a trained model — Trim the largest supplied squared residuals.

residual=c(.1,.2,.4,2,30); alpha=.2
h=floor((1-alpha)*length(residual)); result=sum(sort(residual)[seq_len(h)])
print(result)

Sources

From the CodeSplash reference, entry UCL010.