├── LICENSE ├── Main.m ├── README.md ├── ionosphere.mat ├── jBDE.m └── jFitnessFunction.m /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2020, Jingwei Too 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /Main.m: -------------------------------------------------------------------------------- 1 | %-------------------------------------------------------------------% 2 | % Binary Differential Evolution (BDE) demo version % 3 | %-------------------------------------------------------------------% 4 | 5 | 6 | %---Input------------------------------------------------------------ 7 | % feat : feature vector (instances x features) 8 | % label : label vector (instances x 1) 9 | % N : Number of solutions 10 | % max_Iter : Maximum number of iterations 11 | % CR : Crossover rate 12 | 13 | %---Output----------------------------------------------------------- 14 | % sFeat : Selected features (instances x features) 15 | % Sf : Selected feature index 16 | % Nf : Number of selected features 17 | % curve : Convergence curve 18 | %-------------------------------------------------------------------- 19 | 20 | 21 | %% Binary Differential Evolution 22 | clc, clear, close 23 | % Benchmark data set 24 | load ionosphere.mat; 25 | 26 | % Set 20% data as validation set 27 | ho = 0.2; 28 | % Hold-out method 29 | HO = cvpartition(label,'HoldOut',ho,'Stratify',false); 30 | 31 | % Parameter setting 32 | N = 10; 33 | max_Iter = 100; 34 | CR = 0.9; 35 | % Binary Differential Evolution 36 | [sFeat,Sf,Nf,curve] = jBDE(feat,label,N,max_Iter,CR,HO); 37 | 38 | % Plot convergence curve 39 | plot(1:max_Iter,curve); 40 | xlabel('Number of generations'); 41 | ylabel('Fitness Value'); 42 | title('BDE'); grid on; 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Binary Differential Evolution for Feature Selection 2 | 3 | [![View Binary Differential Evolution for Feature Selection on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://www.mathworks.com/matlabcentral/fileexchange/71515-binary-differential-evolution-for-feature-selection) 4 | [![License](https://img.shields.io/badge/license-BSD_3-yellow.svg)](https://github.com/JingweiToo/Binary-Differential-Evolution-for-Feature-Selection/blob/master/LICENSE) 5 | [![GitHub release](https://img.shields.io/badge/release-1.3-green.svg)](https://github.com/JingweiToo/Binary-Differential-Evolution-for-Feature-Selection) 6 | 7 | ![Wheel](https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/f2a7eded-0f65-4980-bf79-dcb027c792a0/f1b6049d-781a-4216-91b7-c4e36c746b9f/images/screenshot.PNG) 8 | 9 | 10 | ## Introduction 11 | * This toolbox offers Binary Differential Evolution ( BDE ) method 12 | * The `Main` file illustrates the example of how BDE can solve the feature selection problem using benchmark data-set. 13 | 14 | ## Input 15 | * *`feat`* : feature vector ( Instances *x* Features ) 16 | * *`label`* : label vector ( Instances *x* 1 ) 17 | * *`N`* : number of solutions 18 | * *`max_Iter`* : maximum number of iterations 19 | * *`CR`* : crossover rate 20 | 21 | 22 | ## Output 23 | * *`sFeat`* : selected features 24 | * *`Sf`* : selected feature index 25 | * *`Nf`* : number of selected features 26 | * *`curve`* : convergence curve 27 | 28 | 29 | ### Example 30 | ```code 31 | % Benchmark data set 32 | load ionosphere.mat; 33 | 34 | % Set 20% data as validation set 35 | ho = 0.2; 36 | % Hold-out method 37 | HO = cvpartition(label,'HoldOut',ho); 38 | 39 | % Parameter setting 40 | N = 10; 41 | max_Iter = 100; 42 | CR = 0.9; 43 | % Binary Differential Evolution 44 | [sFeat,Sf,Nf,curve] = jBDE(feat,label,N,max_Iter,CR,HO); 45 | 46 | % Plot convergence curve 47 | plot(1:max_Iter,curve); 48 | xlabel('Number of generations'); 49 | ylabel('Fitness Value'); 50 | title('BDE'); grid on; 51 | ``` 52 | 53 | 54 | ## Requirement 55 | * MATLAB 2014 or above 56 | * Statistics and Machine Learning Toolbox 57 | 58 | 59 | ## Cite As 60 | ```code 61 | @article{too2019hybrid, 62 | title={Hybrid Binary Particle Swarm Optimization Differential Evolution-Based Feature Selection for EMG Signals Classification}, 63 | author={Too, Jingwei and Abdullah, Abdul Rahim and Mohd Saad, Norhashimah}, 64 | journal={Axioms}, 65 | volume={8}, 66 | number={3}, 67 | pages={79}, 68 | year={2019}, 69 | publisher={Multidisciplinary Digital Publishing Institute} 70 | } 71 | 72 | 73 | @article{too2019emg, 74 | title={EMG feature selection and classification using a Pbest-guide binary particle swarm optimization}, 75 | author={Too, Jingwei and Abdullah, Abdul Rahim and Mohd Saad, Norhashimah and Tee, Weihown}, 76 | journal={Computation}, 77 | volume={7}, 78 | number={1}, 79 | pages={12}, 80 | year={2019}, 81 | publisher={Multidisciplinary Digital Publishing Institute} 82 | } 83 | ``` 84 | -------------------------------------------------------------------------------- /ionosphere.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JingweiToo/Binary-Differential-Evolution-for-Feature-Selection/8d6d48cc4f866f042ab818a7ef631007187233fd/ionosphere.mat -------------------------------------------------------------------------------- /jBDE.m: -------------------------------------------------------------------------------- 1 | function [sFeat,Sf,Nf,curve] = jBDE(feat,label,N,max_Iter,CR,HO) 2 | 3 | fun = @jFitnessFunction; 4 | dim = size(feat,2); 5 | X = zeros(N,dim); 6 | for i = 1:N 7 | for d = 1:dim 8 | if rand() > 0.5 9 | X(i,d) = 1; 10 | end 11 | end 12 | end 13 | fit = zeros(1,N); 14 | fitG = inf; 15 | for i = 1:N 16 | fit(i) = fun(feat,label,X(i,:),HO); 17 | if fit(i) < fitG 18 | fitG = fit(i); 19 | Xgb = X(i,:); 20 | end 21 | end 22 | MV = zeros(N,dim); 23 | Xnew = zeros(N,dim); 24 | 25 | curve = inf; 26 | t = 1; 27 | %---Iterations start-------------------------------------------------- 28 | while t <= max_Iter 29 | for i = 1:N 30 | R = randperm(N); R(R == i) = []; 31 | r1 = R(1); 32 | r2 = R(2); 33 | r3 = R(3); 34 | for d = 1:dim 35 | if X(r1,d) == X(r2,d) 36 | diffV = 0; 37 | else 38 | diffV = X(r1,d); 39 | end 40 | if diffV == 1 41 | MV(i,d) = 1; 42 | else 43 | MV(i,d) = X(r3,d); 44 | end 45 | end 46 | jrand = randi([1,dim]); 47 | for d = 1:dim 48 | if rand() <= CR || d == jrand 49 | Xnew(i,d) = MV(i,d); 50 | else 51 | Xnew(i,d) = X(i,d); 52 | end 53 | end 54 | end 55 | for i = 1:N 56 | Fnew = fun(feat,label,Xnew(i,:),HO); 57 | if Fnew <= fit(i) 58 | X(i,:) = Xnew(i,:); 59 | fit(i) = Fnew; 60 | end 61 | if fit(i) < fitG 62 | fitG = fit(i); 63 | Xgb = X(i,:); 64 | end 65 | end 66 | curve(t) = fitG; 67 | fprintf('\nIteration %d Best (BDE)= %f',t,curve(t)) 68 | t = t + 1; 69 | end 70 | Pos = 1:dim; 71 | Sf = Pos(Xgb == 1); 72 | Nf = length(Sf); 73 | sFeat = feat(:,Sf); 74 | end 75 | 76 | 77 | -------------------------------------------------------------------------------- /jFitnessFunction.m: -------------------------------------------------------------------------------- 1 | % Notation: This fitness function is for demonstration 2 | 3 | function cost = jFitnessFunction(feat,label,X,HO) 4 | if sum(X == 1) == 0 5 | cost = 1; 6 | else 7 | cost = jwrapperKNN(feat(:, X == 1),label,HO); 8 | end 9 | end 10 | 11 | 12 | function error = jwrapperKNN(sFeat,label,HO) 13 | %---// Parameter setting for k-value of KNN // 14 | k = 5; 15 | 16 | xtrain = sFeat(HO.training == 1,:); 17 | ytrain = label(HO.training == 1); 18 | xvalid = sFeat(HO.test == 1,:); 19 | yvalid = label(HO.test == 1); 20 | 21 | Model = fitcknn(xtrain,ytrain,'NumNeighbors',k); 22 | pred = predict(Model,xvalid); 23 | num_valid = length(yvalid); 24 | correct = 0; 25 | for i = 1:num_valid 26 | if isequal(yvalid(i),pred(i)) 27 | correct = correct + 1; 28 | end 29 | end 30 | Acc = correct / num_valid; 31 | error = 1 - Acc; 32 | end 33 | 34 | --------------------------------------------------------------------------------