-
Notifications
You must be signed in to change notification settings - Fork 0
/
step4_hyperparameter_optimization_knn.m
43 lines (35 loc) · 1.23 KB
/
step4_hyperparameter_optimization_knn.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
close all;
clear variables;
clc;
dataset_type = 'A';
if dataset_type == 'A'; dataset_name = '\Dataset A';
elseif dataset_type == 'B'; dataset_name = '\Dataset B';
elseif dataset_type == 'C'; dataset_name = '\Dataset C';
elseif dataset_type == 'D'; dataset_name = '\Dataset D';
end
output_path = strcat(pwd,dataset_name,'\trained_model\');
if ~exist(output_path, 'dir')
mkdir(output_path);
end
input_file = strcat(pwd,dataset_name,'\lopo cv dataset\lopo_dataset.mat');
output_file = strcat(output_path,'fitcknn_JSE.mat');
load(input_file);
optimized_model = {};
cnt = 0;
person_list = transpose(cell2mat(LOPO(:,1)));
training_dataset = LOPO(:,2);
for p = person_list
tic
fprintf("%d / %d\n", p, max(person_list));
training = training_dataset{p};
Y = training(:,size(training,2)); % label
X = training(:,1:size(training,2)-1); % features
knn_model = fitcknn(X,Y,'OptimizeHyperparameters','all',...
'HyperparameterOptimizationOptions',...
struct('AcquisitionFunctionName','expected-improvement-plus',...
'Verbose',0,'ShowPlots',0));
cnt = cnt + 1;
optimized_model(cnt,:) = {p,knn_model};
toc
end
save(output_file,'optimized_model');