1#ifndef FSTCLASSIFIERLDA_H
2#define FSTCLASSIFIERLDA_H
110template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
111class Classifier_LDA :
public Clonable_Impl<Classifier_LDA<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>,Classifier<RETURNTYPE,DIMTYPE,SUBSET,DATAACCESSOR> > {
120 notify(
"Classifier_LDA constructor.");
143 virtual std::ostream&
print(std::ostream& os)
const override {os <<
"Classifier_LDA()";
return os;}
157 notify(
"Classifier_LDA copy-constructor.");
164 std::unique_ptr<Model_Normal<DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR> >
_model;
165 std::unique_ptr<Indexed_Matrix<REALTYPE,DIMTYPE,SUBSET> >
_LUtemp;
166 std::unique_ptr<Indexed_Matrix<REALTYPE,DIMTYPE,SUBSET> >
_pooled;
167 std::unique_ptr<Indexed_Matrix<REALTYPE,DIMTYPE,SUBSET> >
_pooledinv;
174 std::vector<REALTYPE>
_a;
175 std::vector<REALTYPE>
_b;
176 std::vector<REALTYPE>
_m;
179template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
182 notify(
"Classifier_LDA::initialize().");
184 assert(da->getNoOfFeatures()>0);
185 if(da->getNoOfClasses()<2)
throw fst_error(
"Classifier_LDA requires at least 2 classes");
187 if(da->getNoOfFeatures()>
_LUtemp->get_n_max())
_LUtemp->reset(da->getNoOfFeatures(),
true);
189 if(da->getNoOfFeatures()>
_pooled->get_n_max())
_pooled->reset(da->getNoOfFeatures(),
false);
191 if(da->getNoOfFeatures()>
_pooledinv->get_n_max())
_pooledinv->reset(da->getNoOfFeatures(),
false);
198template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
201 notify(
"Classifier_LDA::train().");
205 assert(
_classes==da->getNoOfClasses());
206 assert(
_features==da->getNoOfFeatures());
207 assert(sub->get_n_raw()==da->getNoOfFeatures());
222 for(DIMTYPE
f1=0;
f1<d;
f1++)
223 for(DIMTYPE f2=0;f2<d;f2++)
247 const auto &mu=
_model->get_mean()[c];
248 const REALTYPE pc=
_model->get_Pc(c);
255 REALTYPE *ac=&
_a[(std::size_t)c*d];
256 auto &mu=
_model->get_mean()[c];
257 for(DIMTYPE
f1=0;
f1<d;
f1++)
260 for(DIMTYPE f2=0;f2<d;f2++) v+=
_pooledinv->at_raw(
f1,f2)*(mu.at_raw(f2)-
_m[f2]);
265 _b[c]=-0.5*dot+std::log(
_model->get_Pc(c));
268 for(std::size_t j=0;j<
_a.size();j++)
if(!std::isfinite(
_a[j]))
return false;
269 for(DIMTYPE c=0;c<
_classes;c++)
if(!std::isfinite(
_b[c]))
return false;
273template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
281 const REALTYPE *ac=&
_a[(std::size_t)c*d];
283 for(DIMTYPE f=0;f<d;f++) v+=ac[f]*((REALTYPE)pattern[
_index[f]]-
_m[f]);
284 scores[c]=(RETURNTYPE)v;
289template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
Linear (Fisher/Gaussian shared-covariance) discriminant classifier.
Definition classifier_lda.hpp:111
virtual bool train(const PDataAccessor da, const PSubset sub) override
Learns per-class Gaussian parameters on the designated training part, pools the per-class covariances...
Definition classifier_lda.hpp:199
std::unique_ptr< Model_Normal< DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR > > _model
per-class means/covariances (learned by train(), see Model_Normal)
Definition classifier_lda.hpp:164
DIMTYPE _subfeatures
d = current subset size
Definition classifier_lda.hpp:171
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET > > _pooled
pooled within-class covariance
Definition classifier_lda.hpp:166
virtual bool classify(DIMTYPE &cls, const PPattern &pattern) override
Definition classifier_lda.hpp:290
DIMTYPE _features
raw (full) feature count, as recorded by initialize()
Definition classifier_lda.hpp:170
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition classifier_lda.hpp:143
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET > > _pooledinv
its inverse
Definition classifier_lda.hpp:167
std::shared_ptr< DATAACCESSOR > PDataAccessor
shared pointer to the data accessor supplying train/test blocks
Definition classifier_lda.hpp:115
std::vector< DIMTYPE > _index
subset position -> raw feature index (first d entries valid)
Definition classifier_lda.hpp:172
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET > > _LUtemp
LU scratch (lazy allocation).
Definition classifier_lda.hpp:165
std::vector< RETURNTYPE > _cs_sc
classify() score scratch (member to avoid per-pattern allocation)
Definition classifier_lda.hpp:173
Classifier_LDA(const Classifier_LDA &cld)
Deep-copies the learned model and the pooled-covariance matrices (LU scratch, pooled covariance,...
Definition classifier_lda.hpp:148
DATAACCESSOR::PPattern PPattern
one data sample as exposed by DATAACCESSOR (a raw pointer to its feature vector)
Definition classifier_lda.hpp:117
std::vector< REALTYPE > _a
classes x d discriminant directions, row-major
Definition classifier_lda.hpp:174
virtual bool classify_scores(std::vector< RETURNTYPE > &scores, const PPattern &pattern) override
Definition classifier_lda.hpp:274
DIMTYPE _classes
number of classes, as recorded by initialize()
Definition classifier_lda.hpp:169
std::vector< REALTYPE > _b
classes discriminant offsets
Definition classifier_lda.hpp:175
Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > parent
the abstract base type
Definition classifier_lda.hpp:113
void initialize(const PDataAccessor da)
Pre-allocates model and matrix buffers and records the class/feature counts of da....
Definition classifier_lda.hpp:180
Clonable_Impl< Classifier_LDA, parent > parent_impl
implements clone() via the copy-constructor below
Definition classifier_lda.hpp:114
std::vector< REALTYPE > _m
d-entry centering point of the discriminants: the prior-weighted mean of the class means (see train()...
Definition classifier_lda.hpp:176
std::shared_ptr< SUBSET > PSubset
shared pointer to the feature subset being trained/classified on
Definition classifier_lda.hpp:116
abstract class, defines interface for classifier implementations (mainly to be used in wrappers)
Definition classifier.hpp:97
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
FST4 specific error passing class.
Definition error.hpp:61
Defines interface for classifier implementations.
Defines interface for classes that may need to get cloned.
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
@ f1
classification, two-class data: F1 of the positive class, which is Metric_Spec::f1_positive (class in...
Definition wrapper_metrics.hpp:63