Clustering, density estimation, and anomaly detection · Partition clustering · UCL013 · Algorithm
Spherical K-means
Cluster directions using cosine similarity, commonly for normalized document or embedding vectors when magnitude should not determine group membership. Each cluster is represented by a unit vector.
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
Cluster directions using cosine similarity, commonly for normalized document or embedding vectors when magnitude should not determine group membership. Each cluster is represented by a unit vector.
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
Unit-length prototypes updated from normalized cluster sums.
Assumptions
The method assumes direction carries the desired signal. Negative coordinates are allowed mathematically, although interpretation differs from nonnegative text counts. Long documents can still change direction through content composition even after normalization.
Formula / computational definition
For unit-norm and , maximize . Assignment selects the largest cosine similarity; center updates normalize the sum of assigned vectors. Equivalently, squared Euclidean distances on the unit sphere can be related to cosine distances, but center renormalization remains essential.
Parameters and notation
Learned or fitted quantities: Directional centers and labels. User-set hyperparameters/design choices: Set , restart count, convergence tolerance, and handling of zero vectors. 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 cosine-based compactness and silhouette, stability, representative terms or neighbors, and downstream utility. A two-dimensional projection is insufficient to establish separation in the original embedding space.
Example application
Group normalized document embeddings by direction. 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 normalized centroid updates directly in NumPy; R provides skmeans. Merely normalizing data once and using ordinary K-means does not exactly enforce unit-norm centers throughout optimization, so label that shortcut accurately.
Component: Spherical centroid renormalization. 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 — Spherical centroid renormalization. · needs numpy
import numpy as np
X=np.array([[1.,0.],[.8,.6]])
center=X.sum(0); center/=np.linalg.norm(center)
result=X@center
print(result)
Component demo, not a trained model — Spherical centroid renormalization.
X=rbind(c(1,0),c(.8,.6)); center=colSums(X); center=center/sqrt(sum(center^2))
result=as.vector(X%*%center)
print(result)
Sources
- Source 1cran.r-project.org
From the CodeSplash reference, entry UCL013.