Ignore:
Timestamp:
Oct 2, 2007, 4:02:08 PM (14 years ago)
Author:
Markus Ringnér
Message:

NCC and IGP have been changed to templates on Distance

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/classifier/IGP.h

    r865 r925  
    2525*/
    2626
     27#include "DataLookup1D.h"
     28#include "MatrixLookup.h"
     29#include "Target.h"
    2730#include "yat/utility/vector.h"
     31#include "yat/utility/yat_assert.h"
     32#include "yat/statistics/vector_distance.h"
     33
     34#include <cmath>
     35#include <limits>
    2836
    2937namespace theplu {
    3038namespace yat {
    31 
    32   namespace statistics {
    33     class Distance;
    34   }
    35 
    3639namespace classifier { 
    3740
     
    4346  /// See Kapp and Tibshirani, Biostatistics (2006).
    4447  ///
     48  template <typename Distance>
    4549  class IGP
    4650  {
     
    5155    /// the distance measure as input.
    5256    ///
    53     IGP(const MatrixLookup&, const Target&, const statistics::Distance&);
     57    IGP(const MatrixLookup&, const Target&);
    5458
    5559    ///
     
    6165    /// @return the IGP score for each class as elements in a vector.
    6266    ///
    63     const utility::vector& score(void) const {return igp_;}
     67    const utility::vector& score(void) const;
    6468
    6569
     
    6771    utility::vector igp_;
    6872
    69     const statistics::Distance& distance_;
    7073    const MatrixLookup& matrix_;
    7174    const Target& target_;
    7275  }; 
     76
     77 
     78  // templates
     79
     80  template <typename Distance>
     81  IGP<Distance>::IGP(const MatrixLookup& data, const Target& target)
     82    : matrix_(data), target_(target)
     83  {   
     84    yat_assert(target_.size()==matrix_.columns());
     85   
     86    // Calculate IGP for each class
     87    igp_.clone(utility::vector(target_.nof_classes()));
     88   
     89    for(u_int i=0; i<target_.size(); i++) {
     90      u_int neighbor=i;
     91      double mindist=std::numeric_limits<double>::max();
     92      const DataLookup1D a(matrix_,i,false);
     93      for(u_int j=0; j<target_.size(); j++) {           
     94        DataLookup1D b(matrix_,j,false);
     95        double dist=statistics::
     96          vector_distance(a.begin,a.end(),b.begin(),
     97                          statistics::vector_distance_traits<Distance>::distace());
     98        if(j!=i && dist<mindist) {
     99          mindist=dist;
     100          neighbor=j;
     101        }
     102      }
     103      if(target_(i)==target_(neighbor))
     104        igp_(target_(i))++;
     105     
     106    }
     107    for(u_int i=0; i<target_.nof_classes(); i++) {
     108      igp_(i)/=static_cast<double>(target_.size(i));
     109    }
     110  }
     111 
     112  template <typename Distance>
     113  IGP<Distance>::~IGP()   
     114  {
     115  }
     116
     117 
     118  template <typename Distance>
     119  const utility::vector& IGP<Distance>::score(void) const
     120  {
     121    return igp_;
     122  }
    73123 
    74124}}} // of namespace classifier, yat, and theplu
Note: See TracChangeset for help on using the changeset viewer.