Skip to content

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

CLARA

Scale medoid clustering by fitting PAM on repeated subsets, then evaluating each candidate medoid set against all observations. It is useful when a full dissimilarity matrix is unaffordable.

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

Scale medoid clustering by fitting PAM on repeated subsets, then evaluating each candidate medoid set against all observations. It is useful when a full dissimilarity matrix is unaffordable.

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

Repeated sample-level PAM fits, followed by full-population assignment and candidate comparison.

Assumptions

The approximation depends on the chance that samples contain adequate representatives of every relevant group. Small rare clusters may be missed even when the overall objective looks good. Stratified sampling changes the design and should be reported; it is not automatically equivalent to ordinary CLARA.

Formula / computational definition

For sample SbS_b, obtain medoids MbM_b with PAM and choose arg⁡min⁡b∑i=1nmin⁡m∈Mbd(xi,xm)\arg\min_b\sum_{i=1}^n\min_{m\in M_b}d(x_i,x_m). Here bb indexes sampling rounds; the final comparison is full-data cost rather than the sample's training cost.

Parameters and notation

Learned or fitted quantities: The winning sampled medoid set and full-data assignments. User-set hyperparameters/design choices: Choose KK, sample size, number of samples, metric, and random seed. 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 cost and rare-group recovery against sample size; compare with PAM on a manageable reference subset. Check that assignment of the full dataset does not create implausibly large or empty groups.

Example application

Cluster a large survey using medoids without storing every pairwise distance. 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 offers cluster::clara. Python can implement its outer sampling and evaluation loop with a verified PAM routine or bridge to R. Preserve the full-data scoring step, which is essential to the method.

Component: Full-data selection of sampled medoid candidates. 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 — Full-data selection of sampled medoid candidates. · needs numpy

import numpy as np
x=np.array([0.,1.,4.,8.]); candidates=[[0.,4.],[1.,8.]]
cost=[np.abs(x[:,None]-np.array(m)).min(1).sum() for m in candidates]
result=np.array(cost)
print(result)
R

Component demo, not a trained model — Full-data selection of sampled medoid candidates.

x=c(0,1,4,8); candidates=list(c(0,4),c(1,8))
result=sapply(candidates,function(m) sum(apply(abs(outer(x,m,"-")),1,min)))
print(result)

Sources

From the CodeSplash reference, entry UCL004.