1#ifndef FSTCLASSIFIERNORMALBAYES_H
2#define FSTCLASSIFIERNORMALBAYES_H
55#define M_PI 3.14159265358979323846
115template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
116class Classifier_Normal_Bayes :
public Clonable_Impl<Classifier_Normal_Bayes<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>,Classifier<RETURNTYPE,DIMTYPE,SUBSET,DATAACCESSOR> > {
120 typedef std::shared_ptr<SUBSET>
const PSubset;
122 Classifier_Normal_Bayes();
123 virtual ~Classifier_Normal_Bayes() {
notify(
"Classifier_Normal_Bayes destructor.");}
146 if(!
classify(cls,pattern))
return false;
148 for(DIMTYPE c=0;c<
_classes;c++) scores[c]=(RETURNTYPE)
_Pwx[c];
159 virtual std::ostream&
print(std::ostream& os)
const override {os <<
"Classifier_Normal_Bayes()";
return os;}
164 Classifier_Normal_Bayes(
const Classifier_Normal_Bayes& cnb);
166 std::unique_ptr<Model_Normal<DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR> >
_model;
170 std::unique_ptr<Indexed_Matrix<REALTYPE,DIMTYPE,SUBSET> >
_LUtemp;
171 std::unique_ptr<Indexed_Matrix<REALTYPE,DIMTYPE,SUBSET>[]>
_inverse;
172 std::unique_ptr<REALTYPE[]>
_det;
174 std::unique_ptr<REALTYPE[]>
_pxw;
175 std::unique_ptr<REALTYPE[]>
_Pwx;
185template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
186Classifier_Normal_Bayes<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>::Classifier_Normal_Bayes(
const Classifier_Normal_Bayes& cnb) :
192 notify(
"Classifier_Normal_Bayes copy-constructor.");
211template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
212Classifier_Normal_Bayes<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>::Classifier_Normal_Bayes()
214 notify(
"Classifier_Normal_Bayes constructor.");
219 _prelearn_mode=
false;
222template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
225 notify(
"Classifier_Normal_Bayes::init_prelearn_mode().");
228 assert(da->getNoOfFeatures()>0);
229 assert(da->getNoOfClasses()>0);
234template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
237 notify(
"Classifier_Normal_Bayes::initialize().");
239 assert(da->getNoOfFeatures()>0);
240 assert(da->getNoOfClasses()>0);
244 if(da->getNoOfFeatures()>
_LUtemp->get_n_max())
_LUtemp->reset(da->getNoOfFeatures(),
true);
246 for(DIMTYPE c=0;c<da->getNoOfClasses();c++)
if(da->getNoOfFeatures()>
_inverse[c].get_n_max())
_inverse[c].reset(da->getNoOfFeatures());
247 if(!
_det ||
_classes!=da->getNoOfClasses())
_det.reset(
new REALTYPE[da->getNoOfClasses()]);
249 if(!
_pxw ||
_classes!=da->getNoOfClasses())
_pxw.reset(
new REALTYPE[da->getNoOfClasses()]);
250 if(!
_Pwx ||
_classes!=da->getNoOfClasses())
_Pwx.reset(
new REALTYPE[da->getNoOfClasses()]);
259template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
268 notify(
"Classifier_Normal_Bayes::train().");
272 assert(da->getNoOfFeatures()>0);
274 assert(sub->get_frozen_mode()==
false);
276 assert(sub->get_n_raw()==da->getNoOfFeatures());
282 REALTYPE tmpval=2.0*
M_PI;
for(DIMTYPE f=1;f<sub->get_d_raw();f++) tmpval*=2.0*
M_PI;
283 _LUtemp->redim(sub->get_d_raw());
284 for(DIMTYPE c=0; c<
_model->get_classes(); c++)
303 std::ostringstream sos; sos <<
"index: ";
for(f=0;f<
_subfeatures;f++) sos <<
_index[f] <<
" "; sos << std::endl;
311template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
318 notify(
"Classifier_Normal_Bayes::classify().");
321 assert(
_model->get_classes()>1);
329 REALTYPE ecoef, px, tmpval;
338 for(c_cand=0;c_cand<
_classes;c_cand++)
360 if(!(px>(REALTYPE)0.0) || !std::isfinite(px)) {
361 REALTYPE lbest=0.0, lc;
362 for(c_cand=0;c_cand<
_classes;c_cand++) {
365 if(c_cand==0 || lc>lbest) {lbest=lc; cls=c_cand;}
369 for(c_cand=0;c_cand<
_classes;c_cand++) {
_Pwx[c_cand]=exp(
_Pwx[c_cand]-lbest); psum+=
_Pwx[c_cand];}
376 tmpval=
_Pwx[0]; cls=0;
377 for(c_cand=1;c_cand<
_classes;c_cand++)
if(
_Pwx[c_cand]>tmpval) {tmpval=
_Pwx[c_cand]; cls=c_cand;}
382template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
388 notify(
"Classifier_Normal_Bayes::test().");
391 assert(
_model->get_classes()>1);
395 else assert(da->getNoOfFeatures()>=
get_n());
396 assert(da->getNoOfClasses()>0);
401 typename DATAACCESSOR::PPattern p;
403 IDXTYPE count, correct;
410 for(DIMTYPE c_test=0;c_test<
_classes;c_test++)
412 da->setClass(c_test);
413 for(
const auto &blk : da->blocks(
TEST))
for(i=0,p=blk.data,s=blk.size;i<s;i++)
416 if(clstmp==c_test) correct++;
421 result=(RETURNTYPE)correct/(RETURNTYPE)count;
424 std::ostringstream sos; sos <<
" result=" << result << std::endl;
DIMTYPE _classes
number of classes; sizes the per-class arrays below
Definition classifier_normal_bayes.hpp:168
DIMTYPE _features
raw (full) feature count
Definition classifier_normal_bayes.hpp:169
std::unique_ptr< REALTYPE[]> _pxw
per-class likelihood p(x|class), cached by classify() (lazy allocation)
Definition classifier_normal_bayes.hpp:174
std::unique_ptr< REALTYPE[]> _det
per-class covariance determinant (lazy allocation)
Definition classifier_normal_bayes.hpp:172
std::unique_ptr< REALTYPE[]> _log_constant
ln of _constant, computed without forming _constant itself, for the log-domain fallback in classify()...
Definition classifier_normal_bayes.hpp:176
DIMTYPE _subfeatures
current subset size (== sub->get_d_raw() at the last train() call)
Definition classifier_normal_bayes.hpp:182
void enable_prelearn_mode(const PDataAccessor da)
Definition classifier_normal_bayes.hpp:223
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET >[]> _inverse
per-class inverse covariance matrix (lazy allocation)
Definition classifier_normal_bayes.hpp:171
Clonable_Impl< Classifier_Normal_Bayes, Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > > parent_impl
implements clone(); sharing/stateless_clone throw in Classifier
Definition classifier_normal_bayes.hpp:118
std::unique_ptr< REALTYPE[]> _constant
per-class normalizing constant sqrt((2*pi)^d * _det[c]) (lazy allocation)
Definition classifier_normal_bayes.hpp:173
virtual bool test(RETURNTYPE &result, const PDataAccessor da) override
Definition classifier_normal_bayes.hpp:383
virtual bool train(const PDataAccessor da, const PSubset sub) override
learns from designated training part of data
Definition classifier_normal_bayes.hpp:260
virtual bool classify_scores(std::vector< RETURNTYPE > &scores, const PPattern &pattern) override
Definition classifier_normal_bayes.hpp:143
std::unique_ptr< REALTYPE[]> _ecoef
per-class Mahalanobis terms buffered for the fallback (lazy allocation)
Definition classifier_normal_bayes.hpp:177
DIMTYPE get_n() const
"full feature set" dimensionality the model was learned at (see Model_Normal::get_n())
Definition classifier_normal_bayes.hpp:154
std::shared_ptr< DATAACCESSOR > PDataAccessor
shared pointer to the data accessor supplying train/test blocks
Definition classifier_normal_bayes.hpp:119
void initialize(const PDataAccessor da)
(Re)allocates the per-class inverse-covariance, determinant, constant, posterior and log-domain-fallb...
Definition classifier_normal_bayes.hpp:235
std::unique_ptr< REALTYPE[]> _Pwx
per-class posterior P(class|x), cached by classify()/classify_scores() (lazy allocation)
Definition classifier_normal_bayes.hpp:175
bool get_prelearn_mode() const
true if pre-learn mode is active (see enable_prelearn_mode())
Definition classifier_normal_bayes.hpp:132
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET > > _LUtemp
shared LU-decomposition scratch matrix, reused across classes in train()
Definition classifier_normal_bayes.hpp:170
std::unique_ptr< Model_Normal< DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR > > _model
the underlying normal (Gaussian) model supplying per-class mean/covariance
Definition classifier_normal_bayes.hpp:166
DIMTYPE get_d() const
dimensionality currently exposed by the model (see Model_Normal::get_d())
Definition classifier_normal_bayes.hpp:155
virtual bool classify(DIMTYPE &cls, const PPattern &pattern) override
Definition classifier_normal_bayes.hpp:312
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition classifier_normal_bayes.hpp:159
DATAACCESSOR::PPattern PPattern
one data sample as exposed by DATAACCESSOR (a raw pointer to its feature vector)
Definition classifier_normal_bayes.hpp:121
std::unique_ptr< DIMTYPE[]> _index
subset position -> raw feature index, filled by train() (first _subfeatures entries valid)
Definition classifier_normal_bayes.hpp:181
std::shared_ptr< SUBSET > const PSubset
shared pointer to the (fixed, non-reassignable) feature subset being trained/classified on
Definition classifier_normal_bayes.hpp:120
void disable_prelearn_mode()
reverts to the default: learn a new model in every train() call
Definition classifier_normal_bayes.hpp:131
bool _prelearn_mode
true = model learned once via enable_prelearn_mode(), narrow()ed per train(); false (default) = relea...
Definition classifier_normal_bayes.hpp:180
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Matrix representation and operations, allows operation in selected subspace.
Definition indexed_matrix.hpp:102
Implements normal (gaussian) model.
Definition model_normal.hpp:112
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.
#define M_PI
value of pi; POSIX <cmath> extension, provided here as a fallback since strict C++17 does not guarant...
Definition classifier_normal_bayes.hpp:55
FST4 specific error passing class.
Matrix representation and operations, allows operation in selected subspace.
Implements normal (gaussian) 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
@ f1
classification, two-class data: F1 of the positive class, which is Metric_Spec::f1_positive (class in...
Definition wrapper_metrics.hpp:63