1#ifndef FSTMODELNORMAL_H
2#define FSTMODELNORMAL_H
111template<
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
112class Model_Normal :
public Model<SUBSET,DATAACCESSOR> {
141 template<
class,
class,
class,
class,
class,
class,
class>
friend class Classifier_LDA;
142 template<
class,
class,
class,
class,
class,
class,
class>
friend class Criterion_Normal;
146 const std::unique_ptr<Indexed_Vector<REALTYPE,DIMTYPE,SUBSET>[]>&
get_mean()
const {
return _mean;}
147 const std::unique_ptr<Indexed_Matrix<REALTYPE,DIMTYPE,SUBSET>[]>&
get_cov()
const {
return _cov;}
159 std::unique_ptr<REALTYPE[]>
_Pc;
160 std::unique_ptr<Indexed_Vector<REALTYPE,DIMTYPE,SUBSET>[]>
_mean;
161 std::unique_ptr<Indexed_Matrix<REALTYPE,DIMTYPE,SUBSET>[]>
_cov;
176template<
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
177Model_Normal<DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>::Model_Normal(
const Model_Normal& mn) :
178 Model<SUBSET,DATAACCESSOR>(mn),
184 notify(
"Model_Normal copy-constructor.");
200template<
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
204 assert(da->getNoOfClasses()>0);
205 assert(da->getNoOfFeatures()>0);
206 PSubset fullset(
new SUBSET(da->getNoOfFeatures()));
207 fullset->select_all();
209 {std::ostringstream sos; sos <<
"fullset = "<< *fullset << std::endl;
syncout::print(std::cout,sos);}
214template<
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
217 notify(
"Model_Normal::learn():");
218 assert(da->getNoOfClasses()>0);
219 assert(da->getNoOfFeatures()>0);
221 assert(sub->get_n_raw()==da->getNoOfFeatures());
222 assert(sub->get_d_raw()>0);
241 IDXTYPE _allpatterns=da->getClassSizeSum();
242 assert(_allpatterns>0);
243 for(DIMTYPE c=0;c<
_classes;c++)
_Pc[c]=(REALTYPE)da->getClassSize(c)/(REALTYPE)_allpatterns;
245 if(sub->get_n_raw()>
_n_max)
258 _cov[c].redim(
_n);
_cov[c].set_all_raw_to(0.0);
262 for(DIMTYPE f : sub->selected_features()) {
_learn_index[fi++]=f;}
265 typename DATAACCESSOR::PPattern p;
271 const DIMTYPE _stride=sub->get_n_raw();
272 assert(da->getSplitIndex()>0);
276 assert(da->getNoOfPatterns(
TRAIN)>0);
278 {std::ostringstream sos; sos <<
"da->getNoOfPatterns(TRAIN) [class="<<c<<
"] = "<<da->getNoOfPatterns(
TRAIN)<<std::endl;
syncout::print(std::cout,sos);}
280 for(
const auto &blk : da->blocks(
TRAIN)) { p=blk.data; cnt=blk.size;
281 for(IDXTYPE i=0;i<cnt;i++)
284 for(DIMTYPE f=0;f<
_n;f++) (
_mean[c])[f]/=da->getNoOfPatterns(
TRAIN);
289 std::ostringstream sos; sos <<
"_Pc:"<<std::endl;
290 for(DIMTYPE c=0;c<
_classes;c++) sos <<
_Pc[c] <<
" ";
292 sos <<
"_mean:"<<std::endl;
293 for(DIMTYPE c=0;c<
_classes;c++) {
for(DIMTYPE f=0;f<
_n;f++) sos << (
_mean[c])[f] <<
" "; sos << std::endl;}
300 for(
const auto &blk : da->blocks(
TRAIN)) { p=blk.data; cnt=blk.size;
301 for(IDXTYPE i=0;i<cnt;i++) {
303 for(DIMTYPE f2=
f1;f2<
_n;f2++){
319 REALTYPE pats=da->getNoOfPatterns(
TRAIN)-1;
323 for(DIMTYPE f2=0;f2<
f1;f2++) {
327 for(DIMTYPE f2=0;f2<
_n;f2++) {
328 _cov[c].at_raw(
f1,f2)/=pats;
335 std::ostringstream sos; sos <<
"_cov:"<<std::endl;
336 for(DIMTYPE c=0;c<
_classes;c++) {
for(DIMTYPE
f1=0;
f1<
_n;
f1++) {
for(DIMTYPE f2=0;f2<
_n;f2++) sos <<
_cov[c].at_raw(
f1,f2) <<
" "; sos << std::endl;} sos << std::endl;}
343template<
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
348 assert(sub->get_n_raw()==
_n);
349 assert(sub->get_d_raw()>0);
351 _mean[c].narrow_to(sub);
352 _cov[c].narrow_to(sub);
357template<
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
Linear (Fisher/Gaussian shared-covariance) discriminant classifier.
Definition classifier_lda.hpp:111
Implements Bayes classifier based on normal (gaussian) model.
Definition classifier_normal_bayes.hpp:116
Implements Bhattacharyya distance based on normal (gaussian) model to serve as feature selection crit...
Definition criterion_normal_bhattacharyya.hpp:105
Implements Divergence (distance) based on normal (gaussian) model to serve as feature selection crite...
Definition criterion_normal_divergence.hpp:110
Implements Generalized Mahalanobis distance based on normal (gaussian) model to serve as feature sele...
Definition criterion_normal_gmahalanobis.hpp:107
Abstract class, defines interface for feature selection criterion implementations based on normal mod...
Definition criterion_normal.hpp:100
Matrix representation and operations, allows operation in selected subspace.
Definition indexed_matrix.hpp:102
Vector representation and operations, allows operation in selected subspace. Also serves as the helpe...
Definition indexed_vector.hpp:100
Implements normal (gaussian) model.
Definition model_normal.hpp:112
DIMTYPE get_d() const
the current virtual (narrow_to()'d) dimensionality, get_d()<=get_n()
Definition model_normal.hpp:133
std::unique_ptr< Indexed_Vector< REALTYPE, DIMTYPE, SUBSET >[]> _mean
per-class learned mean vectors, get_classes() entries; see get_mean()
Definition model_normal.hpp:160
DIMTYPE _d
current virtual (narrow_to()'d) dimensionality, _d<=_n; see get_d()
Definition model_normal.hpp:165
const std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET >[]> & get_cov() const
per-class learned covariance matrices (array of get_classes() Indexed_Matrix, each get_n()/get_d() si...
Definition model_normal.hpp:147
REALTYPE get_Pc(const DIMTYPE c) const
Definition model_normal.hpp:138
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET >[]> _cov
per-class learned covariance matrices, get_classes() entries; see get_cov()
Definition model_normal.hpp:161
std::unique_ptr< REALTYPE[]> _Pc
per-class prior probabilities P(c), get_classes() entries; see get_Pc()
Definition model_normal.hpp:159
DIMTYPE _n_max
allocated capacity: the largest sub->get_n_raw() (the subset's full allocation size,...
Definition model_normal.hpp:157
void narrow_to(const PSubset sub) override
Definition model_normal.hpp:344
DIMTYPE get_n_max() const
the allocated capacity (max n learn() can grow to without reallocating)
Definition model_normal.hpp:131
DIMTYPE get_classes() const
number of classes the model was learned over (learn()'s da->getNoOfClasses())
Definition model_normal.hpp:134
const std::unique_ptr< Indexed_Vector< REALTYPE, DIMTYPE, SUBSET >[]> & get_mean() const
per-class learned mean vectors (array of get_classes() Indexed_Vector, each of dimensionality get_n()...
Definition model_normal.hpp:146
virtual void learn(PDataAccessor da) override
Fits the model's parameters from da's training data, over all its features. Concrete models may addit...
Definition model_normal.hpp:201
DIMTYPE _classes
number of classes the model is learned over; see get_classes() – NOTE: expressions like _classes*_n*_...
Definition model_normal.hpp:149
void denarrow() override
Undoes narrow_to(): model access reverts to the full dimensionality it was learned at.
Definition model_normal.hpp:358
std::unique_ptr< DIMTYPE[]> _learn_index
Definition model_normal.hpp:173
DIMTYPE _n
full ("raw") dimensionality the model is currently learned at, _n<=_n_max; see get_n() – NOTE: expres...
Definition model_normal.hpp:158
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset, as consumed by learn(da,sub)/narrow_to()
Definition model_normal.hpp:115
std::shared_ptr< DATAACCESSOR > PDataAccessor
shared pointer to the data accessor supplying training data
Definition model_normal.hpp:114
DIMTYPE get_n() const
the full ("raw") dimensionality the model is currently learned at, get_n()<=get_n_max()
Definition model_normal.hpp:132
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
FST4 specific error passing class.
Matrix representation and operations, allows operation in selected subspace.
Vector representation and operations, allows operation in selected subspace.
Defines interface for data model implementations.
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
@ TRAIN
the split's training part
Definition data_accessor.hpp:85
@ f1
classification, two-class data: F1 of the positive class, which is Metric_Spec::f1_positive (class in...
Definition wrapper_metrics.hpp:63