Changeset 1031 for trunk/yat


Ignore:
Timestamp:
Feb 4, 2008, 4:44:44 PM (14 years ago)
Author:
Markus Ringnér
Message:

Fixes #272

Location:
trunk/yat
Files:
4 edited
3 moved

Legend:

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

    r1009 r1031  
    3030#include "yat/utility/vector.h"
    3131#include "yat/utility/yat_assert.h"
    32 #include "yat/statistics/vector_distance.h"
     32#include "yat/statistics/distance.h"
    3333
    3434#include <cmath>
     
    9595        DataLookup1D b(matrix_,j,false);
    9696        double dist=statistics::
    97           vector_distance(a.begin,a.end(),b.begin(),
    98                           statistics::vector_distance_traits<Distance>::distace());
     97          distance(a.begin,a.end(),b.begin(),
     98                          statistics::distance_traits<Distance>::distace());
    9999        if(j!=i && dist<mindist) {
    100100          mindist=dist;
  • trunk/yat/classifier/KNN.h

    r1028 r1031  
    3030#include "SupervisedClassifier.h"
    3131#include "Target.h"
    32 #include "yat/statistics/vector_distance.h"
     32#include "yat/statistics/distance.h"
    3333#include "yat/utility/matrix.h"
    3434#include "yat/utility/yat_assert.h"
     
    144144 
    145145  template <typename Distance>
    146   utility::matrix* KNN<Distance>::calculate_distances(const DataLookup2D& input) const
     146  utility::matrix* KNN<Distance>::calculate_distances(const DataLookup2D& test) const
    147147  {
    148148    // matrix with training samples as rows and test samples as columns
    149149    utility::matrix* distances =
    150       new utility::matrix(data_.columns(),input.columns());
    151    
    152     // if both training and test are unweighted: unweighted
    153     // calculations are used.
    154     const MatrixLookup* test_unweighted =
    155       dynamic_cast<const MatrixLookup*>(&input);     
    156     if(test_unweighted && !data_.weighted()) {
    157       const MatrixLookup* data_unweighted =
    158         dynamic_cast<const MatrixLookup*>(&data_);     
     150      new utility::matrix(data_.columns(),test.columns());
     151   
     152    // unweighted test data
     153    if(const MatrixLookup* test_unweighted =
     154       dynamic_cast<const MatrixLookup*>(&test)) {     
    159155      for(size_t i=0; i<data_.columns(); i++) {
    160         classifier::DataLookup1D training(*data_unweighted,i,false);
    161         for(size_t j=0; j<input.columns(); j++) {
     156        for(size_t j=0; j<test.columns(); j++) {
    162157          classifier::DataLookup1D test(*test_unweighted,j,false);
    163           utility::yat_assert<std::runtime_error>(training.size()==test.size());
    164158          (*distances)(i,j) =
    165             statistics::vector_distance(training.begin(),training.end(),
    166                                         test.begin(), typename statistics::vector_distance_traits<Distance>::distance());
     159            statistics::distance(classifier::DataLookup1D(data_,
     160                                                          i,false).begin(),
     161                                 classifier::DataLookup1D(data_,
     162                                                          i,false).end(),
     163                                 test.begin(),
     164                                 typename statistics::
     165                                 distance_traits<Distance>::distance());
    167166          utility::yat_assert<std::runtime_error>(!std::isnan((*distances)(i,j)));
    168167        }
    169168      }
    170169    }
    171     // if either training or test is weighted: weighted calculations
    172     // are used.
     170    // weighted test data
    173171    else {
    174172      const MatrixLookupWeighted* data_weighted =
    175173        dynamic_cast<const MatrixLookupWeighted*>(&data_);
    176174      const MatrixLookupWeighted* test_weighted =
    177         dynamic_cast<const MatrixLookupWeighted*>(&input);               
     175        dynamic_cast<const MatrixLookupWeighted*>(&test);               
    178176      if(data_weighted && test_weighted) {
    179177        for(size_t i=0; i<data_.columns(); i++) {
    180178          classifier::DataLookupWeighted1D training(*data_weighted,i,false);
    181           for(size_t j=0; j<input.columns(); j++) {
     179          for(size_t j=0; j<test.columns(); j++) {
    182180            classifier::DataLookupWeighted1D test(*test_weighted,j,false);
    183181            utility::yat_assert<std::runtime_error>(training.size()==test.size());
    184182            (*distances)(i,j) =
    185               statistics::vector_distance(training.begin(),training.end(),
    186                                           test.begin(), typename statistics::vector_distance_traits<Distance>::distance());
     183              statistics::distance(training.begin(),training.end(),
     184                                   test.begin(), typename statistics::distance_traits<Distance>::distance());
    187185            utility::yat_assert<std::runtime_error>(!std::isnan((*distances)(i,j)));
    188186          }
     
    251249
    252250  template <typename Distance>
    253   void KNN<Distance>::predict(const DataLookup2D& input,                   
     251  void KNN<Distance>::predict(const DataLookup2D& test,                     
    254252                              utility::matrix& prediction) const
    255253  {   
    256     utility::matrix* distances=calculate_distances(input);
     254    utility::yat_assert<std::runtime_error>(data_.rows()==test.rows());
     255
     256    utility::matrix* distances=calculate_distances(test);
    257257   
    258258    // for each test sample (column in distances) find the closest
    259259    // training samples
    260     prediction.clone(utility::matrix(target_.nof_classes(),input.columns(),0.0));
     260    prediction.clone(utility::matrix(target_.nof_classes(),test.columns(),0.0));
    261261    for(size_t sample=0;sample<distances->columns();sample++) {
    262262      std::vector<size_t> k_index;
  • trunk/yat/classifier/NCC.h

    r1013 r1031  
    3737#include "yat/statistics/Averager.h"
    3838#include "yat/statistics/AveragerWeighted.h"
    39 #include "yat/statistics/vector_distance.h"
     39#include "yat/statistics/distance.h"
    4040
    4141#include "yat/utility/Iterator.h"
     
    112112    // MatrixLookup and MatrixLookupWeighted
    113113    const DataLookup2D& data_;
    114 
     114    bool centroids_nan_;
    115115  };
    116116
     
    125125  template <typename Distance>
    126126  NCC<Distance>::NCC(const MatrixLookup& data, const Target& target)
    127     : SupervisedClassifier(target), centroids_(0), data_(data)
     127    : SupervisedClassifier(target), centroids_(0), data_(data), centroids_nan_(false)
    128128  {
    129129  }
     
    131131  template <typename Distance>
    132132  NCC<Distance>::NCC(const MatrixLookupWeighted& data, const Target& target)
    133     : SupervisedClassifier(target), centroids_(0), data_(data)
     133    : SupervisedClassifier(target), centroids_(0), data_(data), centroids_nan_(false)
    134134  {
    135135  }
     
    169169                              target);
    170170      }
    171       ncc->centroids_=0;
    172171    }
    173172    catch (std::bad_cast) {
     
    198197        for(size_t c=0;c<target_.nof_classes();c++) {
    199198          (*centroids_)(i,c) = class_averager[c].mean();
     199          if(class_averager[c].sum_w()==0)
     200            centroids_nan_=true;
    200201        }
    201202      }
     
    230231    prediction.clone(utility::matrix(centroids_->columns(), test.columns()));       
    231232
    232     // unweighted test data
     233    // unweighted test data and no nan's in centroids
     234    // Markus: Should test centroid_nan_ here!!!
    233235    if (const MatrixLookup* test_unweighted =
    234236        dynamic_cast<const MatrixLookup*>(&test)) {
     
    240242          utility::yat_assert<std::runtime_error>(in.size()==centroid.size());
    241243          prediction(k,j)=statistics::
    242             vector_distance(in.begin(),in.end(),centroid.begin(),
    243                             typename statistics::vector_distance_traits<Distance>::distance());
     244            distance(in.begin(),in.end(),centroid.begin(),
     245                            typename statistics::distance_traits<Distance>::distance());
    244246        }
    245247      }
     
    255257          utility::yat_assert<std::runtime_error>(in.size()==centroid.size());
    256258          prediction(k,j)=statistics::
    257             vector_distance(in.begin(),in.end(),centroid.begin(),
    258                             typename statistics::vector_distance_traits<Distance>::distance());
     259            distance(in.begin(),in.end(),centroid.begin(),
     260                            typename statistics::distance_traits<Distance>::distance());
    259261        }
    260262      }
  • trunk/yat/statistics/Makefile.am

    r1003 r1031  
    4040include_statistics_HEADERS = AUC.h Averager.h AveragerPair.h \
    4141  AveragerWeighted.h AveragerPairWeighted.h \
    42   euclidean_vector_distance.h Fisher.h \
     42  distance.h euclidean_distance.h Fisher.h \
    4343  FoldChange.h Histogram.h \
    4444  KolmogorovSmirnov.h \
    4545  Pearson.h PearsonCorrelation.h \
    46   pearson_vector_distance.h ROC.h \
     46  pearson_distance.h ROC.h \
    4747  SAMScore.h Score.h SNRScore.h tScore.h tTest.h \
    48   utility.h vector_distance.h VectorFunction.h WilcoxonFoldChange.h
     48  utility.h VectorFunction.h WilcoxonFoldChange.h
  • trunk/yat/statistics/distance.h

    r1030 r1031  
    1 #ifndef theplu_yat_statistics_vector_distance_h
    2 #define theplu_yat_statistics_vector_distance_h
     1#ifndef theplu_yat_statistics_distance_h
     2#define theplu_yat_statistics_distance_h
    33
    44// $Id$
     
    3939  /// For each measure to calculate distances between containers
    4040  /// with random access iterators one should provide a struct
    41   /// that inherits from vector_distance_tag and provides a "label" for
     41  /// that inherits from distance_tag and provides a "label" for
    4242  /// the distance measure.
    4343  ///
    44   struct vector_distance_tag {
     44  struct distance_tag {
    4545    /// tag defining distance measure
    46     typedef vector_distance_tag distance;
     46    typedef distance_tag distance;
    4747  };
    4848
     
    5454  /// distance measures.  Instead it can access the "label" of each
    5555  /// distance measure through the special template structure
    56   /// vector_distance_traits.
     56  /// distance_traits.
    5757  ///
    5858  template <class T>
    59   struct vector_distance_traits {
     59  struct distance_traits {
    6060    /// \return distance type
    6161    typedef typename T::distance distance;
     
    7373  ///
    7474  template <typename Iter1, typename Iter2, typename Dist>
    75   double vector_distance(Iter1 beg1, Iter1 end1, Iter2 beg2, const Dist disttag)
     75  double distance(Iter1 beg1, Iter1 end1, Iter2 beg2, const Dist disttag)
    7676  {
    7777    return
    78       vector_distance(beg1,end1,beg2,
    79                       typename vector_distance_traits<Dist>::distance(),
     78      distance(beg1,end1,beg2,
     79                      typename distance_traits<Dist>::distance(),
    8080                      typename utility::weighted_if_any2<Iter1, Iter2>::type());
    8181  }       
  • trunk/yat/statistics/euclidean_distance.h

    r1030 r1031  
    1 #ifndef theplu_yat_statistics_euclidean_vector_distance_h
    2 #define theplu_yat_statistics_euclidean_vector_distance_h
     1#ifndef theplu_yat_statistics_euclidean_distance_h
     2#define theplu_yat_statistics_euclidean_distance_h
    33
    44// $Id$
     
    2727#include "AveragerPair.h"
    2828#include "AveragerPairWeighted.h"
    29 #include "vector_distance.h"
     29#include "distance.h"
    3030#include "yat/utility/iterator_traits.h"
    3131
     
    4141  /// the Euclidean distance measure.
    4242  ///
    43   struct euclidean_vector_distance_tag
    44     : public vector_distance_tag
     43  struct euclidean_distance_tag
     44    : public distance_tag
    4545  {
    4646    /// \brief tag for euclidean distance
    47     typedef euclidean_vector_distance_tag distance;
     47    typedef euclidean_distance_tag distance;
    4848  };
    4949
     
    5555  ///
    5656  template <typename Iter1, typename Iter2>
    57   double vector_distance(Iter1 beg1,Iter1 end1, Iter2 beg2,
    58                          const euclidean_vector_distance_tag& disttype,
     57  double distance(Iter1 beg1,Iter1 end1, Iter2 beg2,
     58                         const euclidean_distance_tag& disttype,
    5959                         utility::unweighted_type)
    6060  {
     
    7171  ///
    7272  template <typename Iter1, typename Iter2>
    73   double vector_distance(Iter1 beg1, Iter1 end1, Iter2 beg2,
    74                          const euclidean_vector_distance_tag& disttype,
     73  double distance(Iter1 beg1, Iter1 end1, Iter2 beg2,
     74                         const euclidean_distance_tag& disttype,
    7575                         utility::weighted_type)
    7676  {
  • trunk/yat/statistics/pearson_distance.h

    r1030 r1031  
    1 #ifndef theplu_yat_statistics_pearson_vector_distance_h
    2 #define theplu_yat_statistics_pearson_vector_distance_h
     1#ifndef theplu_yat_statistics_pearson_distance_h
     2#define theplu_yat_statistics_pearson_distance_h
    33
    44// $Id$
     
    2525*/
    2626
    27 #include "vector_distance.h"
     27#include "distance.h"
    2828
    2929#include "AveragerPair.h"
     
    4040  /// the Pearson distance measure.
    4141  ///
    42   struct pearson_vector_distance_tag
    43     : public vector_distance_tag
     42  struct pearson_distance_tag
     43    : public distance_tag
    4444  {
    4545    /// \brief tag for pearson distance
    46     typedef pearson_vector_distance_tag distance;
     46    typedef pearson_distance_tag distance;
    4747  };
    4848
     
    5454  ///
    5555  template <class Iter>
    56   double vector_distance(Iter beg1,Iter end1, Iter beg2,
    57                          const pearson_vector_distance_tag& disttype,
     56  double distance(Iter beg1,Iter end1, Iter beg2,
     57                         const pearson_distance_tag& disttype,
    5858                         utility::unweighted_type)
    5959  {
     
    6969  ///
    7070  template <class Iter>
    71   double vector_distance(Iter beg1,Iter end1, Iter beg2,
    72                          const pearson_vector_distance_tag& disttype,
     71  double distance(Iter beg1,Iter end1, Iter beg2,
     72                         const pearson_distance_tag& disttype,
    7373                         utility::weighted_type)
    7474  {
Note: See TracChangeset for help on using the changeset viewer.