-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.m
105 lines (79 loc) · 2.64 KB
/
demo.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
97
98
99
100
101
102
103
104
105
%% Demos for the BIRCH algorithm
clc;
clear;
% Select a data set
dataset = load('electricity');
fns = fieldnames(dataset);
[ X, Y ] = divideTable( dataset.(fns{1}) );
param.DistanceMetric = 'euclidean';
param.NumOfNeighbors = 1;
ACC = demo1(X, Y, 'KNN', param);
[ idx, newX, newY, R, T ] = demo2( X, Y, param );
[ idx2, newX2, newY2, R2, T2 ] = demo3( X, Y );
clear dataset;
clear fns;
%%
function [ ACC ] = demo1( X, Y, classifier, param )
predictions = repmat(Y, 1, 2);
indices = crossvalind('Kfold', Y, 10);
for i = 1:10
fprintf('%d',i);
test = (indices == i);
train = ~test;
trainY = Y(train,:);
trainX = X(train,:);
testX = X(test,:);
idx = BIRCH(trainX, trainY, param);
newTrainX = trainX(idx, :);
newTrainY = trainY(idx);
switch classifier
case 'KNN'
Mdl = fitcknn(newTrainX, newTrainY, 'NumNeighbors', 1);
case 'CART'
Mdl = fitctree(newTrainX, newTrainY);
predictions(test, 2) = predict(Mdl, testX);
case 'NB'
% "normal", "mn", "kernel", "mvmn".
Mdl = fitcnb(newTrainX, newTrainY, 'DistributionNames', 'normal');
predictions(test, 2) = predict(Mdl, testX);
case 'SVM'
% "linear", "gaussian", "rbf", "polynomial"
t = templateSVM('Standardize', true, 'KernelFunction', 'linear');
Mdl = fitcecoc(newTrainX, newTrainY, 'Learners', t);
predictions(test, 2) = predict(Mdl, testX);
end
predictions(test, 2) = predict(Mdl, testX);
end
ACC = sum(predictions(:,1) == predictions(:,2))*100/length(Y);
end
%%
function [ idx, newX, newY, R, T ] = demo2( X, Y, param )
m = numel(Y);
tic;
idx = BIRCH(X, Y, param);
T = toc; % Running time
newX = X(idx, :);
newY = Y(idx);
% Calculate the reduction rate
R = (m - numel(idx))*100/m;
end
%%
function [ idx, newX, newY, R, T ] = demo3( X, Y )
m = numel(Y);
tic;
idx = BIRCH(X, Y);
T = toc;
newX = X(idx, :);
newY = Y(idx);
% Calculate the reduction rate
R = (m - numel(idx))*100/m;
end
%% Separate the dataset into the input matrix and the output vector
function [ X, Y ] = divideTable( DATASET )
if istable(DATASET)
X = table2array(DATASET(:,1:end-1));
Y = categorical(DATASET.Class);
else
error('The parameter must be a table, not a %s.', class(DATASET));
end
end