source: trunk/yat/classifier/NBC.cc @ 808

Last change on this file since 808 was 808, checked in by Peter, 15 years ago

previous argument was invalid, but here is an implementation. Fixes #205

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1// $Id: NBC.cc 808 2007-03-15 20:07:01Z peter $
2
3/*
4  Copyright (C) The authors contributing to this file.
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "NBC.h"
25#include "DataLookup2D.h"
26#include "MatrixLookup.h"
27#include "MatrixLookupWeighted.h"
28#include "Target.h"
29#include "yat/statistics/AveragerWeighted.h"
30#include "yat/utility/matrix.h"
31
32#include <cassert>
33#include <vector>
34
35namespace theplu {
36namespace yat {
37namespace classifier {
38
39  NBC::NBC(const MatrixLookup& data, const Target& target) 
40    : SupervisedClassifier(target), data_(data)
41  {
42  }
43
44  NBC::NBC(const MatrixLookupWeighted& data, const Target& target) 
45    : SupervisedClassifier(target), data_(data)
46  {
47  }
48
49  NBC::~NBC()   
50  {
51  }
52
53
54  const DataLookup2D& NBC::data(void) const
55  {
56    return data_;
57  }
58
59
60  SupervisedClassifier* 
61  NBC::make_classifier(const DataLookup2D& data, const Target& target) const 
62  {     
63    NBC* ncc=0;
64    if(data.weighted()) {
65      ncc=new NBC(dynamic_cast<const MatrixLookupWeighted&>(data),target);
66    }
67    else {
68      ncc=new NBC(dynamic_cast<const MatrixLookup&>(data),target);
69    }
70    return ncc;
71  }
72
73
74  bool NBC::train()
75  {   
76    sigma2_=centroids_=utility::matrix(data_.rows(), target_.nof_classes());
77    utility::matrix nof_in_class(data_.rows(), target_.nof_classes());
78   
79   
80    for(size_t i=0; i<data_.rows(); ++i) {
81      std::vector<statistics::AveragerWeighted> aver(target_.nof_classes());
82      for(size_t j=0; j<data_.columns(); ++j) {
83        if (data_.weighted()){
84          const MatrixLookupWeighted& data = 
85            dynamic_cast<const MatrixLookupWeighted&>(data_);
86          aver[target_(j)].add(data.data(i,j), data.weight(i,j));
87        }
88        else
89          aver[target_(j)].add(data_(i,j),1.0);
90      }
91      for (size_t j=0; target_.nof_classes(); ++j){
92        centroids_(i,j) = aver[j].mean();
93        sigma2_(i,j) = aver[j].variance();
94      }
95    }   
96    trained_=true;
97    return trained_;
98  }
99
100
101  void NBC::predict(const DataLookup2D& input,                   
102                    utility::matrix& prediction) const
103  {   
104    std::cerr << "NBC::predict not implemented\n";
105    exit(1);
106    assert(data_.rows()==input.rows());
107
108    //    utility
109    //for (size_t i=0;
110
111
112    prediction = utility::matrix(centroids_.columns(),input.columns());
113    for (size_t c=0; c<centroids_.columns(); ++c) {
114      double sum_ln_sigma=0;
115      for (size_t i=0; i<sigma2_.rows(); ++i) 
116        sum_ln_sigma += log(sigma2_(i,c));
117      sum_ln_sigma /= 2;
118
119      for (size_t s=0; s<input.columns(); ++s) {
120        // -lnp = sum{ln(sigma_i)} + sum{(x_i-m_i)^2/(2sigma_i)}
121        prediction(c,s) = sum_ln_sigma;
122        for (size_t i=0; i<input.columns(); ++i) {
123          prediction(c,s) += std::pow(input(i,s)-centroids_(i,c),2)/sigma2_(i,c);
124        }
125      }
126    }
127    // exponentiate and normalize
128  }
129
130
131}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.