clear;
clf;
close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% K Means %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
EPSILON=0.0001; %%% If 0/0, choose average vector
n=200; %%% Training samples
K=8; %%% Learning Components
CYCLE=20; %%% Repeated Time
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%% Choose SAMPLE Distributions %%%%%%%%%%%%
TYPESET=6; %%% Training Sample Type
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(TYPESET==1) %%%%%%%%%%% (1) Uniform %%%%
x=rand(2,n);
end
if(TYPESET==2) %%%%%%%%%% (2) Gaussian
x=[0.1,0.12;0.12,0.1]*randn(2,n)+0.5;
end
if(TYPESET==3) %%%%% (3) Gaussian mixtue %%%%%
x0=[0,1,0,1,0.5;0,0,1,1,0.5];
for i=1:1:n
j=1+floor(5*rand(1,1));
x(:,i)=x0(:,j);
end
x=x+0.05*randn(2,n);
end
if(TYPESET==4) %%%%%%%%%% (4) line %%%
x(1,:)=rand(1,n);
x(2,:)=4*(0.5-x(1,:)).^2;
end
if(TYPESET==5) %%%% (5) circle %%%
x(1,:)=rand(1,n);
x(2,:)=sign(2*rand(1,n)-1).*sqrt(0.25-(x(1,:)-0.5).^2)+0.5;
end
if(TYPESET==6)
x(1,:)=rand(1,n);
x(2,:)=0.3*sin(2*pi*x(1,:))+0.05*randn(1,n);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%% Matrix definition %%%%%%%%%
recordy=zeros(CYCLE,2,K); %%% Recording
ID1=ones(1,K);
ID2=ones(n,1);
%%%%%%%%%%%%%%%%%%%%%% Learning Machine and Record %%%%%%%%%%
y=rand(2,K);
recordy(1,:,:)=y;
%%%%%%%%%%%%%%%%%%%%%% Learning Begin %%%%%%%%%%%%%%%
for j=2:1:(CYCLE+1)
c=zeros(n,K);
E=0;
for i=1:1:n
yy=y-x(:,i)*ID1;
dist=sum(yy.*yy,1);
[minval, k]=min(dist);
E=E+dot(x(:,i)-y(:,k),x(:,i)-y(:,k));
c(i,k)=1;
end
fprintf('E[%2g]=%.3f\n',j-1,E);
d=(c+EPSILON*ID2*ID1)./(ID2*sum(c)+EPSILON*n);
y=x*d;
recordy(j,:,:)=y;
end
%%%%%%%%%%%%%%%%%%%%%% Learning End %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% Patition Numbers %%%%%%%%%%%%%%%%
fprintf('Partition Numbers: ')
for k=1:1:K
fprintf('%g ',sum(c(:,k)));
end
fprintf('\n');
%%%%%%%%%%%%%%%%%%%%%% Draw graph %%%%%%%%%%%%%%%%%%%
figure(1);
plot(x(1,:),x(2,:),'bx'); hold on;
title('x: sample');
figure(2);
plot(x(1,:),x(2,:),'bx'); hold on;
for k=1:1:K
plot(recordy(1,1,k),recordy(1,2,k),'ro'); hold on;
plot(recordy(:,1,k),recordy(:,2,k),'ro-'); hold on;
if(sum(c(:,k))==0)
plot(recordy(CYCLE+1,1,k),recordy(CYCLE+1,2,k),'gs'); hold on;
else
plot(recordy(CYCLE+1,1,k),recordy(CYCLE+1,2,k),'rs','MarkerFaceColor','g'); hold on;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
title('K-Mean. x: sample, o: start, square: goal');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(3);
plot(x(1,:),x(2,:),'bx'); hold on;
for k=1:1:K
if(sum(c(:,k))==0)
plot(recordy(CYCLE+1,1,k),recordy(CYCLE+1,2,k),'gs'); hold on;
else
plot(recordy(CYCLE+1,1,k),recordy(CYCLE+1,2,k),'rs','MarkerFaceColor','r'); hold on;
end
end
voronoi(y(1,:),y(2,:));
title('K-Mean and Voronoi');