1#ifndef FSTCLASSIFIERMULTINOMNAIVEBAYES_H
2#define FSTCLASSIFIERMULTINOMNAIVEBAYES_H
112template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
113class Classifier_Multinomial_NaiveBayes :
public Clonable_Impl<Classifier_Multinomial_NaiveBayes<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>,Classifier<RETURNTYPE,DIMTYPE,SUBSET,DATAACCESSOR> > {
117 typedef std::shared_ptr<SUBSET>
const PSubset;
119 Classifier_Multinomial_NaiveBayes();
120 virtual ~Classifier_Multinomial_NaiveBayes() {
notify(
"Classifier_Multinomial_NaiveBayes destructor.");}
144 if(!
classify(cls,pattern))
return false;
146 for(DIMTYPE c=0;c<
_classes;c++) scores[c]=(RETURNTYPE)
_Pcd[c];
153 classes=da->getNoOfClasses();
154 if(classes<2)
return false;
155 confusion.assign((
size_t)classes*(
size_t)classes,(RETURNTYPE)0.0);
169 virtual std::ostream&
print(std::ostream& os)
const override {os <<
"Classifier_Multinomial_NaiveBayes()";
return os;}
172 Classifier_Multinomial_NaiveBayes(
const Classifier_Multinomial_NaiveBayes& cmnb);
174 std::unique_ptr<Model_Multinomial<DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR> >
_model;
178 std::unique_ptr<REALTYPE[]>
_Pcd;
186template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
187Classifier_Multinomial_NaiveBayes<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>::Classifier_Multinomial_NaiveBayes(
const Classifier_Multinomial_NaiveBayes& cmnb) :
193 notify(
"Classifier_Multinomial_NaiveBayes copy-constructor.");
207template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
208Classifier_Multinomial_NaiveBayes<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>::Classifier_Multinomial_NaiveBayes()
210 notify(
"Classifier_Multinomial_NaiveBayes constructor.");
215 _prelearn_mode=
false;
218template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
221 notify(
"Classifier_Multinomial_NaiveBayes::init_prelearn_mode().");
224 assert(da->getNoOfFeatures()>0);
225 assert(da->getNoOfClasses()>0);
230template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
233 notify(
"Classifier_Multinomial_NaiveBayes::initialize().");
235 assert(da->getNoOfFeatures()>0);
236 assert(da->getNoOfClasses()>0);
238 if(!
_Pcd ||
_classes!=da->getNoOfClasses())
_Pcd.reset(
new REALTYPE[da->getNoOfClasses()]);
245template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
254 notify(
"Classifier_Multinomial_NaiveBayes::train().");
258 assert(da->getNoOfFeatures()>0);
260 assert(sub->get_frozen_mode()==
false);
262 assert(sub->get_n_raw()==da->getNoOfFeatures());
281template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
287 notify(
"Classifier_Multinomial_NaiveBayes::classify().");
290 assert(
_model->get_classes()>1);
304 REALTYPE *theta=&(
_model->get_theta()[0]);
315 for(c_cand=0;c_cand<
_classes;c_cand++)
317 theta_tmp=&theta[wTH];
321 res+=(REALTYPE)pattern[
_index[f]] * log(theta_tmp[f]);
328 for(c_cand=1;c_cand<
_classes;c_cand++)
if(
_Pcd[c_cand]>res) {res=
_Pcd[c_cand]; cls=c_cand;}
333template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
338 notify(
"Classifier_Multinomial_NaiveBayes::test().");
341 assert(
_model->get_classes()>1);
345 assert(da->getNoOfClasses()>0);
346 assert(da->getNoOfClasses()==
_classes);
352 typename DATAACCESSOR::PPattern p;
354 IDXTYPE count, correct;
367 for(DIMTYPE c_test=0;c_test<
_classes;c_test++)
369 da->setClass(c_test);
370 for(
const auto &blk : da->blocks(
TEST))
for(i=0,p=blk.data,s=blk.size;i<s;i++)
373 if(clstmp==c_test) correct++;
379 result=(RETURNTYPE)correct/(RETURNTYPE)count;
382 std::ostringstream sos; sos <<
" result=" << result << std::endl;
std::unique_ptr< Model_Multinomial< DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR > > _model
the underlying multinomial model, learned/narrowed by train()
Definition classifier_multinom_naivebayes.hpp:174
virtual bool test_confusion(std::vector< RETURNTYPE > &confusion, DIMTYPE &classes, const PDataAccessor da) override
Definition classifier_multinom_naivebayes.hpp:149
DIMTYPE get_d() const
the underlying model's current virtual (narrowed) dimensionality (Model_Multinomial::get_d())
Definition classifier_multinom_naivebayes.hpp:165
DIMTYPE _classes
class count, sized by initialize() (dimensions _Pcd)
Definition classifier_multinom_naivebayes.hpp:176
void initialize(const PDataAccessor da)
Definition classifier_multinom_naivebayes.hpp:231
DATAACCESSOR::PPattern PPattern
one data sample as exposed by DATAACCESSOR (a raw pointer to its feature vector)
Definition classifier_multinom_naivebayes.hpp:118
std::shared_ptr< DATAACCESSOR > PDataAccessor
shared pointer to the data accessor supplying train/test blocks
Definition classifier_multinom_naivebayes.hpp:116
DIMTYPE _features
raw feature count, sized by initialize() (dimensions _index)
Definition classifier_multinom_naivebayes.hpp:177
void disable_prelearn_mode()
reverts to the default (per-train() relearning) mode; see enable_prelearn_mode()
Definition classifier_multinom_naivebayes.hpp:128
bool _prelearn_mode
see enable_prelearn_mode()/disable_prelearn_mode()/get_prelearn_mode()
Definition classifier_multinom_naivebayes.hpp:181
bool get_prelearn_mode() const
true if enable_prelearn_mode() is currently in effect
Definition classifier_multinom_naivebayes.hpp:129
virtual bool classify(DIMTYPE &cls, const PPattern &pattern) override
Definition classifier_multinom_naivebayes.hpp:282
virtual bool test(RETURNTYPE &result, const PDataAccessor da) override
Definition classifier_multinom_naivebayes.hpp:334
virtual bool train(const PDataAccessor da, const PSubset sub) override
learns from designated training part of data
Definition classifier_multinom_naivebayes.hpp:246
DIMTYPE _subfeatures
number of features in the currently trained subset (sub->get_d_raw() at the last train() call)
Definition classifier_multinom_naivebayes.hpp:183
DIMTYPE get_n() const
the underlying model's full ("raw") learned dimensionality (Model_Multinomial::get_n())
Definition classifier_multinom_naivebayes.hpp:164
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition classifier_multinom_naivebayes.hpp:169
std::unique_ptr< DIMTYPE[]> _index
subset position -> raw feature index (first _subfeatures entries valid), filled by train()
Definition classifier_multinom_naivebayes.hpp:182
Clonable_Impl< Classifier_Multinomial_NaiveBayes, Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > > parent_impl
implements clone(); sharing/stateless_clone throw in Classifier
Definition classifier_multinom_naivebayes.hpp:115
std::unique_ptr< REALTYPE[]> _Pcd
per-class score of the pattern last passed to classify(): the UNNORMALIZED log-posterior ln P(c) + su...
Definition classifier_multinom_naivebayes.hpp:178
std::shared_ptr< SUBSET > const PSubset
shared pointer to the (fixed, non-reassignable) feature subset being trained/evaluated on
Definition classifier_multinom_naivebayes.hpp:117
void enable_prelearn_mode(const PDataAccessor da)
Definition classifier_multinom_naivebayes.hpp:219
virtual bool classify_scores(std::vector< RETURNTYPE > &scores, const PPattern &pattern) override
Definition classifier_multinom_naivebayes.hpp:139
virtual bool test_confusion(std::vector< RETURNTYPE > &confusion, DIMTYPE &classes, const PDataAccessor da)
Definition classifier.hpp:181
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Multinomial ("bag of words") model: per-class term-count sums and the Laplace-smoothed term probabili...
Definition model_multinom.hpp:120
static void print(std::ostream &os, std::ostringstream &sos)
Writes sos's buffered content to os, serializing concurrent writers under FST_THREADED via a shared m...
Definition global.hpp:86
Defines interface for classifier implementations.
FST4 specific error passing class.
Implements multinomial model.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
void notify(const std::string msg)
Prints msg followed by a newline via syncout::print(); a no-op unless the preprocessor macro DEBUG is...
Definition global.hpp:112
@ TEST
the split's test part
Definition data_accessor.hpp:86