ECOCPAK v0.9
Classifier_flda.hpp
Go to the documentation of this file.
00001 // Copyright (C) 2011 the authors listed below
00002 // http://ecocpak.sourceforge.net
00003 //
00004 // Authors:
00005 // - Dimitrios Bouzas (bouzas at ieee dot org)
00006 // - Nikolaos Arvanitopoulos (niarvani at ieee dot org)
00007 // - Anastasios Tefas (tefas at aiia dot csd dot auth dot gr)
00008 //
00009 // This file is part of the ECOC PAK C++ library. It is
00010 // provided without any warranty of fitness for any purpose.
00011 //
00012 // You can redistribute this file and/or modify it under
00013 // the terms of the GNU Lesser General Public License (LGPL)
00014 // as published by the Free Software Foundation, either
00015 // version 3 of the License or (at your option) any later
00016 // version.
00017 // (see http://www.opensource.org/licenses for more info)
00018 
00019 
00022 
00023 
00024 #ifndef _CLASSIFIER_FLDA_H_
00025 #define _CLASSIFIER_FLDA_H_
00026 
00027 
00028 
00029 #include "Classifier.hpp"
00030 
00031 
00032 
00040 class Classifier_flda : public Classifier
00041   {
00042   public:
00043 
00044   // ---------------------------------------------------------------- //
00045   // ------------------------ Constructors -------------------------- //
00046   // ---------------------------------------------------------------- //
00047 
00048   // Copy ctor
00049   Classifier_flda
00050     (
00051     const Classifier_flda& c
00052     );
00053 
00054   // User defined ctor FLDA -- Overloaded
00055   Classifier_flda
00056     (
00057     const mat& A,
00058     const mat& B
00059     );
00060 
00061   // ---------------------------------------------------------------- //
00062   // ---------------------- Member Functions ------------------------ //
00063   // ---------------------------------------------------------------- //
00064 
00065   // return prediction value of classifier for input feature vector
00066   double predict(const rowvec& t) const;
00067 
00068   // ---------------------------------------------------------------- //
00069   // --------------------- Overloaded Operators --------------------- //
00070   // ---------------------------------------------------------------- //
00071 
00072   // ---------------------------------------------------------------- //
00073   // -------------------------- Attributes -------------------------- //
00074   // ---------------------------------------------------------------- //
00075 
00076   // projection vector for FLDA
00077   colvec w;
00078 
00079   // bias for FLDA
00080   double w0;
00081   };
00082 
00083 
00084 
00092 Classifier_flda::Classifier_flda
00093   (
00094   const Classifier_flda& c
00095   )
00096   {
00097   // update projection vector attribute
00098   w = c.w;
00099 
00100   // update bias
00101   w0 = c.w0;
00102 
00103   // update the plus and minus classes
00104   pos = c.pos;
00105   neg = c.neg;
00106 
00107   // update number of possitive and negative samples
00108   n_pos = c.n_pos;
00109   n_neg = c.n_neg;
00110 
00111   // update training error
00112   training_error = c.training_error;
00113   }
00114 
00115 
00116 
00126 Classifier_flda::Classifier_flda
00127   (
00128   const mat& A,
00129   const mat& B
00130   )
00131   {
00132   // number of samples
00133   const u32 n_samplesA = A.n_rows;
00134 
00135   // samples' dimensionality
00136   const u32 n_samplesB = B.n_rows;
00137 
00138   // number of attributes
00139   const u32 d = A.n_cols;
00140 
00141   // vector of classes means
00142   mat MC = zeros<mat>(2, d);
00143 
00144   // find classes means
00145   rowvec mA = mean(A);
00146   rowvec mB = mean(B);
00147 
00148   // initialize within-class covariance matrix
00149   mat SW = zeros<mat>(d, d);
00150 
00151   // compute the within-class covariace matrix for class A
00152   for(u32 i = 0; i < n_samplesA; i++)
00153     {
00154     rowvec tmp_vec = A.row(i) - mA;
00155     SW += (trans(tmp_vec) * tmp_vec);
00156     }
00157 
00158   // compute the within-class covariace matrix for class B
00159   for(u32 i = 0; i < n_samplesB; i++)
00160     {
00161     rowvec tmp_vec = B.row(i) - mB;
00162     SW += (trans(tmp_vec) * tmp_vec);
00163     }
00164 
00165   // add some noise in the diagonal for robustness
00166   SW.diag() += (10e-15 * ones<vec>(d,1));
00167 
00168   // compute the weight vector
00169   w = solve(SW, trans(mB - mA));
00170 
00171   // compute the bias
00172   w0 = as_scalar((mB + mA) * w) / 2.0;
00173 
00174   // initialize number of possitive and negative samples
00175   n_pos = 0;
00176   n_neg = 0;
00177   }
00178 
00179 
00180 
00193 inline
00194 double
00195 Classifier_flda::predict(const rowvec& t) const
00196   {
00197   // return prediction
00198   return w0 - as_scalar(t * w);
00199   }
00200 
00201 
00202 
00203 #endif
00204 
00205 
00206 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerator Defines