forked from treder/MVPA-Light
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_logreg.m
More file actions
81 lines (61 loc) · 2.16 KB
/
Copy pathexample_logreg.m
File metadata and controls
81 lines (61 loc) · 2.16 KB
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
%%% Train and test logistic regression.
close all
clear all
% Load data (in /examples folder)
[dat, clabel] = load_example_data('epoched3', 0);
% Average activity in 0.6-0.8 interval (see example1)
ival_idx = find(dat.time >= 0.6 & dat.time <= 0.8);
X = squeeze(mean(dat.trial(:,:,ival_idx),3));
%% Logistic regression
param = mv_get_classifier_param('logreg');
cf = train_logreg(param, X, clabel);
%% Hyperparameter Alpha: classification accuracy and computation duration
%%% Investigate the effects on classification performance and computation
%%% duration of the hyperparameter alpha. The parameter is varied from
%%% 10^-10 to 1.
%%% For larger alpha terms, the computation takes quite a while!
% alphas = 10.^[-10:-3];
alphas = [10.^[-10:-1], 0.2, 0.5, 1];
acc = zeros(numel(alphas),1);
time = zeros(numel(alphas),1);
% Get default parameters for logistic regression
param = mv_classifier_defaults('logreg');
% Setup cfg for cross-validation
cfg = [];
cfg.classifier = 'logreg';
cfg.K = 5;
cfg.repeat = 2;
cfg.balance = 'undersample';
for aa=1:numel(alphas)
fprintf('Lambda=%2.6f\n',alphas(aa))
% Vary the lambda parameter
param.alpha = alphas(aa);
cfg.param = param;
tic
acc(aa) = mv_crossvalidate(cfg, X, label);
time(aa) = toc;
end
figure
subplot(1,2,1)
% semilogx(alphas, acc)
plot(alphas, acc)
xlabel('Alpha'),ylabel('Classification accuracy')
subplot(1,2,2)
semilogx(alphas, time)
xlabel('Alpha'),ylabel('Computation time [s]')
%% Hyperparameter lambda: Deviance and sparsity
%%% If alpha is set to an intermediate or large value e.g. alpha > 0.2, the
%%% coefficient vector tends to become more sparse. The amount of sparsity
%%% (=number of zero coefficients) is determined by the amount of
%%% regularisation lambda.
param = mv_classifier_defaults('logreg');
param.alpha = 10^-8;%0.5;
[cf, b, stats] = train_logreg(X,label,param);
figure
subplot(1,2,1),
lassoPlot(b,stats,'plottype','CV');
title('Deviance as a function of lambda')
subplot(1,2,2),
semilogx(stats.lambda, sum(b))
title('Sparsity as a function of lambda')
xlabel('Lambda'),ylabel('Nr. of zero coefficients')