LDA.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 1999-2008 Soeren Sonnenburg
00008  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/common.h"
00012 
00013 #ifdef HAVE_LAPACK
00014 #include "classifier/Classifier.h"
00015 #include "classifier/LinearClassifier.h"
00016 #include "classifier/LDA.h"
00017 #include "features/Labels.h"
00018 #include "lib/Mathematics.h"
00019 #include "lib/lapack.h"
00020 
00021 CLDA::CLDA(float64_t gamma)
00022 : CLinearClassifier(), m_gamma(gamma)
00023 {
00024 }
00025 
00026 CLDA::CLDA(float64_t gamma, CRealFeatures* traindat, CLabels* trainlab)
00027 : CLinearClassifier(), m_gamma(gamma)
00028 {
00029     set_features(traindat);
00030     set_labels(trainlab);
00031 }
00032 
00033 
00034 CLDA::~CLDA()
00035 {
00036 }
00037 
00038 bool CLDA::train()
00039 {
00040     ASSERT(labels);
00041     ASSERT(features);
00042     int32_t num_train_labels=0;
00043     int32_t* train_labels=labels->get_int_labels(num_train_labels);
00044     ASSERT(train_labels);
00045 
00046     int32_t num_feat=features->get_num_features();
00047     int32_t num_vec=features->get_num_vectors();
00048     ASSERT(num_vec==num_train_labels);
00049 
00050     int32_t* classidx_neg=new int32_t[num_vec];
00051     int32_t* classidx_pos=new int32_t[num_vec];
00052 
00053     int32_t i=0;
00054     int32_t j=0;
00055     int32_t num_neg=0;
00056     int32_t num_pos=0;
00057     for (i=0; i<num_train_labels; i++)
00058     {
00059         if (train_labels[i]==-1)
00060             classidx_neg[num_neg++]=i;
00061         else if (train_labels[i]==+1)
00062             classidx_pos[num_pos++]=i;
00063         else
00064         {
00065             SG_ERROR( "found label != +/- 1 bailing...");
00066             return false;
00067         }
00068     }
00069 
00070     if (num_neg<=0 && num_pos<=0)
00071     {
00072       SG_ERROR( "whooooo ? only a single class found\n");
00073         return false;
00074     }
00075 
00076     delete[] w;
00077     w=new float64_t[num_feat];
00078     w_dim=num_feat;
00079 
00080     float64_t* mean_neg=new float64_t[num_feat];
00081     memset(mean_neg,0,num_feat*sizeof(float64_t));
00082 
00083     float64_t* mean_pos=new float64_t[num_feat];
00084     memset(mean_pos,0,num_feat*sizeof(float64_t));
00085 
00086     /* calling external lib */
00087     double* scatter=new double[num_feat*num_feat];
00088     double* buffer=new double[num_feat*CMath::max(num_neg, num_pos)];
00089     int nf = (int) num_feat;
00090 
00091     //mean neg
00092     for (i=0; i<num_neg; i++)
00093     {
00094         int32_t vlen;
00095         bool vfree;
00096         float64_t* vec=
00097             features->get_feature_vector(classidx_neg[i], vlen, vfree);
00098         ASSERT(vec);
00099 
00100         for (j=0; j<vlen; j++)
00101         {
00102             mean_neg[j]+=vec[j];
00103             buffer[num_feat*i+j]=vec[j];
00104         }
00105 
00106         features->free_feature_vector(vec, classidx_neg[i], vfree);
00107     }
00108 
00109     for (j=0; j<num_feat; j++)
00110         mean_neg[j]/=num_neg;
00111 
00112     for (i=0; i<num_neg; i++)
00113     {
00114         for (j=0; j<num_feat; j++)
00115             buffer[num_feat*i+j]-=mean_neg[j];
00116     }
00117     cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans, nf, nf,
00118         (int) num_neg, 1.0, buffer, nf, buffer, nf, 0, scatter, nf);
00119     
00120     //mean pos
00121     for (i=0; i<num_pos; i++)
00122     {
00123         int32_t vlen;
00124         bool vfree;
00125         float64_t* vec=
00126             features->get_feature_vector(classidx_pos[i], vlen, vfree);
00127         ASSERT(vec);
00128 
00129         for (j=0; j<vlen; j++)
00130         {
00131             mean_pos[j]+=vec[j];
00132             buffer[num_feat*i+j]=vec[j];
00133         }
00134 
00135         features->free_feature_vector(vec, classidx_pos[i], vfree);
00136     }
00137 
00138     for (j=0; j<num_feat; j++)
00139         mean_pos[j]/=num_pos;
00140 
00141     for (i=0; i<num_pos; i++)
00142     {
00143         for (j=0; j<num_feat; j++)
00144             buffer[num_feat*i+j]-=mean_pos[j];
00145     }
00146     cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans, nf, nf, (int) num_pos,
00147         1.0/(num_train_labels-1), buffer, nf, buffer, nf,
00148         1.0/(num_train_labels-1), scatter, nf);
00149 
00150     float64_t trace=CMath::trace((float64_t*) scatter, num_feat, num_feat);
00151 
00152     double s=1.0-m_gamma; /* calling external lib; indirectly */
00153     for (i=0; i<num_feat*num_feat; i++)
00154         scatter[i]*=s;
00155 
00156     for (i=0; i<num_feat; i++)
00157         scatter[i*num_feat+i]+= trace*m_gamma/num_feat;
00158 
00159     double* inv_scatter= (double*) CMath::pinv(
00160         scatter, num_feat, num_feat, NULL);
00161 
00162     float64_t* w_pos=buffer;
00163     float64_t* w_neg=&buffer[num_feat];
00164 
00165     cblas_dsymv(CblasColMajor, CblasUpper, nf, 1.0, inv_scatter, nf,
00166         (double*) mean_pos, 1, 0., (double*) w_pos, 1);
00167     cblas_dsymv(CblasColMajor, CblasUpper, nf, 1.0, inv_scatter, nf,
00168         (double*) mean_neg, 1, 0, (double*) w_neg, 1);
00169     
00170     bias=0.5*(CMath::dot(w_neg, mean_neg, num_feat)-CMath::dot(w_pos, mean_pos, num_feat));
00171     for (i=0; i<num_feat; i++)
00172         w[i]=w_pos[i]-w_neg[i];
00173 
00174 #ifdef DEBUG_LDA
00175     SG_PRINT("bias: %f\n", bias);
00176     CMath::display_vector(w, num_feat, "w");
00177     CMath::display_vector(w_pos, num_feat, "w_pos");
00178     CMath::display_vector(w_neg, num_feat, "w_neg");
00179     CMath::display_vector(mean_pos, num_feat, "mean_pos");
00180     CMath::display_vector(mean_neg, num_feat, "mean_neg");
00181 #endif
00182 
00183     delete[] train_labels;
00184     delete[] mean_neg;
00185     delete[] mean_pos;
00186     delete[] scatter;
00187     delete[] inv_scatter;
00188     delete[] classidx_neg;
00189     delete[] classidx_pos;
00190     delete[] buffer;
00191     return true;
00192 }
00193 #endif

SHOGUN Machine Learning Toolbox - Documentation