Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
criterion_wrapper.hpp
Go to the documentation of this file.
1#ifndef FSTCRITERIONWRAPPER_H
2#define FSTCRITERIONWRAPPER_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.hpp"
56#include "wrapper_metrics.hpp"
57
58/*============== Template parameter type naming conventions ==============
59--------- Numeric types: -------------------------------------------------
60DATATYPE - data sample values - usually real numbers (but may be integers
61 in text processing etc.)
62REALTYPE - must be real numbers - for representing intermediate results of
63 calculations like mean, covariance etc.
64IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
65 extent depends on numbers of samples in data
66DIMTYPE - index values for enumeration of features (dimensions), or classes (not
67 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
68 but be aware of expressions like _classes*_features*_features !
69 in linearized representations of feature matrices for all classes
70BINTYPE - feature selection marker type - represents ca. <10 different feature
71 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
72RETURNTYPE - criterion value: real value, but may be extended in future to support
73 multiple values
74--------- Class types: ---------------------------------------------------
75SUBSET - class of class type Subset
76CLASSIFIER - class implementing interface defined in abstract class Classifier
77EVALUATOR - class implementing interface defined in abstract class Sequential_Step
78DISTANCE - class implementing interface defined in abstract class Distance
79DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
80INTERVALCONTAINER - class of class type Data_Intervaller
81CONTAINER - STL container of class type Data_Interval
82========================================================================== */
83
84namespace FST {
85
94template<class RETURNTYPE, class SUBSET, class CLASSIFIER, class DATAACCESSOR>
95class Criterion_Wrapper : public Clonable_Impl<Criterion_Wrapper<RETURNTYPE,SUBSET,CLASSIFIER,DATAACCESSOR>,Criterion<RETURNTYPE,SUBSET> > { // adapter class
96public:
98 typedef std::shared_ptr<CLASSIFIER> PClassifier;
99 typedef std::shared_ptr<DATAACCESSOR> PDataAccessor;
100 typedef std::shared_ptr<SUBSET> PSubset;
101 Criterion_Wrapper() {notify("Criterion_Wrapper constructor.");}
102 virtual ~Criterion_Wrapper() {notify("Criterion_Wrapper destructor.");}
103
109 virtual void initialize(const PClassifier classifier, const PDataAccessor da);
110
114 virtual bool evaluate(RETURNTYPE &result, const PSubset sub) override;
115
120
121
122 virtual std::ostream& print(std::ostream& os) const override {os << "Criterion_Wrapper()"; if(_classifier) os << " with " << *_classifier; return os;}
123protected:
124 friend parent_impl;
126 Criterion_Wrapper(const Criterion_Wrapper& cw);
127
128 std::shared_ptr<CLASSIFIER> _classifier;
129 std::shared_ptr<DATAACCESSOR> _da;
131};
132
133template<class RETURNTYPE, class SUBSET, class CLASSIFIER, class DATAACCESSOR>
134Criterion_Wrapper<RETURNTYPE,SUBSET,CLASSIFIER,DATAACCESSOR>::Criterion_Wrapper(const Criterion_Wrapper& cw)
135{
136 notify("Criterion_Wrapper copy-constructor.");
140}
141
142
143
144template<class RETURNTYPE, class SUBSET, class CLASSIFIER, class DATAACCESSOR>
145void Criterion_Wrapper<RETURNTYPE,SUBSET,CLASSIFIER,DATAACCESSOR>::initialize(const PClassifier classifier, const PDataAccessor da) // passing by std::shared_ptr to enable keeping the reference throughout *this life cycle
146{
147 assert(da);
148 assert(classifier);
149 _da=da;
150 _classifier=classifier;
151}
152
153template<class RETURNTYPE, class SUBSET, class CLASSIFIER, class DATAACCESSOR>
155{
156 assert(_da);
157 assert(_classifier);
158 RETURNTYPE val;
159 result=0.0;
160 RETURNTYPE cnt=0.0;
161 if(!_metric_spec.is_default()) {
162 // non-default metric: per-split metric, then mean over splits (macro over folds,
163 // same shape as the accuracy path below). Splits on which the metric is undefined
164 // (e.g. AUC with a single-class fold) are skipped; all-splits-undefined = failure.
165 for(bool b=_da->getFirstSplit();b==true;b=_da->getNextSplit()) {
166 if(!_classifier->train(_da,sub)) return false;
167 if(_classifier->test_metric(val,_da,_metric_spec)) {
168 result+=val;
169 cnt+=1.0;
170 }
171 }
172 if(cnt==0.0) return false;
173 result/=(RETURNTYPE)cnt;
174 return true;
175 }
176 for(bool b=_da->getFirstSplit();b==true;b=_da->getNextSplit()) {
177 if(!_classifier->train(_da,sub)) return false;
178 if(!_classifier->test(val,_da)) return false;
179 result+=val;
180 cnt+=1.0;
181 }
182 if(cnt==0.0) return false; // no split produced (an unprepared or exhausted splitter)
183 result/=(RETURNTYPE)cnt;
184 return true;
185}
186
187} // namespace
188#endif // FSTCRITERIONWRAPPER_H ///:~
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Wraps Classifier objects to serve as feature selection criterion.
Definition criterion_wrapper.hpp:95
Metric_Spec< RETURNTYPE > _metric_spec
Definition criterion_wrapper.hpp:130
virtual void initialize(const PClassifier classifier, const PDataAccessor da)
Stores the classifier and the data accessor to be used by every later evaluate(). Must be called befo...
Definition criterion_wrapper.hpp:145
const Metric_Spec< RETURNTYPE > & get_metric() const
returns the currently configured wrapper metric (see set_metric())
Definition criterion_wrapper.hpp:119
std::shared_ptr< SUBSET > PSubset
Definition criterion_wrapper.hpp:100
void set_metric(const Metric_Spec< RETURNTYPE > &spec)
Definition criterion_wrapper.hpp:118
Clonable_Impl< Criterion_Wrapper, Criterion< RETURNTYPE, SUBSET > > parent_impl
Definition criterion_wrapper.hpp:97
std::shared_ptr< REGRESSOR > _classifier
Definition criterion_wrapper.hpp:128
virtual bool evaluate(RETURNTYPE &result, const PSubset sub) override
Trains and tests the wrapped classifier on every split, returning the mean of the per-split values of...
Definition criterion_wrapper.hpp:154
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition criterion_wrapper.hpp:122
std::shared_ptr< REGRESSOR > PClassifier
Definition criterion_wrapper.hpp:98
std::shared_ptr< DATAACCESSOR > _da
Definition criterion_wrapper.hpp:129
Criterion_Wrapper(const Criterion_Wrapper &cw)
Copy-constructor; clones _classifier and sharing-clones _da, copies _metric_spec.
Definition criterion_wrapper.hpp:134
std::shared_ptr< DATAACCESSOR > PDataAccessor
Definition criterion_wrapper.hpp:99
Defines interface for feature selection criterion classes.
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
std::unique_ptr< T > clone_unique(const T &src)
Definition clonable.hpp:234
std::unique_ptr< T > sharing_clone_unique(const T &src)
sharing_clone() counterpart of clone_unique() – see its comment above.
Definition clonable.hpp:239
configuration of the wrapper metric; immutable during search, copied by criterion clones (thread-safe...
Definition wrapper_metrics.hpp:73
Wrapper criterion metrics beyond accuracy (balanced accuracy, F1, AUC, class weights,...
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.