-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmeansClustering.m
78 lines (51 loc) · 1.22 KB
/
kmeansClustering.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
clear;
clc;
close all;
rand('seed',0);
randn('seed',0);
load iris1;
y = y+1;
N = length(y); % numero patrones
M = max(y); % numero de clases
K = M;
first = 1;
last = 4;
N_features = 4;
step = round(last/N_features);
i = 1;
for j = first:step:last
xtst(i,:)=x(j,:);
i = i+1;
end
for i=1:M
ind = find(y==i);
C{i} = covpat(xtst(:,ind));
end
%centroides = kmeans(x1,K);
centroides1 = kmeans(x(1:2,:),K);
centroides2 = kmeans(x(3:4,:),K);
centroides = kmeans(x,K);
for i=1:K
CC{i} = centroides(:,i);
end
for i=1:K
d(i,:) = d_mahal(xtst,CC{i},C{i});
end
[~,clase] = min(d);
c1= find(y == 1);
c2 = find(y == 2);
c3 = find(y == 3);
matriz_confusion = [100*sum(y(c1)==clase(c1))/length(c1);
100*sum(y(c2)==clase(c2))/length(c2);
100*sum(y(c3)==clase(c3))/length(c3)]
100*sum(y==clase)/N
%%
hold on;
color=['rgbkymc'];
for i=1:M
ind = find(y==i);
plot(x(1,ind),x(2,ind),['.' color(i)]);hold on
plot(x(3,ind),x(4,ind),['.' color(i)]);
end
plot(centroides1(1,:),centroides1(2,:),'.b','MarkerSize',30);
plot(centroides2(1,:),centroides2(2,:),'.b','MarkerSize',30);