source: trunk/lib/classifier/NCC.cc @ 525

Last change on this file since 525 was 525, checked in by Markus Ringnér, 17 years ago

Added structure to Supervised classifier and NCC to support ranking inputs with a statistics::Score and using only top-scoring inputs in classification.

File size: 3.1 KB
Line 
1// $Id$
2
3#include <c++_tools/classifier/NCC.h>
4
5#include <c++_tools/classifier/DataLookup1D.h>
6#include <c++_tools/classifier/DataLookup2D.h>
7#include <c++_tools/classifier/InputRanker.h>
8#include <c++_tools/classifier/Target.h>
9#include <c++_tools/gslapi/vector.h>
10#include <c++_tools/statistics/Distance.h>
11#include <c++_tools/utility/stl_utility.h>
12
13#include<iostream>
14#include<iterator>
15#include <map>
16#include <cmath>
17
18namespace theplu {
19namespace classifier {
20
21  NCC::NCC(const DataLookup2D& data, const Target& target, 
22           const statistics::Distance& distance) 
23    : SupervisedClassifier(target), distance_(distance), matrix_(data)
24  {   
25  }
26
27  NCC::NCC(const DataLookup2D& data, const Target& target, 
28           const statistics::Distance& distance, 
29           statistics::Score& score, size_t nof_inputs) 
30    : SupervisedClassifier(target, &score, nof_inputs), 
31      distance_(distance), matrix_(data)
32  {
33  }
34
35  NCC::~NCC()   
36  {
37    if(ranker_)
38      delete ranker_;
39  }
40
41
42  SupervisedClassifier* 
43  NCC::make_classifier(const DataLookup2D& data, 
44                       const Target& target) const 
45  {     
46    NCC* ncc= new NCC(data,target,this->distance_);
47    ncc->score_=this->score_;
48    ncc->nof_inputs_=this->nof_inputs_;
49    return ncc;
50  }
51
52
53  bool NCC::train()
54  {
55    if(ranker_)
56      delete ranker_;
57    if(score_)
58      ranker_=new InputRanker(matrix_, target_, *score_);
59    // Markus : ranker_ should be taken into account if used!!!
60
61    // Calculate the centroids for each class
62    centroids_=gslapi::matrix(matrix_.rows(),target_.nof_classes());
63    gslapi::matrix nof_in_class(matrix_.rows(),target_.nof_classes());
64    for(size_t i=0; i<matrix_.rows(); i++) {
65      for(size_t j=0; j<matrix_.columns(); j++) {
66        if(!std::isnan(matrix_(i,j))) {
67          centroids_(i,target_(j)) += matrix_(i,j);
68          nof_in_class(i,target_(j))++;
69        }
70      }
71    }
72    centroids_.div_elements(nof_in_class);
73    trained_=true;
74    return trained_;
75  }
76
77  void NCC::predict(const DataLookup1D& input, 
78                    gslapi::vector& prediction) const
79  {
80    // Markus : ranker_ should be taken into account if used!!!
81
82    prediction=gslapi::vector(centroids_.columns());   
83    gslapi::vector w(input.size(),0);
84    for(size_t i=0; i<input.size(); i++)  // take care of missing values
85      if(!std::isnan(input(i)))
86        w(i)=1.0;
87    for(size_t j=0; j<centroids_.columns(); j++) 
88      prediction(j)=distance_(gslapi::vector(input),
89                             gslapi::vector(centroids_,j,false),w, w);   
90  }
91
92
93  void NCC::predict(const DataLookup2D& input,                   
94                    gslapi::matrix& prediction) const
95  {
96    // Markus : ranker_ should be taken into account if used!!!
97   
98    prediction=gslapi::matrix(centroids_.columns(), input.columns());   
99    for(size_t j=0; j<input.columns();j++) {     
100      DataLookup1D in(input,j,true);
101      gslapi::vector out;
102      predict(in,out);
103      prediction.set_column(j,out);
104    }
105  }
106
107 
108  // additional operators
109
110  std::ostream& operator<< (std::ostream& s, const NCC& ncc) {
111//    std::copy(ncc.classes().begin(), ncc.classes().end(),
112//              std::ostream_iterator<std::map<double, u_int>::value_type>
113//              (s, "\n"));
114    s << "\n" << ncc.centroids() << "\n";
115    return s;
116  }
117
118}} // of namespace classifier and namespace theplu
Note: See TracBrowser for help on using the repository browser.