-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRandomCenters+LS.m
77 lines (65 loc) · 2.03 KB
/
RandomCenters+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
clc;clear all;close all;
load('Train.mat');
load('GroundTruth.mat');
load('Test.mat');
load('TrueClass.mat');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% FINDING CENTERS
N = 60000;
K = 100;
indices = randperm(60000, K);
centres_new = Train(indices, :);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% 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 (%)');