You are on page 1of 6

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/286775150

k-means clustering using matlab

Code · December 2015


DOI: 10.13140/RG.2.1.3104.1369

CITATIONS READS
0 1,536

1 author:

Dalia Sami Jasim


Universiti Kebangsaan Malaysia
9 PUBLICATIONS   43 CITATIONS   

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Timetable Scheduling For Secondary Schools in Iraq View project

All content following this page was uploaded by Dalia Sami Jasim on 13 December 2015.

The user has requested enhancement of the downloaded file.


K-Means Program
Dalia Sami – GP03146

Dataset : seeds
Programming language: MATLAB

Results

cluster1 correct incorrect class

kama 59 10 1

*******************************************************************
cluster2 correct incorrect class

rosa 65 2 2

*******************************************************************
cluster3 correct incorrect class

Canadian 67 10 3

********************************************************************
clear ; close all; clc
data = load('seed.csv');
class=load('class.csv');
rows=size(data,1);
columns=size(data,2);

%identify clusters
cluster1=[];classify1=[];
cluster2=[];classify2=[];
cluster3=[];classify3=[];

% determine centroids
cent1 = data(58,:);
cent1=cent1(:);
cent2 = data(132,:);
cent2=cent2(:);
cent3 = data(175,:);
cent3=cent3(:);

index1=0;index2=0;index3=0;
n=0;

for k = 1:rows

c = data(k,:);
b=c(:);
cl=class(k,1);
result1=sqrt(sum((cent1-b).^2));
result2=sqrt(sum((cent2-b).^2));
result3=sqrt(sum((cent3-b).^2));

min1=min(result1,result2);
min2=min(min1,result3);

if min2 == result1
index1 = index1 + 1; cluster1(index1,:)=c;classify1(index1,1)=cl;
elseif min2 == result2
index2 = index2 + 1;cluster2(index2,:)=c;classify2(index2,1)=cl;
else
index3 = index3 + 1;cluster3(index3,:)=c;classify3(index3,1)=cl;
end

end

while n <=90
%calculate new centriods
for i=1:columns
cent1(i,1)=sum(cluster1(:,i))/index1;
cent2(i,1)=sum(cluster2(:,i))/index2;
cent3(i,1)=sum(cluster3(:,i))/index3;
end
cluster1=[];cluster2=[];cluster3=[];
n=n+1;
index1=0;index2=0;index3=0;

for k = 1:rows

c = data(k,:);
b=c(:);
result1=sqrt(sum((cent1-b).^2));
result2=sqrt(sum((cent2-b).^2));
result3=sqrt(sum((cent3-b).^2));

min1=min(result1,result2);
min2=min(min1,result3);

if min2 == result1
index1 = index1 + 1; cluster1(index1,:)=c;
elseif min2 == result2
index2 = index2 + 1;cluster2(index2,:)=c;
else
index3 = index3 + 1;cluster3(index3,:)=c;
end

end
end

[t,f,c]= classifycorrection( classify1 );


fprintf('%12s %24s\r\n','cluster1');
fprintf('%12s %12s \r\n','correct','incorrect','class');
fprintf('%10s %10s','kama');
fprintf( '%20d %12d \r\n',f,c);
display('**************************************************************
*****')
[t,f,c]= classifycorrection( classify2 );
fprintf('%12s %24s\r\n','cluster2');
fprintf('%12s %12s %12s\r\n','correct','incorrect','class');
fprintf('%10s %10s','rosa');
fprintf( '%22d %12d \r\n',f,c);
display('**************************************************************
*****')
[t,f,c]= classifycorrection( classify3 );
fprintf('%12s %24s\r\n','cluster3');
fprintf('%12s %12s %12s\r\n','correct','incorrect','class');
fprintf('%12s %12s','Canadian');
fprintf( '%20d %12d\r\n',f,c);
display('**************************************************************
******')

figure
hold on

scatter(cluster1(:,1),cluster1(:,2),cluster1(:,3),cluster1(:,4),'b','o'
,'LineWidth',5)
scatter(cluster2(:,1),cluster2(:,2),cluster2(:,3),cluster2(:,4),'r','+'
,'LineWidth',5)
scatter(cluster3(:,1),cluster3(:,2),cluster3(:,3),cluster3(:,4),'g','*'
,'LineWidth',5)
scatter(cent1(1,1),cent1(2,1),cent1(3,1),cent1(4,1),'k','+','LineWidth'
,5)
scatter(cent2(1,1),cent2(2,1),cent2(3,1),cent2(4,1),'k','+','LineWidth'
,5)
scatter(cent3(1,1),cent3(2,1),cent3(3,1),cent3(4,1),'k','+','LineWidth'
,5)

legend('cluster1','cluster2','cluster3');
hold off;

function [corr,incorr,cl]= classifycorrection( y )


%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
raw=size(y,1);
first=[];second=[];third=[];
first(1,1)=1;second(1,1)=2;index1=1;index2=1;index3=0;
for i=2:raw
if y(i,1)==first(1,1)
index1=index1+1; first(index1,1)=y(i,1);
elseif y(i,1)==second(1,1)
index2=index2+1; second(index2,1)=y(i,1);
else
index3=index3+1; third(index3,1)=y(i,1);
end
end

max1=max(size(first,1),size(second,1));
max2=max(max1,size(third,1));
if max2==size(first,1)
corr=size(first,1);
incorr=size(second,1)+size(third,1);
cl=first(1,1);
elseif max2==size(second,1)
corr=size(second,1);
incorr=size(first,1)+size(third,1);
cl=second(1,1);
else
corr=size(third,1);
incorr=size(first,1)+size(second,1);
cl=third(1,1);
end

end

View publication stats

You might also like