Skip to content

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

Kernel K-means

Find nonlinear partitions by applying K-means in a reproducing-kernel feature space without explicitly constructing that space.

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.spectral. 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 nonlinear partitions by applying K-means in a reproducing-kernel feature space without explicitly constructing that space. It is useful when a defensible similarity kernel captures structure missed by ordinary Euclidean distance.

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 Gram matrix supplies implicit feature-space distances to cluster means.

Assumptions

Dense Gram matrices require quadratic memory. Kernel choice can manufacture apparent separation, and the objective still has local minima. An arbitrary dissimilarity matrix is not automatically a valid kernel.

Formula / computational definition

Minimize ∑i∥ϕ(xi)−mzi∥2\sum_i\|\phi(x_i)-m_{z_i}\|^2. For cluster CC, squared distance is k(xi,xi)−2∣C∣−1∑j∈Ck(xi,xj)+∣C∣−2∑j,l∈Ck(xj,xl)k(x_i,x_i)-2|C|^{-1}\sum_{j\in C}k(x_i,x_j)+|C|^{-2}\sum_{j,l\in C}k(x_j,x_l). Here kk is the kernel and mCm_C the feature-space mean.

Parameters and notation

Learned or fitted quantities: Assignments and implicit feature-space centroids. User-set hyperparameters/design choices: Choose KK, a positive-semidefinite kernel, bandwidth or other kernel parameters, and initial labels. 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 kernel-space cost at fixed kernel, stability across starts, and sensitivity across plausible bandwidths. Validate interpretations in the original variables and on held-out cases where assignment is defined.

Example application

Separate nonlinear sensor regimes with a justified similarity kernel. 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 the displayed Gram-matrix distance formula with NumPy or base R matrix operations. This is a custom implementation route; ordinary KMeans/kmeans on rows of the Gram matrix optimizes a different geometry.

Component: Kernel-space distance to one cluster. 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 — Kernel-space distance to one cluster. · needs numpy

import numpy as np
x=np.array([0.,1.,3.]); K=np.exp(-((x[:,None]-x)**2)/2)
members=np.array([0,1])
result=np.diag(K)-2*K[:,members].mean(1)+K[np.ix_(members,members)].mean()
print(result)
R

Component demo, not a trained model — Kernel-space distance to one cluster.

x=c(0,1,3); K=exp(-outer(x,x,'-')^2/2); members=c(1,2)
result=diag(K)-2*rowMeans(K[,members])+mean(K[members,members])
print(result)

Sources

From the CodeSplash reference, entry UCL014.