Code covered by the BSD License  

Highlights from
Kernel k-means

4.2 | 6 ratings Rate this file 54 Downloads (last 30 days) File Size: 17.3 KB File ID: #26182 Version: 1.7
image thumbnail

Kernel k-means


Mo Chen (view profile)


23 Dec 2009 (Updated )

kernel k-means algorithm

| Watch this File

File Information

This function performs kernel version of kmeans algorithm. When the linear kernel (i.e., inner product) is used, the algorithm is equivalent to standard kmeans algorithm.

K: n x n a semi-definite matrix computed by a kernel function on all sample pairs
m: the number of clusters k (1 x 1) or the initial label of samples (1 x n, 1<=label(i)<=k)

reference: [1] Kernel Methods for Pattern Analysis
by John Shawe-Taylor, Nello Cristianini

sample code:
load data;
K=x'*x; % use linear kernel

MATLAB release MATLAB 7.9 (R2009b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (18)
28 Jul 2015 usman shahzad

can anyone send me the full code on kernel k-means in matlab?

Comment only
28 Jun 2015 Xiaodong

Can this code return the cluster centers?

10 May 2015 mohammad ghr

how to plot the original data with color?

14 Apr 2015 Jesus G Cruz-Garza

Jung, Tajana, I found the same problem in my Matlab version R2014b.
The function "unique" on line 18 changes the variable "label" from a row vector to a column vector.
This caused the error in my case.

To fix it, transpose the variable "label" after line 18.

label = label'.

Hope it helps

Comment only
01 Apr 2015 Zachery Hernandez

Jung and Tajana, I ran the sample code without any problems but I'm wondering if your data array is the right size (2 X 1000).

In either case, you might want to see if the 'any' function in your MATLAB version can compare a vector to a scalar or to another vector. If you get the same 'Matrix dimensions must agree' error then that may be the problem.

Comment only
24 Mar 2015 Tajana Schneiderman

Jung, were you able to resolve this issue? I'm also having quite a bit of trouble with this, and can't resolve the error.

Comment only
21 Aug 2014 Jung Soo Park


I get following error message when pasted 'sample code' in code description? thanks.

>> load data;
K=x'*x; % use linear kernel
Error using ~=
Matrix dimensions must

Error in knkmeans (line 26)

Comment only
21 Aug 2014 Jung Soo Park  
07 Jun 2012 Ajay Singh  
02 Apr 2012 Santiago  
10 Jan 2012 Mo Chen

Mo Chen (view profile)

Hi, Phillip,
You computation is right, only not very efficient. Check my new code

Comment only
11 Apr 2011 Phillip

Hello I encountered the same problem as john luckily i had the book, I added the following code. The energy is the sum squared clustering cost function. I have been optimizing my kernel hyper-parameters to minimise this energy. Been working fairly well so thanks. Not an expert so could be wrong.

for i=1:1:size(K,1)
energy = trace(K)-trace(sqrt(D)*A'*K*A*sqrt(D));

Comment only
15 Nov 2010 John Melonakos

The code appears broken to me:

>> load data;
K=x'*x; % use linear kernel
??? Undefined function or variable 'val'.

Error in ==> knkmeans at 31
energy = sum(val)+trace(K);

Comment only
20 Mar 2010 Mo Chen

Mo Chen (view profile)

Hi mathieu,
As indicated in the description, this algorithm is explained in
reference: [1] Kernel Methods for Pattern Analysis
by John Shawe-Taylor, Nello Cristianini

Comment only
23 Feb 2010 Seyed Salim

Mathieu, you can refer to machine learning and pattern recognition by Bishop, 2005. Alternatively this is for free:

Comment only
23 Feb 2010 Mathieu

I see, reading the code I do not manage to understand what are the principles behind the algorithm. Do you have a reference that I could get from the web or do you advise to buy the book ?

Comment only
11 Feb 2010 Mo Chen

Mo Chen (view profile)

This happens for standard kmeans too, which is caused by the nature of the algorithm. The reason is that when you set a very big number for k, after several iterations, some clusters might become empty.

Comment only
11 Feb 2010 Mathieu

It seems that if I request N clusters, the algorithms outputs k clusters, k<=N clusters and most of the time k<<N. I was wondering if this is by construction. If yes, could provide me with an explanation ?

25 Dec 2009 1.1

add sample data and detail description

30 Sep 2010 1.2

remove empty clusters

03 Feb 2012 1.5

fix a minor bug of returning energy

03 Feb 2012 1.6


03 Feb 2012 1.7

Improve the code and fix a bug of returning energy

Contact us