Changeset 898


Ignore:
Timestamp:
Sep 26, 2007, 3:44:19 PM (14 years ago)
Author:
Markus Ringnér
Message:

A first suggestion for how to adress #250. Also removed contamination of namespace std (see #251).

Location:
trunk
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/iterator_test.cc

    r883 r898  
    2424#include "yat/classifier/DataLookup1D.h"
    2525
     26#include "yat/classifier/DataLookupWeighted1D.h"
     27#include "yat/classifier/MatrixLookupWeighted.h"
    2628#include "yat/utility/Iterator.h"
     29#include "yat/utility/IteratorWeighted.h"
     30#include "yat/utility/matrix.h"
    2731#include "yat/utility/vector.h"
    2832
     
    6367  std::sort(begin, end);
    6468
     69  // test std algorithm on IteratorWeighted
     70  utility::matrix m(1,3,1);
     71  m(0,1)=2.0;
     72  utility::matrix w(1,3,1);
     73  classifier::MatrixLookupWeighted mw(m,w);
     74  classifier::DataLookupWeighted1D aw(mw,0,true);
     75  size_t nof1=std::count(aw.begin(),aw.end(),1.0);
     76  if(nof1!=2) {
     77    *message << "std algoritm with IteratorWeighted failed" << std::endl;
     78    ok=false;
     79  }
     80
    6581  if (!ok)
    6682    *message << "iterator test failed" << std::endl;
  • trunk/test/ncc_test.cc

    r865 r898  
    2828#include "yat/classifier/Target.h"
    2929#include "yat/utility/matrix.h"
    30 #include "yat/statistics/PearsonDistance.h"
     30#include "yat/statistics/vector_distance_ptr.h"
     31#include "yat/statistics/pearson_vector_distance.h"
    3132#include "yat/utility/utility.h"
    3233
     
    7172     
    7273  classifier::MatrixLookupWeighted dataviewweighted(data,weights);
    73   statistics::PearsonDistance pearson; 
    74   classifier::NCC ncc(dataviewweighted,targets,pearson);
     74  statistics::vector_distance_lookup_weighted_ptr distance=
     75    statistics::vector_distance<statistics::pearson_vector_distance_tag>;
     76  classifier::NCC ncc(dataviewweighted,targets,distance);
    7577  ncc.train();
    7678
     
    127129 
    128130
    129   // Testing IGP 
    130   classifier:: MatrixLookup dataview(data);
    131   *error << "testing igp" << std::endl;
    132   classifier::IGP igp(dataview,targets,pearson);
    133   *error << igp.score() << std::endl;
    134 
    135131  if(ok)
    136132    *error << "OK" << std::endl;
  • trunk/test/vector_distance_test.cc

    r897 r898  
    55#include "yat/statistics/euclidean_vector_distance.h"
    66#include "yat/statistics/pearson_vector_distance.h"
     7#include "yat/statistics/vector_distance_ptr.h"
    78#include "yat/utility/matrix.h"
    89#include "yat/utility/vector.h"
     
    1415
    1516using namespace theplu::yat;
     17
     18
     19// Function to test pointers to distance specialized for DataLookup1D::iterator
     20double f(statistics::vector_distance_lookup_weighted_ptr distance) {
     21  utility::matrix m(2,3,1);
     22  m(0,1)=2;
     23  m(1,0)=0;
     24  m(1,1)=0;
     25  utility::matrix w(2,3,1);
     26  w(0,0)=0;
     27  classifier::MatrixLookupWeighted mw(m,w);
     28  classifier::DataLookupWeighted1D aw(mw,0,true);
     29  classifier::DataLookupWeighted1D bw(mw,1,true);
     30 
     31  double dist=(*distance)(aw.begin(),aw.end(),bw.begin());
     32  return dist; 
     33}
    1634
    1735int main(const int argc,const char* argv[])
     
    6381 
    6482  dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(),
    65                                           statistics::euclidean_vector_distance_tag());
     83                                   statistics::euclidean_vector_distance_tag());
    6684 
    6785  if(fabs(dist-2)>tolerance) {
     
    7997
    8098
    81   // Test with reference to a vector_distance_tag
    82   statistics::euclidean_vector_distance_tag tag;
    83   statistics::vector_distance_tag& tagref=tag;
    84   dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(),tagref);
    85   *error << "Dist: " << dist << std::endl;
     99  // Test with std::vectors
     100  std::vector<double> sa(3,1);
     101  sa[1] = 2;
     102  std::vector<double> sb(3,0);
     103  sb[2] = 1;
    86104
     105  dist=statistics::vector_distance(sa.begin(),sa.end(),sb.begin(),
     106                                   statistics::euclidean_vector_distance_tag());  if(fabs(dist-2.23607)>tolerance) {
     107    *error << "Error in vector_distance for std::vector: " << std::endl;
     108    ok=false;
     109  }
     110
     111
     112  // Test with pointer to a vector_distance
     113  statistics::vector_distance_lookup_weighted_ptr test_ptr=
     114    statistics::vector_distance<statistics::euclidean_vector_distance_tag>;
     115  dist=(*test_ptr)(aw.begin(),aw.end(),bw.begin());
     116  if(fabs(dist-2)>tolerance) {
     117    *error << "Error when using pointer to vector_distance" << std::endl;
     118    ok=false;
     119  }
     120
     121  if(!ok) {
     122    *error << "vector_distance_test failed" << std::endl;
     123  }
    87124  if (error!=&std::cerr)
    88125    delete error;
    89 
    90126  if (ok=true) 
    91127    return 0;
  • trunk/yat/classifier/NCC.cc

    r874 r898  
    2727#include "DataLookup1D.h"
    2828#include "DataLookup2D.h"
     29#include "DataLookupWeighted1D.h"
    2930#include "MatrixLookup.h"
    3031#include "MatrixLookupWeighted.h"
    3132#include "Target.h"
     33#include "yat/statistics/vector_distance.h"
     34#include "yat/statistics/euclidean_vector_distance.h"
     35#include "yat/utility/Iterator.h"
     36#include "yat/utility/IteratorWeighted.h"
    3237#include "yat/utility/matrix.h"
    3338#include "yat/utility/vector.h"
    34 #include "yat/statistics/Distance.h"
    3539#include "yat/utility/stl_utility.h"
    3640
     
    4549
    4650  NCC::NCC(const MatrixLookup& data, const Target& target,
    47            const statistics::Distance& distance)
     51           const statistics::vector_distance_lookup_weighted_ptr distance)
    4852    : SupervisedClassifier(target), distance_(distance), data_(data)
    4953  {
     
    5155
    5256  NCC::NCC(const MatrixLookupWeighted& data, const Target& target,
    53            const statistics::Distance& distance)
     57           const statistics::vector_distance_lookup_weighted_ptr distance)
    5458    : SupervisedClassifier(target), distance_(distance), data_(data)
    5559  {
     
    6569    return centroids_;
    6670  }
     71 
    6772
    68     const DataLookup2D& NCC::data(void) const
    69     {
     73  const DataLookup2D& NCC::data(void) const
     74  {
    7075    return data_;
    71     }
    72 
     76  }
     77 
    7378  SupervisedClassifier*
    7479  NCC::make_classifier(const DataLookup2D& data, const Target& target) const
     
    109114  }
    110115
    111 
    112   void NCC::predict(const utility::vector& input, const utility::vector& weights,
    113                     utility::vector& prediction) const
    114   {
    115     prediction.clone(utility::vector(centroids_.columns()));
    116    
    117     // take care of nan's in centroids
    118     for(size_t j=0; j<centroids_.columns(); j++) {
    119       const utility::vector centroid(utility::vector(centroids_,j,false));
    120       utility::vector wc(centroid.size(),0);
    121       for(size_t i=0; i<centroid.size(); i++)  {
    122         if(!std::isnan(centroid(i)))
    123           wc(i)=1.0;
    124       }
    125       prediction(j)=distance_(input,centroid,weights,wc);   
    126     }
    127   }
    128 
    129 
    130116  void NCC::predict(const DataLookup2D& input,                   
    131117                    utility::matrix& prediction) const
    132118  {   
    133     prediction.clone(utility::matrix(centroids_.columns(), input.columns()));
    134     // weighted case
    135     const MatrixLookupWeighted* data =
    136       dynamic_cast<const MatrixLookupWeighted*>(&input); 
    137     if (data) {
    138       for(size_t j=0; j<input.columns();j++) {     
    139         utility::vector in(input.rows(),0);
    140         for(size_t i=0; i<in.size();i++)
    141           in(i)=data->data(i,j);
    142         utility::vector weights(in.size(),0);
    143         for(size_t i=0; i<in.size();i++)
    144           weights(i)=data->weight(i,j);
    145         utility::vector out;
    146         predict(in,weights,out);
    147         prediction.column(j,out);
     119    prediction.clone(utility::matrix(centroids_.columns(), input.columns()));   
     120
     121    // Weighted case
     122    const MatrixLookupWeighted* testdata =
     123      dynamic_cast<const MatrixLookupWeighted*>(&input);     
     124    if (testdata) {
     125      utility::matrix centroid_weights;
     126      utility::nan(centroids_,centroid_weights);
     127      MatrixLookupWeighted weighted_centroids(centroids_,centroid_weights);
     128      for(size_t j=0; j<input.columns();j++) {       
     129        DataLookupWeighted1D in(*testdata,j,false);
     130        for(size_t k=0; k<centroids_.columns();k++) {
     131          DataLookupWeighted1D centroid(weighted_centroids,k,false);
     132          prediction(k,j)=(*distance_)(in.begin(),in.end(),centroid.begin());
     133        }
    148134      }
    149       return;
    150135    }
    151     // non-weighted case
    152     const MatrixLookup* x = dynamic_cast<const MatrixLookup*>(&input);
    153     if (!x){
     136    else {
    154137      std::string str;
    155138      str = "Error in NCC::predict: DataLookup2D of unexpected class.";
    156139      throw std::runtime_error(str);
    157140    }
    158     for(size_t j=0; j<input.columns();j++) {     
    159       utility::vector in(input.rows(),0);
    160       for(size_t i=0; i<in.size();i++)
    161         in(i)=(*data)(i,j);
    162       utility::vector weights(in.size(),1.0);
    163       utility::vector out;
    164       predict(in,weights,out);
    165       prediction.column(j,out);
    166     }
    167141  }
    168 
    169  
    170   // additional operators
    171 
    172 //  std::ostream& operator<< (std::ostream& s, const NCC& ncc) {
    173 //    std::copy(ncc.classes().begin(), ncc.classes().end(),
    174 //              std::ostream_iterator<std::map<double, u_int>::value_type>
    175 //              (s, "\n"));
    176 //    s << "\n" << ncc.centroids() << "\n";
    177 //    return s;
    178 //  }
    179 
     142   
    180143}}} // of namespace classifier, yat, and theplu
  • trunk/yat/classifier/NCC.h

    r874 r898  
    2828
    2929#include "yat/utility/matrix.h"
     30#include "yat/statistics/vector_distance_ptr.h"
    3031#include "SupervisedClassifier.h"
    3132
     
    3738  namespace utlitity {
    3839    class vector;
    39   }
    40 
    41   namespace statistics {
    42     class Distance;
    4340  }
    4441
     
    6158    ///
    6259    /// Constructor taking the training data, the target vector, and
    63     /// the distance measure as input.
     60    /// the distance measure tag as input.
    6461    ///
    65     NCC(const MatrixLookup&, const Target&, const statistics::Distance&);
     62    NCC(const MatrixLookup&, const Target&,
     63        const statistics::vector_distance_lookup_weighted_ptr);
    6664   
    6765    ///
    6866    /// Constructor taking the training data with weights, the target
    69     /// vector, the distance measure, and a weight matrix for the
    70     /// training data as input.
     67    /// vector, the distance measure tag.
    7168    ///
    72     NCC(const MatrixLookupWeighted&, const Target&, const statistics::Distance&);
     69    NCC(const MatrixLookupWeighted&, const Target&,
     70        const statistics::vector_distance_lookup_weighted_ptr);
    7371
    7472    virtual ~NCC();
     
    9795    ///
    9896    void predict(const DataLookup2D&, utility::matrix&) const;
    99 
    100 
     97   
     98   
    10199  private:
    102100    utility::matrix centroids_;
    103     const statistics::Distance& distance_;                 
     101    const statistics::vector_distance_lookup_weighted_ptr distance_;                 
    104102
    105103    // data_ has to be of type DataLookup2D to accomodate both
    106104    // MatrixLookup and MatrixLookupWeighted
    107105    const DataLookup2D& data_;
    108 
    109     ///
    110     /// Calculate the distance to each centroid for a test sample
    111     ///
    112     void predict(const utility::vector&, const utility::vector&,
    113                  utility::vector&) const;
    114106
    115107  };
  • trunk/yat/statistics/euclidean_vector_distance.h

    r897 r898  
    5050  double vector_distance(Iter1 beg1, Iter1 end1, Iter2 beg2,
    5151                         const euclidean_vector_distance_tag& disttype,
    52                          std::weighted_random_access_iterator_tag)
     52                         utility::weighted_random_access_iterator_tag)
    5353  {
    5454    AveragerPairWeighted ap;
  • trunk/yat/statistics/pearson_vector_distance.h

    r897 r898  
    77
    88#include "AveragerPair.h"
     9#include "AveragerPairWeighted.h"
    910
    1011namespace theplu {
     
    4748  double vector_distance(Iter beg1,Iter end1, Iter beg2,
    4849                         const pearson_vector_distance_tag& disttype,
    49                          std::weighted_random_access_iterator_tag)
     50                         utility::weighted_random_access_iterator_tag)
    5051  {
    5152    AveragerPairWeighted ap;
  • trunk/yat/statistics/vector_distance.h

    r897 r898  
    66#include <iterator>
    77
     8#include "yat/classifier/DataLookup1D.h"
     9#include "yat/classifier/DataLookupWeighted1D.h"
     10
    811namespace theplu {
    912namespace yat {
    1013
    1114namespace statistics {
    12 
     15 
    1316  ///
    1417  /// For each measure to calculate distances between containers
     
    5255                           typename std::iterator_traits<Iter1>::iterator_category());
    5356  }       
     57
     58  /// A partial specialization for DataLookupWeighted1D
     59  template<typename Dist>
     60  double vector_distance(const classifier::DataLookupWeighted1D::const_iterator& beg1,const classifier::DataLookupWeighted1D::const_iterator& end1,const classifier::DataLookupWeighted1D::const_iterator& beg2)
     61  {
     62    return vector_distance(beg1,end1,beg2, typename statistics::vector_distance_traits<Dist>::distance());
     63  }
    5464 
    55   // Temporary function of where I end up when calling with a reference to distance_tag
    56   template <typename Iter1, typename Iter2>
    57   double vector_distance(Iter1 beg1, Iter1 end1, Iter2 beg2,
    58                          const vector_distance_tag& disttag)
     65  /// A partial specialization for DataLookup1D
     66  template<typename Dist>
     67  double vector_distance(const classifier::DataLookup1D::const_iterator& beg1,const classifier::DataLookup1D::const_iterator& end1,const classifier::DataLookup1D::const_iterator& beg2)
    5968  {
    60     return 0;
    61   }       
     69    return vector_distance(beg1,end1,beg2, typename statistics::vector_distance_traits<Dist>::distance());
     70  }
    6271
    6372 
  • trunk/yat/utility/IteratorWeighted.h

    r897 r898  
    77#include <stddef.h>
    88
    9 namespace std {
    10   struct weighted_random_access_iterator_tag
    11     : public random_access_iterator_tag {
    12   };
    13 }
     9// namespace std {
     10// struct weighted_random_access_iterator_tag
     11//    : public random_access_iterator_tag {
     12// };
     13// }
    1414
    1515
     
    1717namespace yat {
    1818namespace utility {
     19
     20  struct weighted_random_access_iterator_tag
     21    : public std::random_access_iterator_tag {
     22  };
    1923
    2024  /**
     
    2529  {
    2630  public:
    27     typedef std::weighted_random_access_iterator_tag iterator_category;
     31    typedef weighted_random_access_iterator_tag iterator_category;
    2832    typedef double value_type;
    2933    typedef size_t difference_type;
Note: See TracChangeset for help on using the changeset viewer.