Decision Boundaries for kmeans clusters.

10 views (last 30 days)
Kawther
Kawther on 30 Nov 2014
Answered: Tom Lane on 7 Dec 2014
I have the below code. I want to find the decision boundaries of the obtained clusters. The code does not work with me. can you please help me? any support would be appreciated.
clear all
clc
load fisheriris
labels = (1:1200);
T=[ 2+2*i 2-2*i -2+2*i -2-2*i];
A=randn(150,2)+2*ones(150,2); C=randn(150,2)-2*ones(150,2);
B=randn(150,2)+2*ones(150,2); F=randn(150,2)-2*ones(150,2);
D=randn(150,2)+2*ones(150,2); G=randn(150,2)-2*ones(150,2);
E=randn(150,2)+2*ones(150,2); H=randn(150,2)-2*ones(150,2);
X = [A; B; D; C; F; E; G; H];
[idx, centroids] = kmeans(X, 4, 'Replicates', 20);
x = X(:,1);
y = X(:,2);
classifierType = 'quadratic'; %# 'quadratic', 'linear'
npoints=100;
mn = min(X); mx = max(X);
[I,J] = meshgrid( linspace(mn(1),mx(1),npoints), linspace(mn(2),mx(2),npoints));
[K,L] = meshgrid( linspace(mn(1),mx(1),npoints), linspace(mn(2),mx(2),npoints));
I = I(:); J = J(:); K = K(:); L = L(:);
[C,err,P,logp,coeff] = classify([I J K L], X, labels, classifierType);
figure;
hold on;
colors = 'rgbk';
for num = 1 : 4
plot(x(idx == num), y(idx == num), [colors(num) '.']);
end
plot(centroids(:,1), centroids(:,2), 'c.', 'MarkerSize', 14);
NUM_K=4;
%# draw decision boundaries between pairs of clusters
for i=1:NUM_K
for j=i+1:NUM_K
if strcmp(coeff(i,j).type, 'quadratic')
K = coeff(i,j).const;
L = coeff(i,j).linear;
Q = coeff(i,j).quadratic;
f = sprintf('0 = %g + %g*x + %g*y + %g*x^2 + %g*x.*y + %g*y.^2',...
K,L,Q(1,1),Q(1,2)+Q(2,1),Q(2,2));
else
K = coeff(i,j).const;
L = coeff(i,j).linear;
f = sprintf('0 = %g + %g*x + %g*y', K,L(1),L(2));
end
h2 = ezplot(f, [mn(1) mx(1) mn(2) mx(2)]);
set(h2, 'Color','k', 'LineWidth',2)
end
end
grid;
Thank you.

Answers (1)

Tom Lane
Tom Lane on 7 Dec 2014
You probably didn't mean to define 1200 distinct labels.
You probably didn't mean to pass into classify a first argument with four dimensions and a second with two dimensions.
You may have intended to do both kmeans and discriminant analysis and compare the results, I'm not sure, but in any case you should recognize that they are different. For example, if you intend to do discriminant analysis on the basis of the four clusters found by kmeans, I would expect you to use idx in place of labels when you call classify.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!