source: trunk/c++_tools/classifier/NBC.cc @ 675

Last change on this file since 675 was 675, checked in by Jari Häkkinen, 17 years ago

References #83. Changing project name to yat. Compilation will fail in this revision.

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