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

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

Closes #129

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1// $Id: NBC.cc 722 2006-12-27 20:49:11Z markus $
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 <vector>
33
34namespace theplu {
35namespace yat {
36namespace classifier {
37
38  NBC::NBC(const MatrixLookup& data, const Target& target) 
39    : SupervisedClassifier(target), data_(data)
40  {
41  }
42
43  NBC::NBC(const MatrixLookupWeighted& data, const Target& target) 
44    : SupervisedClassifier(target), data_(data)
45  {
46  }
47
48  NBC::~NBC()   
49  {
50  }
51
52
53    const DataLookup2D& NBC::data(void) const
54    {
55    return data_;
56    }
57
58
59  SupervisedClassifier* 
60  NBC::make_classifier(const DataLookup2D& data, const Target& target) const 
61  {     
62    NBC* ncc=0;
63    if(data.weighted()) {
64      ncc=new NBC(dynamic_cast<const MatrixLookupWeighted&>(data),target);
65    }
66    else {
67      ncc=new NBC(dynamic_cast<const MatrixLookup&>(data),target);
68    }
69    return ncc;
70  }
71
72
73  bool NBC::train()
74  {   
75    sigma_=centroids_=utility::matrix(data_.rows(), target_.nof_classes());
76    utility::matrix nof_in_class(data_.rows(), target_.nof_classes());
77   
78   
79    for(size_t i=0; i<data_.rows(); ++i) {
80      std::vector<statistics::AveragerWeighted> aver(target_.nof_classes());
81      for(size_t j=0; j<data_.columns(); ++j) {
82        if (data_.weighted()){
83          const MatrixLookupWeighted& data = 
84            dynamic_cast<const MatrixLookupWeighted&>(data_);
85            aver[target_(j)].add(data.data(i,j), data.weight(i,j));
86        }
87        else
88          aver[target_(j)].add(data_(i,j),1.0);
89      }
90      for (size_t j=0; target_.nof_classes(); ++j){
91        centroids_(i,j) = aver[j].mean();
92        sigma_(i,j) = aver[j].variance();
93      }
94    }   
95    trained_=true;
96    return trained_;
97  }
98
99
100  void NBC::predict(const DataLookup2D& input,                   
101                    utility::matrix& prediction) const
102  {   
103    std::cerr << "NBC::predict not implemented\n";
104    exit(1);
105  }
106
107
108}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.