Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
criterion_wrapper_bias_estimate.hpp
Go to the documentation of this file.
1#ifndef FSTCRITERIONWRAPPERBIASESTIMATE_H
2#define FSTCRITERIONWRAPPERBIASESTIMATE_H
3 /*
27=========================================================================
28Copyright and license:
29 * FST4 software (with the exception of any bundled or externally linked
30 third-party components) is Copyright (C) 2000-present Institute of
31 Information Theory and Automation of the Czech Academy of Sciences (UTIA).
32 * FST4 is free software: you can redistribute it and/or modify it under
33 the terms of the GNU Affero General Public License as published by the
34 Free Software Foundation, version 3. The full text of the license is in
35 the file LICENSE in the distribution root and at
36 https://www.gnu.org/licenses/agpl-3.0.html.
37 * FST4 is distributed in the hope that it will be useful, but WITHOUT ANY
38 WARRANTY; without even the implied warranty of MERCHANTABILITY or
39 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
40 License for more details.
41 * Closed-source use without the AGPL's copyleft obligations requires a
42 commercial license from UTIA; in case of interest write to
43 utia@utia.cas.cz. LICENSING.md in the distribution root explains both
44 options, the contributor license agreement that every contribution
45 requires, and the contacts (technical questions and pull requests:
46 somol@utia.cas.cz, with "FST" in the subject line).
47========================================================================== */
48
49#include <memory>
50#include <cassert>
51#include <iostream>
52#include <cmath>
53#include "error.hpp"
54#include "global.hpp"
55#include "criterion_wrapper.hpp"
56
57/*============== Template parameter type naming conventions ==============
58--------- Numeric types: -------------------------------------------------
59DATATYPE - data sample values - usually real numbers (but may be integers
60 in text processing etc.)
61REALTYPE - must be real numbers - for representing intermediate results of
62 calculations like mean, covariance etc.
63IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
64 extent depends on numbers of samples in data
65DIMTYPE - index values for enumeration of features (dimensions), or classes (not
66 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
67 but be aware of expressions like _classes*_features*_features !
68 in linearized representations of feature matrices for all classes
69BINTYPE - feature selection marker type - represents ca. <10 different feature
70 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
71RETURNTYPE - criterion value: real value, but may be extended in future to support
72 multiple values
73--------- Class types: ---------------------------------------------------
74SUBSET - class of class type Subset
75CLASSIFIER - class implementing interface defined in abstract class Classifier
76EVALUATOR - class implementing interface defined in abstract class Sequential_Step
77DISTANCE - class implementing interface defined in abstract class Distance
78DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
79INTERVALCONTAINER - class of class type Data_Intervaller
80CONTAINER - STL container of class type Data_Interval
81========================================================================== */
82
83namespace FST {
84
95template<class RETURNTYPE, class SUBSET, class CLASSIFIER, class DATAACCESSOR>
96class Criterion_Wrapper_Bias_Estimate : public Clonable_Impl<Criterion_Wrapper_Bias_Estimate<RETURNTYPE,SUBSET,CLASSIFIER,DATAACCESSOR>,Criterion_Wrapper<RETURNTYPE,SUBSET,CLASSIFIER,DATAACCESSOR> > { // adapter class
97public:
100 typedef std::shared_ptr<CLASSIFIER> PClassifier;
101 typedef std::shared_ptr<DATAACCESSOR> PDataAccessor;
102 typedef std::shared_ptr<SUBSET> PSubset;
103 Criterion_Wrapper_Bias_Estimate():_collected_bias_estimate(0.0),_collected_bias_count(0.0) {notify("Criterion_Wrapper_Bias_Estimate constructor.");}
104 virtual ~Criterion_Wrapper_Bias_Estimate() {notify("Criterion_Wrapper_Bias_Estimate destructor.");}
105
109
117 virtual bool evaluate(RETURNTYPE &result, const PSubset sub) override;
118
119 RETURNTYPE getBiasEstimate() const {if(_collected_bias_count>0.0) return _collected_bias_estimate/_collected_bias_count; else return 0.0;}
121
122
123
124 virtual std::ostream& print(std::ostream& os) const override {os << "Criterion_Wrapper_Bias_Estimate()"; if(parent::_classifier) os << " with " << *parent::_classifier; return os;}
125protected:
126 friend parent_impl;
129
132};
133
134
135
136template<class RETURNTYPE, class SUBSET, class CLASSIFIER, class DATAACCESSOR>
138{
139 assert(parent::_da);
140 assert(parent::_classifier);
141 RETURNTYPE val1, val2, bias=0.0;
142 result=0.0;
143 RETURNTYPE cnt=0.0;
144 // bias estimation
145 parent::_da->resubstitute();
146 for(bool b=parent::_da->getFirstSplit();b==true;b=parent::_da->getNextSplit()) {
147 // train on TRAIN data part
148 if(!parent::_classifier->train(parent::_da,sub)) return false;
149 // test on TRAIN (dependent) data part
150 parent::_da->substitute(TEST,TRAIN);
151 if(!parent::_classifier->test(val2,parent::_da)) return false;
152 // test on TEST (independent) data part
153 parent::_da->substitute(TEST,TEST);
154 if(!parent::_classifier->test(val1,parent::_da)) return false;
155
156 bias+=val2-val1;
157 cnt+=1.0;
158 }
159 if(cnt==0.0) return false; // no split produced (an unprepared or exhausted splitter)
161 _collected_bias_count+=(RETURNTYPE)cnt;
162 result=bias/(RETURNTYPE)cnt;
163 return true;
164}
165
166} // namespace
167#endif // FSTCRITERIONWRAPPERBIASESTIMATE_H ///:~
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Wrapper Bias Evaluator - returns bias estimate (difference of classifier accuracy on training and val...
Definition criterion_wrapper_bias_estimate.hpp:96
Criterion_Wrapper< RETURNTYPE, SUBSET, CLASSIFIER, DATAACCESSOR > parent
base class: plain accuracy wrapper criterion
Definition criterion_wrapper_bias_estimate.hpp:98
Criterion_Wrapper_Bias_Estimate(const Criterion_Wrapper_Bias_Estimate &cwbe)
Copy-constructor; delegates to parent_impl (clones _classifier, sharing-clones _da) and copies the co...
Definition criterion_wrapper_bias_estimate.hpp:128
RETURNTYPE _collected_bias_estimate
running sum of per-split bias (train-part minus test-part accuracy) since the last resetBiasEstimate(...
Definition criterion_wrapper_bias_estimate.hpp:130
RETURNTYPE getBiasEstimate() const
returns the mean per-split bias over all splits evaluated since the last reset (each evaluate() call ...
Definition criterion_wrapper_bias_estimate.hpp:119
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition criterion_wrapper_bias_estimate.hpp:124
std::shared_ptr< SUBSET > PSubset
shared pointer to the feature subset being evaluated
Definition criterion_wrapper_bias_estimate.hpp:102
Clonable_Impl< Criterion_Wrapper_Bias_Estimate, parent > parent_impl
implements clone(); sharing/stateless_clone throw in Criterion
Definition criterion_wrapper_bias_estimate.hpp:99
virtual bool evaluate(RETURNTYPE &result, const PSubset sub) override
Trains on the TRAIN part of each split, tests on both parts, and returns the mean per-split bias: acc...
Definition criterion_wrapper_bias_estimate.hpp:137
std::shared_ptr< DATAACCESSOR > PDataAccessor
shared pointer to the data accessor supplying training/test splits
Definition criterion_wrapper_bias_estimate.hpp:101
RETURNTYPE _collected_bias_count
number of SPLITS accumulated into _collected_bias_estimate since the last reset (one evaluate() call ...
Definition criterion_wrapper_bias_estimate.hpp:131
std::shared_ptr< CLASSIFIER > PClassifier
shared pointer to the wrapped classifier
Definition criterion_wrapper_bias_estimate.hpp:100
void resetBiasEstimate()
clears the accumulated bias estimate and the split count
Definition criterion_wrapper_bias_estimate.hpp:120
Wraps Classifier objects to serve as feature selection criterion.
Definition criterion_wrapper.hpp:95
std::shared_ptr< CLASSIFIER > _classifier
the wrapped classifier trained then tested by evaluate()
Definition criterion_wrapper.hpp:128
std::shared_ptr< DATAACCESSOR > _da
data accessor supplying the train/test splits
Definition criterion_wrapper.hpp:129
Criterion_Wrapper adapts Classifier objects to serve as feature selection criterion.
FST4 specific error passing class.
Global definitions.
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
@ TRAIN
the split's training part
Definition data_accessor.hpp:85
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.