-
Notifications
You must be signed in to change notification settings - Fork 2
/
KM+LS.m
96 lines (83 loc) · 2.36 KB
/
KM+LS.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
clc;clear all;close all;
load('Train.mat');
load('GroundTruth.mat');
load('Test.mat');
load('TrueClass.mat');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% FINDING CENTERS
N = 60000;
K = 100;
ClosestCentroids = zeros(1,N);
centres = datasample(Train,K,1,'Replace',false);
centres_new = zeros(K,785);
Iteration = 1;
while(true)
for i=1:N
[value index] = min(sum(sqrt((bsxfun(@minus,Train(i,:),centres)).^2),2));
ClosestCentroids(i) = index;
end
for i=1:K
centres_new(i,:)= mean(Train(i == ClosestCentroids,:));
end
a = corrcoef(centres,centres_new);
if(a(2,1)>0.99)
break;
else
centres = centres_new;
end
Iteration = Iteration + 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% FINDING VARIANCES
d_max = 0;
for i = 1:K-1
my_dist(i) = sum((centres_new(i,:)-centres_new(i+1,:)).^2);
if my_dist(i) > d_max
d_max = my_dist(i);
else
end
end
% Normalization method from the slides
spread = (d_max/sqrt(2*K))*ones(K,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% FINDING WEIGHTS
centres_new = centres_new';
load('Train.mat');
load('GroundTruth.mat');
load('Test.mat');
load('TrueClass.mat');
w = rand(10,K)./2 - 0.25;
g = 0;
for epoch = 1:1
for m = 1:length(Train)
x = Train(m,:);
d = GroundTruth;
% Gaussian RBF Kernel, or RBF Kernel: https://en.wikipedia.org/wiki/Radial_basis_function_kernel
% Applying Kernel on the Test Data
for i = 1:K
g(i,m) = exp(-(x-centres_new(:,i)')*(x-centres_new(:,i)')'/(2*spread(i)^2));
end
end
% Implementation of the Least Squares algorithm
w=pinv(g)'*d;
end
% Applying Kernel on the Test Data
for m = 1:length(Test)
x = Test(m,:);
for i = 1:K
g_test(i,m) = exp(-(x-centres_new(:,i)')*(x-centres_new(:,i)')'/(2*spread(i)^2));
end
end
% Predicting the values and finding Classification Rate.
PREDICTIONS = (g_test'*w);
for z = 1:length(Test)
[val, col] = max(PREDICTIONS(z,:));
predict(z,:) = col-1;
end
CR = (sum(predict==TrueClass)/length(TrueClass))*100
% Visualizations
figure;
plot(1:epoch,CR,'b--o');
title('Recognition Curve');
xlabel('Number of epochs');
ylabel('Classification Rate (%)');