-
Notifications
You must be signed in to change notification settings - Fork 2
/
Assignment_2_2_6.m
71 lines (66 loc) · 2.03 KB
/
Assignment_2_2_6.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
clear; clc;
close all;
data = xlsread('dataset.xlsx');
%Input normalization
data(:,1:end-1) = (data(:,1:end-1)-mean(data(:,1:end-1)))./std(data(:,1:end-1));
X = data(:,1:end-1); %Inputs
Y = data(:,end); %Target outputs
%Converting target output to two output neurons
for i=1:length(Y)
if Y(i)==1
z(i,:) = [1 0];
else
z(i,:) = [0 1];
end
end
%Randomly divide the dataset into training (70%) and testing (30%) set
%p = randperm(length(Y));
C = cvpartition(Y,'HoldOut',0.3);
tr = C.training;
te = C.test;
trainInput = X(tr,:);
testInput = X(te,:);
trainOutput = z(tr,:);
testOutput = z(te,:);
%trainInput = X(p(1:0.7*size(X,1)),:); trainOutput = z(p(1:0.7*size(X,1)),:);
%testInput = X(p(0.7*size(X,1)+1:end),:); testOutput = z(p(0.7*size(X,1)+1:end),:);
k = 600; %Hidden neurons
[ind,c] = kmeans(trainInput,k); %kmeans clustering centres and indices
n = zeros(k,1); %number of inputs belonging to each cluster
for i=1:k
n(i) = sum(ind(:)==i);
end
sigma = zeros(k,1); %standard deviation
for i=1:k
sigma(i) = norm(trainInput(ind(:)==i,:)-c(i))/n(i);
end
%Linear Kernel Function
%Hidden layer matrix evaluation
for i=1:length(trainOutput)
for j=1:size(c,1)
H(i,j) = (norm(trainInput(i,:)-c(j,:)));
end
end
W = pinv(H)*trainOutput; %Weight evaluation
%Test data evaluation
for i=1:length(testOutput)
for j=1:size(c,1)
Ht(i,j) = (norm(testInput(i,:)-c(j,:)));
end
end
yp = Ht*W; %Output evaluation
%Class determination
[~,pb]=max(testOutput,[],2);
[~,pa]=max(yp,[],2);
[cm, ~] = confusionmat(pa,pb); %calculating confusion matrix
IA = zeros(1,2);
OA = 0;
for i = 1:2
IA(i) = cm(i,i)/sum(cm(i,:)); %individual accuracy
end
for i=1:2
OA= OA + cm(i,i);
end
OA = (OA/sum(cm(:)))*100; %overall accuracy
display(cm);
display(OA);