-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosine.m
69 lines (58 loc) · 1.49 KB
/
cosine.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
X=csvread('C:\Users\Devanshu\OneDrive\Data Mining\HW3\onlydata2.csv');
a=zeros(99,1);
s=zeros(99,1);
d=zeros(99,1);
normX = zeros(8560,1);
normC = zeros(8560,1);
for i = 1:8560
normX(i) = norm(X(i),2);
end
for k = 2:100
[idx,C,sumd,D] = kmeans(X,k,'Distance','cosine','Options',statset('UseParallel',1),'MaxIter',1000,'Replicates',1);
incluster = zeros(k,1);
betweencluster = zeros(k,1);
count = zeros(k,1);
%Incluster similarity
for j = 1:8560
i = idx(j);
for m = 1:8560
%temp = (dot(X(j),X(m)));
temp = 1-(dot(X(j),X(m)))/(normX(j)*normX(m));
incluster(i)=incluster(i)+temp;
count(i)=count(i)+1;
end
end
for i = 1:k
incluster(i)=incluster(i)/count(i);
%fprintf('%16.f',incluster(i));
%disp(incluster(i));
end
s(k) = sum(incluster)/(k-1);
%Between Cluster
for j = 1:k
for m = 1:k
%temp = (dot(C(j),C(m)));
temp = 1- (dot(C(j),C(m)))/(norm(C(j),2)*norm(C(m),2));
betweencluster(j)=betweencluster(j)+temp;
end
end
for i = 1:k
incluster(i)=incluster(i)/k;
%fprintf('%16.f',incluster(i));
%disp(incluster(i));
end
d(k) = sum(betweencluster)/(k-1);
%disp('S : ');
%disp(s);
%disp('D :');
%disp(d);
disp(k);
a(k)=s(k)/d(k);
end
disp('Dot product');
plot(s);
saveas(gcf,'dot_s.jpg');
plot(d);
saveas(gcf,'dot_d.jpg');
plot(a);
saveas(gcf,'dot_a.jpg');