Clustering, density estimation, and anomaly detection · Partition clustering · UCL003 · Algorithm
K-medoids / Partitioning Around Medoids (PAM)
Find representative observed cases under a chosen dissimilarity, including non-Euclidean or mixed-data distances. Unlike centroids, medoids remain actual observations and can be inspected directly.
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page.
- k-medoids (PAM) Verified in Python and R
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
Find representative observed cases under a chosen dissimilarity, including non-Euclidean or mixed-data distances. Unlike centroids, medoids remain actual observations and can be inspected directly.
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 medoid index set, nearest-medoid assignments, and local swap search.
Assumptions
A dissimilarity need not support averaging, but its scale and treatment of missing values must be defensible. Summed unsquared distances reduce the extreme quadratic influence of K-means; medoids are not universally immune to contamination or tiny anomalous groups. Storing a dense dissimilarity matrix is often the main memory bottleneck.
Formula / computational definition
Select indices minimizing . PAM constructs an initial set and evaluates medoid/nonmedoid swaps until no improving swap is found. This gives a local solution, not a general global optimum.
Parameters and notation
Learned or fitted quantities: Selected representative indices and cluster assignments. User-set hyperparameters/design choices: Specify , dissimilarity, initialization, and swap strategy. 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
Use within-medoid cost and silhouette under the same distance, resampling stability, and representative-case review. Compare medoid membership across seeds before assigning substantive meanings.
Example application
Choose representative patients using a clinically justified mixed-data 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
A custom NumPy implementation can exhaustively evaluate swaps for small examples; R provides cluster::pam, including precomputed dissimilarities. R can expose that native implementation to Python through a language bridge when exact PAM behavior is required.
Component: Medoid cost for candidate representatives. 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.
Component demo, not a trained model — Medoid cost for candidate representatives. · needs numpy
import numpy as np
x=np.array([0.,1.,4.,8.]); medoids=x[[0,2]]
result=np.abs(x[:,None]-medoids).min(1).sum()
print(result)
Component demo, not a trained model — Medoid cost for candidate representatives.
x=c(0,1,4,8); medoids=x[c(1,3)]
result=sum(apply(abs(outer(x,medoids,"-")),1,min))
print(result)
Sources
- Source 1cran.r-project.org
From the CodeSplash reference, entry UCL003.