Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
branch_and_bound_predictor_averaging.hpp
Go to the documentation of this file.
1#ifndef FSTSEARCHBRANCHANDBOUNDPREDICTORAVERAGING_H
2#define FSTSEARCHBRANCHANDBOUNDPREDICTORAVERAGING_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 "error.hpp"
52#include "global.hpp"
54
55/*============== Template parameter type naming conventions ==============
56--------- Numeric types: -------------------------------------------------
57DATATYPE - data sample values - usually real numbers (but may be integers
58 in text processing etc.)
59REALTYPE - must be real numbers - for representing intermediate results of
60 calculations like mean, covariance etc.
61IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
62 extent depends on numbers of samples in data
63DIMTYPE - index values for enumeration of features (dimensions), or classes (not
64 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
65 but be aware of expressions like _classes*_features*_features !
66 in linearized representations of feature matrices for all classes
67BINTYPE - feature selection marker type - represents ca. <10 different feature
68 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
69RETURNTYPE - criterion value: real value, but may be extended in future to support
70 multiple values
71--------- Class types: ---------------------------------------------------
72SUBSET - class of class type Subset
73CLASSIFIER - class implementing interface defined in abstract class Classifier
74EVALUATOR - class implementing interface defined in abstract class Sequential_Step
75DISTANCE - class implementing interface defined in abstract class Distance
76DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
77INTERVALCONTAINER - class of class type Data_Intervaller
78CONTAINER - STL container of class type Data_Interval
79========================================================================== */
80
81namespace FST {
82
93template<class RETURNTYPE, typename DIMTYPE>
94class Branch_And_Bound_Predictor_Averaging : public Branch_And_Bound_Predictor<RETURNTYPE,DIMTYPE> {
95public:
96 Branch_And_Bound_Predictor_Averaging(): _gamma(1.0), _delta(1) {notify("Branch_And_Bound_Predictor_Averaging constructor.");}
97 virtual ~Branch_And_Bound_Predictor_Averaging() {notify("Branch_And_Bound_Predictor_Averaging destructor.");}
98
99 virtual void initialize(const DIMTYPE features) override;
100 virtual bool learn(const DIMTYPE feature, const RETURNTYPE value) override;
101 virtual bool predict(const DIMTYPE feature, RETURNTYPE &value) const override;
102 unsigned long get_count(const DIMTYPE feature) const {assert(feature<_count.size()); return _count[feature];}
103
104 void set_gamma(const RETURNTYPE gamma=1.0) {assert(gamma>=0.0); _gamma=gamma;}
105 void set_delta(const unsigned int delta=1) {assert(delta>=1); _delta=delta;}
106
107 virtual std::ostream& print(std::ostream& os) const override;
108protected:
109 std::vector<RETURNTYPE> _value;
110 std::vector<unsigned long> _count;
111
112 RETURNTYPE _gamma;
113 unsigned int _delta;
114};
115
116template<class RETURNTYPE, typename DIMTYPE>
118{
119 assert(features>=1);
120 _value.clear(); _value.resize(features);
121 _count.clear(); _count.resize(features);
122 for(DIMTYPE i=0;i<features;i++) {_value[i]=0.0; _count[i]=0;}
123}
124
125template<class RETURNTYPE, typename DIMTYPE>
126bool Branch_And_Bound_Predictor_Averaging<RETURNTYPE,DIMTYPE>::learn(const DIMTYPE feature, const RETURNTYPE value)
127{
128 assert(value>=0.0);
129 assert(feature<_value.size());
130 _value[feature]+=value;
131 _count[feature]++;
132 return true;
133}
134
135template<class RETURNTYPE, typename DIMTYPE>
136bool Branch_And_Bound_Predictor_Averaging<RETURNTYPE,DIMTYPE>::predict(const DIMTYPE feature, RETURNTYPE &value) const
137{
138 assert(feature<_value.size());
139 if(_count[feature]>=_delta) {
140 value=_gamma*(_value[feature]/(RETURNTYPE)_count[feature]);
141 return true;
142 }
143 return false;
144}
145
146template<class RETURNTYPE, typename DIMTYPE>
148{
149 os << "Branch_And_Bound_Predictor_Averaging(gamma="<<_gamma<<", delta="<<_delta<<")";
150 RETURNTYPE val;
151 unsigned long cnt=0;
152 for(DIMTYPE i=0;i<_value.size();i++) if(_count[i]>0) {
153 if(predict(i,val)) os << std::endl << "feature "<<i<<": predictor value="<<val<< ", count="<<_count[i];
154 cnt+=_count[i];
155 }
156 if(cnt>0) os << std::endl << "Total count: "<<cnt;
157 return os;
158}
159
160} // namespace
161#endif // FSTSEARCHBRANCHANDBOUNDPREDICTORAVERAGING_H ///:~
Interface for Implementations of Prediction Mechanism in Fast Branch & Bound and Branch & Bound with ...
void set_delta(const unsigned int delta=1)
sets the minimum sample count a feature must reach before predict() returns a value (asserted >=1); d...
Definition branch_and_bound_predictor_averaging.hpp:105
std::vector< RETURNTYPE > _value
sums up 'learned' values
Definition branch_and_bound_predictor_averaging.hpp:109
unsigned int _delta
minimum sample count before predict() returns a value; see set_delta()
Definition branch_and_bound_predictor_averaging.hpp:113
std::vector< unsigned long > _count
records the number of summed up values
Definition branch_and_bound_predictor_averaging.hpp:110
virtual std::ostream & print(std::ostream &os) const override
writes gamma/delta plus, for every feature with at least one learned sample, its current predicted va...
Definition branch_and_bound_predictor_averaging.hpp:147
virtual void initialize(const DIMTYPE features) override
(re)initializes the per-feature sum/count accumulators to zero for features features; call before use...
Definition branch_and_bound_predictor_averaging.hpp:117
void set_gamma(const RETURNTYPE gamma=1.0)
sets the scaling factor applied to the averaged value in predict() (asserted >=0.0); default 1....
Definition branch_and_bound_predictor_averaging.hpp:104
virtual bool learn(const DIMTYPE feature, const RETURNTYPE value) override
adds one observed value (non-negative, asserted) to feature 's running sum and increments its sample ...
Definition branch_and_bound_predictor_averaging.hpp:126
virtual bool predict(const DIMTYPE feature, RETURNTYPE &value) const override
sets value to the gamma-scaled running average learned for feature and returns true,...
Definition branch_and_bound_predictor_averaging.hpp:136
RETURNTYPE _gamma
scaling factor applied to the averaged prediction; see set_gamma()
Definition branch_and_bound_predictor_averaging.hpp:112
unsigned long get_count(const DIMTYPE feature) const
the number of learn() calls recorded so far for feature - a diagnostic accessor, telling how well fou...
Definition branch_and_bound_predictor_averaging.hpp:102
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
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.