Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleLDA.cpp

example for linear discriminant analysis on a set of classes of vectors

Author
MIP
/* This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** @example ExampleLDA.cpp
@relates LDA
@ingroup g_examples
@brief example for linear discriminant analysis on a set of classes of vectors
@author MIP
*/
#include <bias_config.h>
#include <MathAlgo/LDA.hh>
#include <Base/Math/Random.hh>
using namespace BIAS;
using namespace std;
int main(int args, char **arg)
{
LDA myLDA(true);//equalweights for all classes ?
myLDA.SetDebugLevel(1+2);
std::vector<std::vector<BIAS::Vector<LDAType> > > vec;
int reductionSize = 2;
std::vector<BIAS::Matrix<LDAType> > *covs = NULL;
unsigned int classes = 3;
unsigned int numsamples = 1000;
unsigned int dim = 2;
BIAS::Matrix<LDAType> matrix(reductionSize,dim,MatrixZero);
Vector<double> sigma(dim);
sigma[0] = 1.1;
sigma[1] = 0.01;
for (unsigned int i=2; i<dim; i++) sigma[i] = 0.1;
Random R;
vector<Vector<LDAType> > means;
for (unsigned int c=0; c<classes; c++){
Vector<LDAType> curMean(dim), estMean(dim);
means.push_back(curMean);
switch (c) {
case 0: curMean[0] = -1;curMean[1] = 0;break;
case 1: curMean[0] = 1;curMean[1] = 0;break;
case 2: curMean[0] = 0.1f;curMean[1] = -0.1f;break;
default: {
for (unsigned int d=0; d<dim; d++) {
curMean[d] = (float) R.GetUniformDistributed(-1.0*double(d),
double(d));
estMean[d] = 0;
}
}
}
cout<<"mean "<<c<<" is "<<curMean<<endl;
std::vector<BIAS::Vector<LDAType> > curvec;
for (unsigned int i=0; i<numsamples; i++) {
Vector<LDAType> sample(dim);
for (unsigned int d=0; d<dim; d++) {
sample[d] = (float) R.GetNormalDistributed(curMean[d], sigma[d]);
estMean[d] += sample[d];
}
curvec.push_back(sample);
}
for (unsigned int d=0; d<dim; d++) {
estMean[d] /= float(numsamples);
}
cout<<"estimated mean is "<<estMean<<endl;
vec.push_back(curvec);
}
/*
GenerateRandomTestData(int vectorSize, int numberOfClasses,
int classSizeMin, int classSizeMax, int vectorEntryMin, int vectorEntryMax,
double stdDeviation, double mainDirectionStdDeviation,
std::vector<std::vector<BIAS::Vector<LDAType> > > &vec,
std::vector<BIAS::Vector<LDAType> > &means, bool Normalize);
*/
myLDA.ComputeReductionMatrix(vec, matrix, reductionSize, covs);
cout<<"Reduction matrix is "<<matrix<<endl;
return 0;
}