Contents

Example code

This is a demo to show how OFC_train and OFC_classify can be used. All these codes are in a development step, so bugs and erros can be found. If you find errors or you have comments, please contact the author.

The code is released under the terms of the GNU GPLv3 License. If you use the code for your research please cite the papers [1] and [2] below.

refs: [1] Matías Di Martino, Alicia Fernández, Pablo Iturralde, Federico Lecumberry. "Novel classifier scheme for imbalanced problems". Pattern Recognition Letters 34 (2013) 1146–1151. [2] Matıas Di Martino, Guzman Hernandez, Marcelo Fiori and Alicia Fernandez. "A new framework for optimal classifier design" Pattern Recognition 46 (2013) 2249–2255.

matiasdm@fing.edu.uy (2013)

% ***************************************************************************
% *   This program is free software; you can redistribute it and/or modify  *
% *   it under the terms of the GNU General Public License as published by  *
% *   the Free Software Foundation; either version 3 of the License, or     *
% *   (at your option) any later version.                                   *
% *                                                                         *
% *   You should have received a copy of the GNU General Public License     *
% *   along with this program; if not, write to the                         *
% *   Free Software Foundation, Inc.,                                       *
% *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
% ***************************************************************************


clear all
close all
clc

% 

0) Set general parameters

beta = 1; % defines weight between recall and presicion on Fmeasure definition

% 

1) load some data

load Data
NumSamples = size(Samples,1);
NumFeatures = size(Samples,2);

% Normalization of Features:
for dim = 1:NumFeatures,
    Samples(:,dim) = 2 * ( Samples(:,dim) - min(Samples(:,dim)) ) / ...
        ( max(Samples(:,dim)) - min(Samples(:,dim)) ) - 1 ;
end

% visualization,
figure('name','Data')
S = 5*ones(NumSamples,1);
C = (Labels==1)*[1 0 0] + (Labels==-1)*[0 0 1]; % set different colors for
                                                % classes
scatter(Samples(:,1),Samples(:,2), S, C,'filled'); xlabel('x'), ylabel('y');

% 

2) Split data into train and test subsets

% first mix data
rng(1); % set rand seed (comment this line if you want different partions
        % each time)
[~, RandomIndexs] = sort(rand(NumSamples,1));
Samples = Samples(RandomIndexs,:);
Labels  = Labels(RandomIndexs);

TrainSamples = Samples(1:round(end/2),:);
TrainLabels = Labels(1:round(end/2));
TestSamples = Samples(round(end/2)+1:end,:);
TestLabels = Labels(round(end/2)+1:end);

% 

3) Train classifier,

clear parameters,
parameters.verbose = 2; % ( 1 - some text outputs, 2 - also some images)
% additional parameters can be set, see HELP OFC_train.

[U,X] = OFC_train(TrainSamples,TrainLabels,parameters);

% show output
figure('name','U'),imagesc(X{1}(:),X{2}(:),U'), xlabel('x'); ylabel('y');
axis xy; colormap jet, colorbar; drawnow; %U is transposed on imagesc to have
% a visualization consistent with ndgrid (which flips the two first dimensions
% compared with meshgrid)

% 
It: 1 dif = 3.3581 Fv = 0.1688
It: 2 dif = 2.1297 Fv = 0.28104
It: 200 dif = 1.2913 Fv = 0.33868
It: 500 dif = 1.2898 Fv = 0.33871

It: 1000 dif = 1.2893 Fv = 0.33872

4) Classify test and train samples

[GuessedTrainLabels] = OFC_classify(TrainSamples,U,X);
[GuessedTestLabels] = OFC_classify(TestSamples,U,X);

% 

5) Evaluate

[TrainFV,TrainAccuracy] = performance(TrainLabels, GuessedTrainLabels, beta);
[TestFV,TestAccuracy] = performance(TestLabels, GuessedTestLabels, beta);

fprintf('/////////////////////////////////////////////////////////////// \n')
fprintf('Train FV: %4.5g %%  || Train Accuracy:  %4.5g %% \n',100*TrainFV,100*TrainAccuracy)
fprintf('Test FV: %4.5g  %%  || TestAccuracy:    %4.5g %% \n',100*TestFV,100*TestAccuracy)
fprintf('/////////////////////////////////////////////////////////////// \n')
/////////////////////////////////////////////////////////////// 
Train FV: 38.951 %  || Train Accuracy:  85.182 % 
Test FV: 37.548  %  || TestAccuracy:    85.182 % 
///////////////////////////////////////////////////////////////