Clustering, density estimation, and anomaly detection · Partition clustering · UCL006 · Algorithm
K-medians
Partition numeric observations around coordinatewise medians using Manhattan distance. It is especially useful when absolute deviations and coordinatewise robust summaries better represent costs than squared deviations.
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page.
- k-means clustering 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.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
Partition numeric observations around coordinatewise medians using Manhattan distance. It is especially useful when absolute deviations and coordinatewise robust summaries better represent costs than squared deviations.
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
Hard assignment followed by separate coordinatewise median updates.
Assumptions
Coordinate axes matter: rotating features generally changes the solution. Absolute deviations offer reduced sensitivity to extreme coordinates but cannot resolve arbitrary contamination, overlapping groups, or irrelevant dimensions.
Formula / computational definition
Minimize . Given assignments , each coordinate of is a median of that coordinate within cluster ; given centers, assign each point to its nearest center in distance. This is not K-medoids, and Euclidean geometric-median clustering is another objective.
Parameters and notation
Learned or fitted quantities: Median prototype coordinates and labels. User-set hyperparameters/design choices: Set , restart count, scaling, tie handling, and stopping tolerance. 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 the same absolute-distance objective across restarts, examine coordinatewise cluster summaries, and test stability under modest contamination. Silhouette should use Manhattan distance if it is meant to reflect this geometry.
Example application
Group transaction summaries when absolute deviation is the relevant cost. 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
pyclustering documents a K-medians implementation. A short native R loop can alternate apply(cluster_data, 2, median) with Manhattan-distance assignment; both languages need explicit empty-cluster handling. Do not use kmeans with an unrelated distance matrix as a shortcut.
Component: Coordinatewise median and absolute-deviation cost. 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 — Coordinatewise median and absolute-deviation cost. · needs numpy
import numpy as np
X=np.array([[0.,1.],[1.,3.],[20.,4.]])
center=np.median(X,axis=0)
result=np.abs(X-center).sum()
print(result)
Component demo, not a trained model — Coordinatewise median and absolute-deviation cost.
X=rbind(c(0,1),c(1,3),c(20,4))
center=apply(X,2,median)
result=sum(abs(sweep(X,2,center)))
print(result)
Sources
- Source 1pyclustering.github.io
From the CodeSplash reference, entry UCL006.