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';
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;
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;