Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
FST::Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR > Class Template Reference

Implements Bayes classifier based on normal (gaussian) model. More...

#include <classifier_normal_bayes.hpp>

Inheritance diagram for FST::Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >:
FST::Clonable_Impl< Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >, Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > > FST::Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > FST::Clonable

Public Types

typedef Clonable_Impl< Classifier_Normal_Bayes, Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > > parent_impl
 implements clone(); sharing/stateless_clone throw in Classifier
typedef std::shared_ptr< DATAACCESSOR > PDataAccessor
 shared pointer to the data accessor supplying train/test blocks
typedef std::shared_ptr< SUBSET > const PSubset
 shared pointer to the (fixed, non-reassignable) feature subset being trained/classified on
typedef DATAACCESSOR::PPattern PPattern
 one data sample as exposed by DATAACCESSOR (a raw pointer to its feature vector)
Public Types inherited from FST::Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR >
typedef std::shared_ptr< DATAACCESSOR > PDataAccessor
 shared pointer to the data accessor supplying train/test blocks
typedef std::shared_ptr< SUBSET > const PSubset
 shared pointer to the (fixed, non-reassignable) feature subset being evaluated
typedef DATAACCESSOR::PPattern PPattern
 one data sample as exposed by DATAACCESSOR (a raw pointer to its feature vector)

Public Member Functions

void enable_prelearn_mode (const PDataAccessor da)
void disable_prelearn_mode ()
 reverts to the default: learn a new model in every train() call
bool get_prelearn_mode () const
 true if pre-learn mode is active (see enable_prelearn_mode())
void initialize (const PDataAccessor da)
 (Re)allocates the per-class inverse-covariance, determinant, constant, posterior and log-domain-fallback buffers, sized to da's class/feature counts.
virtual bool classify (DIMTYPE &cls, const PPattern &pattern) override
virtual bool classify_scores (std::vector< RETURNTYPE > &scores, const PPattern &pattern) override
virtual bool train (const PDataAccessor da, const PSubset sub) override
 learns from designated training part of data
virtual bool test (RETURNTYPE &result, const PDataAccessor da) override
DIMTYPE get_n () const
 "full feature set" dimensionality the model was learned at (see Model_Normal::get_n())
DIMTYPE get_d () const
 dimensionality currently exposed by the model (see Model_Normal::get_d())
virtual std::ostream & print (std::ostream &os) const override
 writes a human-readable identification of the object; base default writes nothing
Public Member Functions inherited from FST::Clonable_Impl< Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >, Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > >
Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > * clone () const override
 dispatches to the DEEP_KIND mechanism (default CLONE_DEEP: Derived's copy-constructor)
Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > * sharing_clone () const override
 dispatches to the SHARING_KIND mechanism (default CLONE_INHERIT: defers to Base, which throws by family convention)
Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > * stateless_clone () const override
 dispatches to the STATELESS_KIND mechanism (default CLONE_INHERIT: defers to Base, which throws by family convention)
Public Member Functions inherited from FST::Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR >
virtual bool test_confusion (std::vector< RETURNTYPE > &confusion, DIMTYPE &classes, const PDataAccessor da)
virtual bool test_scores (std::vector< std::pair< RETURNTYPE, DIMTYPE > > &scores, const PDataAccessor da)
virtual bool test_scores_matrix (std::vector< RETURNTYPE > &scores, std::vector< DIMTYPE > &labels, DIMTYPE &classes, const PDataAccessor da)
virtual bool test_metric (RETURNTYPE &result, const PDataAccessor da, const Metric_Spec< RETURNTYPE > &spec)
Public Member Functions inherited from FST::Clonable
bool is_clone () const
bool is_sharing () const
 true if this instance is a sharing_clone() result (referencing, not owning, the source's structures)
bool is_sharing_clone () const
 true if this instance is BOTH a clone and sharing – i.e. specifically a sharing_clone() result, not a clone()/stateless_clone() one

Protected Attributes

std::unique_ptr< Model_Normal< DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR > > _model
 the underlying normal (Gaussian) model supplying per-class mean/covariance
DIMTYPE _classes
 number of classes; sizes the per-class arrays below
DIMTYPE _features
 raw (full) feature count
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET > > _LUtemp
 shared LU-decomposition scratch matrix, reused across classes in train()
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET >[]> _inverse
 per-class inverse covariance matrix (lazy allocation)
std::unique_ptr< REALTYPE[]> _det
 per-class covariance determinant (lazy allocation)
std::unique_ptr< REALTYPE[]> _constant
 per-class normalizing constant sqrt((2*pi)^d * _det[c]) (lazy allocation)
std::unique_ptr< REALTYPE[]> _pxw
 per-class likelihood p(x|class), cached by classify() (lazy allocation)
std::unique_ptr< REALTYPE[]> _Pwx
 per-class posterior P(class|x), cached by classify()/classify_scores() (lazy allocation)
std::unique_ptr< REALTYPE[]> _log_constant
 ln of _constant, computed without forming _constant itself, for the log-domain fallback in classify() (lazy allocation)
std::unique_ptr< REALTYPE[]> _ecoef
 per-class Mahalanobis terms buffered for the fallback (lazy allocation)

Private Member Functions

 Classifier_Normal_Bayes (const Classifier_Normal_Bayes &cnb)
 Deep-copies the learned model and all per-class buffers (inverse covariances, determinants, constants, posterior/fallback caches) and the feature index.

Private Attributes

friend parent_impl
bool _prelearn_mode
 true = model learned once via enable_prelearn_mode(), narrow()ed per train(); false (default) = relearned in every train()
std::unique_ptr< DIMTYPE[]> _index
 subset position -> raw feature index, filled by train() (first _subfeatures entries valid)
DIMTYPE _subfeatures
 current subset size (== sub->get_d_raw() at the last train() call)

Additional Inherited Members

Protected Member Functions inherited from FST::Clonable
void set_cloned (const bool cloned=true)
 marks/unmarks this instance as a clone; call from clone()/stateless_clone() overrides (Clonable_Impl does this automatically)
void set_sharing (const bool sharing=true)
 marks/unmarks this instance as sharing (see is_sharing())
void set_sharing_cloned (const bool cloned=true, const bool sharing=true)
 sets both flags at once; call from sharing_clone() overrides (Clonable_Impl does this automatically)

Detailed Description

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
class FST::Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >

Implements Bayes classifier based on normal (gaussian) model.

Each class is modelled by its own mean vector and its own covariance matrix (the model is Model_Normal), so the decision boundaries are quadratic – this is the QDA-style classifier; Classifier_LDA is the pooled-covariance, linear-boundary sibling. A pattern is assigned to the class of maximum posterior P(class|x), computed from the Gaussian densities and the class priors of the model.

Note
Classifier_Normal_Bayes should be able to handle _model learning in two ways: either [default] to learn() new subspace model in each train() - usable with FS involving multiple DataAccessor training splits, or to pre-learn() full space model and subsequently only narrow() the model in each train() - much faster, but usable only with non-changing set of training patterns, i.e., for one split data access only
Warning
Pre-learn mode must stay DISABLED (the default) when this classifier is driven by Criterion_Wrapper: the wrapper changes the training split between train() calls, and a pre-learned model would keep describing the data of the split it was learned on, silently returning optimistic and meaningless accuracies.
Note
Every class needs enough patterns for a non-singular covariance matrix of the evaluated subset (more patterns than features, and no exactly dependent features). A singular covariance makes the LU decomposition throw, which the criterion reports as a failed evaluation of that subset.

Member Function Documentation

◆ classify()

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
bool FST::Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >::classify ( DIMTYPE & cls,
const PPattern & pattern )
overridevirtual

Classifies one pattern; on success cls receives the predicted class index (0-based, in [0,classes)). Returns false when no decision can be made at all - the model has not been trained, or the implementation hit an unrecoverable condition; false is not a "reject" option and aborts the enclosing test loop.

Implements FST::Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR >.

◆ classify_scores()

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
virtual bool FST::Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >::classify_scores ( std::vector< RETURNTYPE > & scores,
const PPattern & pattern )
inlineoverridevirtual

per-class real scores for one pattern (any monotone transform of the class posteriors); enables ranking-based metrics (AUC). Default: not supported.

Reimplemented from FST::Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR >.

◆ enable_prelearn_mode()

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
void FST::Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >::enable_prelearn_mode ( const PDataAccessor da)

pre-learning mode: if true, calls once _model->learn() for full set size and later in train() uses only narrow()ing to access submatrixes (makes sense only as long as training data do not change, i.e., within one split) if false (default), learns new model in each train() call

◆ initialize()

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
void FST::Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >::initialize ( const PDataAccessor da)

(Re)allocates the per-class inverse-covariance, determinant, constant, posterior and log-domain-fallback buffers, sized to da's class/feature counts.

Note
initialize() does NOT itself learn mean[]/cov[] – that happens in enable_prelearn_mode() (once) or in every train() call (default, non-prelearn mode). Must be called once whenever the training set's class/feature shape changes, before train()/test().

◆ print()

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
virtual std::ostream & FST::Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >::print ( std::ostream & os) const
inlineoverridevirtual

writes a human-readable identification of the object; base default writes nothing

Reimplemented from FST::Clonable.

◆ test()

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
bool FST::Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >::test ( RETURNTYPE & result,
const PDataAccessor da )
overridevirtual

Estimates accuracy (fraction of correctly classified test patterns) using the designated test data. The default implementation is the canonical accuracy loop over the virtual classify(); classifiers needing extra preconditions or a batched evaluation override it (Classifier_Normal_Bayes, Classifier_Multinomial_NaiveBayes, Classifier_SVM, Classifier_Extern). Returns false if the test part is empty or classify() failed.

Reimplemented from FST::Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR >.

◆ train()

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
bool FST::Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >::train ( const PDataAccessor da,
const PSubset sub )
overridevirtual

learns from designated training part of data

Implements FST::Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR >.

Member Data Documentation

◆ parent_impl

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
friend FST::Classifier_Normal_Bayes< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >::parent_impl
private

grants Clonable_Impl access to the copy-constructor


The documentation for this class was generated from the following file:
Copyright © 2000–present ÚTIA, Institute of Information Theory and Automation of the Czech Academy of Sciences · Contacts · This site sets no cookies and uses no analytics.