Skip to content

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

CLARANS

Search the medoid solution space using randomized neighbor exploration. A solution is a set of KK medoids; adjacent solutions exchange one medoid for one nonmedoid. This differs from CLARA's repeated data subsampling.

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

Search the medoid solution space using randomized neighbor exploration. A solution is a set of KK medoids; adjacent solutions exchange one medoid for one nonmedoid. This differs from CLARA's repeated data subsampling.

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

Random moves in a graph whose vertices are medoid sets and edges exchange one representative.

Assumptions

More search can improve cost but does not certify optimality. Large datasets still require many distance evaluations; caching can exchange memory for speed. Results depend on initial medoids and the random search trajectory.

Formula / computational definition

Minimize J(M)=∑imin⁡m∈Md(xi,xm)J(M)=\sum_i\min_{m\in M}d(x_i,x_m) while sampling candidate swaps. Accept an improving neighbor, reset the failed-neighbor counter, and restart after a specified search budget. The random search trades exhaustive PAM swap evaluation for exploration.

Parameters and notation

Learned or fitted quantities: Accepted medoid indices and resulting memberships. User-set hyperparameters/design choices: Control KK, maximum neighbors tested, number of local searches, and metric. 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

Compare cost distributions over independent runs rather than a single favorable seed. Report runtime, distance evaluations when available, and medoid stability. Use identical dissimilarities when comparing with PAM or CLARA.

Example application

Search alternative medoid sets when exhaustive swaps are costly. 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

The scikit-clarans project documents a native Python implementation. R requires a custom swap-search implementation or a Python bridge; do not substitute clara and label it CLARANS.

Component: Acceptance of one proposed medoid swap. 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 — Acceptance of one proposed medoid swap. · needs numpy

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

Component demo, not a trained model — Acceptance of one proposed medoid swap.

x=c(0,1,4,8)
cost=function(m) sum(apply(abs(outer(x,m,"-")),1,min))
result=c(old=cost(c(0,4)),new=cost(c(1,8)),accepted=cost(c(1,8))<cost(c(0,4)))
print(result)

Sources

From the CodeSplash reference, entry UCL005.