-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathA_Main.m
128 lines (109 loc) · 3.74 KB
/
A_Main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
% Wrapper Feature Selection Toolbox
% There are more than 40 wrapper FS methods are offered
% You may open < List_Method.m file > to check all available methods
%---Usage-------------------------------------------------------------
% If you wish to use 'PSO' (see example 1) then you write
% FS = jfs('pso',feat,label,opts);
% If you want to use 'SMA' (see example 2) then you write
% FS = jfs('sma',feat,label,opts);
% * All methods have different calling name (refer List_Method.m file)
%---Input-------------------------------------------------------------
% feat : Feature vector matrix (Instances x Features)
% label : Label matrix (Instances x 1)
% opts : Parameter settings
% opts.N : Number of solutions / population size (* for all methods)
% opts.T : Maximum number of iterations (* for all methods)
% opts.k : Number of k in k-nearest neighbor
% Some methods have their specific parameters (example: PSO, GA, DE)
% if you do not set them then they will define as default settings
% * you may open the < m.file > to view or change the parameters
% * you may use 'opts' to set the parameters of method (see example 1)
% * you may also change the < jFitnessFunction.m file >
%---Output------------------------------------------------------------
% FS : Feature selection model (It contains several results)
% FS.sf : Index of selected features
% FS.ff : Selected features
% FS.nf : Number of selected features
% FS.c : Convergence curve
% Acc : Accuracy of validation model
%% Example 1: Particle Swarm Optimization (PSO)
clear, clc, close;
% Number of k in K-nearest neighbor
opts.k = 5;
% Ratio of validation data
ho = 0.2;
% Common parameter settings
opts.N = 10; % number of solutions
opts.T = 100; % maximum number of iterations
% Parameters of PSO
opts.c1 = 2;
opts.c2 = 2;
opts.w = 0.9;
% Load dataset
load ionosphere.mat;
% Divide data into training and validation sets
HO = cvpartition(label,'HoldOut',ho);
opts.Model = HO;
% Perform feature selection
FS = jfs('pso',feat,label,opts);
% Define index of selected features
sf_idx = FS.sf;
% Accuracy
Acc = jknn(feat(:,sf_idx),label,opts);
% Plot convergence
plot(FS.c); grid on;
xlabel('Number of Iterations');
ylabel('Fitness Value');
title('PSO');
%% Example 2: Slime Mould Algorithm (SMA)
clear, clc, close;
% Number of k in K-nearest neighbor
opts.k = 5;
% Ratio of validation data
ho = 0.2;
% Common parameter settings
opts.N = 10; % number of solutions
opts.T = 100; % maximum number of iterations
% Load dataset
load ionosphere.mat;
% Divide data into training and validation sets
HO = cvpartition(label,'HoldOut',ho);
opts.Model = HO;
% Perform feature selection
FS = jfs('sma',feat,label,opts);
% Define index of selected features
sf_idx = FS.sf;
% Accuracy
Acc = jknn(feat(:,sf_idx),label,opts);
% Plot convergence
plot(FS.c); grid on;
xlabel('Number of Iterations');
ylabel('Fitness Value');
title('SMA');
%% Example 3: Whale Optimization Algorithm (WOA)
clear, clc, close;
% Number of k in K-nearest neighbor
opts.k = 5;
% Ratio of validation data
ho = 0.2;
% Common parameter settings
opts.N = 10; % number of solutions
opts.T = 100; % maximum number of iterations
% Parameter of WOA
opts.b = 1;
% Load dataset
load ionosphere.mat;
% Divide data into training and validation sets
HO = cvpartition(label,'HoldOut',ho);
opts.Model = HO;
% Perform feature selection
FS = jfs('woa',feat,label,opts);
% Define index of selected features
sf_idx = FS.sf;
% Accuracy
Acc = jknn(feat(:,sf_idx),label,opts);
% Plot convergence
plot(FS.c); grid on;
xlabel('Number of Iterations');
ylabel('Fitness Value');
title('WOA');