K-means++ Algorithm | MATLAB
%Code:
clc
clear all
close all
z=["Rob",27,70000;
"Michael",29,90000;
"Mohan",29,61000;
"Ismail",28,60000;
"Kory",42,150000;
"Gautam",39,155000;
"David",41,160000;
"Andrea",38,162000;
"Brad",36,156000;
"Angelina",35,130000;
"Donald",37,137000;
"Tom",26,45000;
"Arnold",27,48000;
"Jared",28,51000;
"Stark",29,49500;
"Ranbir",32,53000;
"Dipika",40,65000;
"Priyanka",41,63000;
"Nick",43,64000;
"Alia",39,80000;
"Sid",41,82000;
"Abdul",39,58000];
x=double(z(:,2));
y=double(z(:,3));
plot(x,y,'b.','linewidth',2);
xlabel('Age');
ylabel('Income');
title('Income
Dataset');
hold on;
m=[];
k=input('Enter
the k value of k means:');
pos = randi(length(x));
startx=x(pos);
starty=y(pos);
center=[startx starty];
d=[];
r=1;
while(r~=k)
for
i=1:length(x)
g=[x(i) y(i)];
ka=dsearchn(center,g);
nearestx=center(ka,1);
nearesty=center(ka,2);
distance=sqrt((nearestx-g(1))^2+(nearestx-g(1))^2);
d=[d distance];
end
[e s]=max(d);
center=[center;[x(s) y(s)]];
x(s)=[];
y(s)=[];
[r c]=size(center);
d=[];
end
disp(center);
plot(center(:,1),center(:,2),'ko','linewidth',3);
grid on;
hold off;
x=double(z(:,2));
y=double(z(:,3));
cx=center(:,1);
cy=center(:,2);
mean_oldx=cx;
mean_newx=cx;
mean_oldy=cy;
mean_newy=cy;
outputx=cell(k,1);
outputy=cell(k,1);
temp=0;
while(temp==0)
mean_oldx=mean_newx;
mean_oldy=mean_newy;
for ij=1:length(x)
mina=[];
mu=x(ij);
nu=y(ij);
for mk=1:length(cx)
mina=[mina
sqrt((mu-cx(mk))^2+(nu-cy(mk))^2)];
end
[gc index]=min(mina);
outputx{index}=[outputx{index} mu];
outputy{index}=[outputy{index} nu];
end
gmckx=[];
gmcky=[];
for i=1:k
gmckx=[gmckx
mean(outputx{i})];
gmcky=[gmcky
mean(outputy{i})];
end
cx=gmckx;
cy=gmcky;
mean_newx=cx;
mean_newy=cy;
gum=0;
bum=0;
if(mean_newx==mean_oldx)
gum=1;
end
if(mean_newy==mean_oldy)
bum=1;
end
if(gum==1 && bum==1)
temp=1;
else
outputx=cell(k,1);
outputy=cell(k,1);
end
end
celldisp(outputx);
celldisp(outputy);
figure;
for
i=1:k
x=outputx{i};
y=outputy{i};
if(i==1)
plot(x,y,'r*','linewidth',3);
elseif(i==2)
plot(x,y,'bo','linewidth',3);
else
plot(x,y,'g+','linewidth',3);
end
hold on;
grid on;
end
xlabel('Age');
ylabel('Income');
title('kmean
clustering on Income data');
No comments