Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
data_scaler_white.hpp
Go to the documentation of this file.
1#ifndef FSTDATASCALERWHITE_H
2#define FSTDATASCALERWHITE_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 <iostream>
50#include <cassert>
51#include <sstream>
52#include <cmath>
53#include "error.hpp"
54#include "global.hpp"
55#include "data_scaler.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
107template<typename DATATYPE, typename IDXTYPE>
108class Data_Scaler_white : public Data_Scaler<DATATYPE> {
109public:
110 Data_Scaler_white(const int dims=1): learn_loop(0), count1(0), mean(0.0), count2(0), stddev(0.0), missing_values(false), _missing_val_code(0) {if(dims!=1) throw fst_error("Data_Scaler_white() implemented for 1-dimensional data only."); notify("Data_Scaler_white constructor.");}
111 Data_Scaler_white(const DATATYPE missing_val_code, const int dims): learn_loop(0), count1(0), mean(0.0), count2(0), stddev(0.0), missing_values(true), _missing_val_code(missing_val_code) {if(dims!=1) throw fst_error("Data_Scaler_white() implemented for 1-dimensional data only."); notify("Data_Scaler_white constructor.");}
113 virtual ~Data_Scaler_white() {notify("Data_Scaler_white destructor.");}
114 virtual int learn_loops() const override {return 2;}
115 virtual bool startFirstLoop() override;
116 virtual bool startNextLoop() override;
117 virtual void learn(const DATATYPE& value) override;
118
119 virtual DATATYPE scale(const DATATYPE& value) override;
120 virtual void scale_inplace(DATATYPE& value) override;
121
122 virtual Data_Scaler_white* duplicate() const override {return new Data_Scaler_white(*this);}
123
124 virtual std::ostream& print(std::ostream& os) const override {os << "Data_Scaler_white("; if(missing_values) os << "missing_value=" << _missing_val_code; os << ")"; return os;}
125
126protected:
128 IDXTYPE count1;
129 DATATYPE mean;
130 IDXTYPE count2;
131 DATATYPE stddev;
132
133 // missing value substitution support
135 const DATATYPE _missing_val_code;
136};
137
138template<typename DATATYPE, typename IDXTYPE>
140{
141 learn_loop=1; count1=0; mean=0.0;
142 return true;
143}
144
145template<typename DATATYPE, typename IDXTYPE>
147{
148 if(learn_loop==1) {
149 if(count1>0) mean/=count1; else mean=0.0;
150 learn_loop=2; count2=0; stddev=0.0; return true;
151 }
152 else if(learn_loop==2) {
153 // population variance: the sum of squared deviations is divided by the number of values,
154 // not by that number minus one
155 if(count2>0) stddev=sqrt(stddev/count2); else stddev=0.0; return false;
156 }
157 else {
158 std::ostringstream sos; sos << "Data_Scaler_white::startNextLoop() wrong previous loop. (count1=" << count1 << ", count2=" << count2 << ")";
159 throw fst_error(sos.str());
160 }
161}
162
163template<typename DATATYPE, typename IDXTYPE>
165{
166 if(learn_loop==1) {
167 if(!missing_values || value!=_missing_val_code) {
168 ++count1;
169 mean+=value;
170 }
171 } else if(learn_loop==2) {
172 if(!missing_values || value!=_missing_val_code) {
173 ++count2;
174 stddev+=(value-mean)*(value-mean);
175 }
176 } else throw fst_error("Data_Scaler_white::learn() wrong loop.");
177}
178
179template<typename DATATYPE, typename IDXTYPE>
180DATATYPE Data_Scaler_white<DATATYPE,IDXTYPE>::scale(const DATATYPE& value)
181{
182 assert(learn_loop==2 && count1==count2);
183 if(missing_values && value==_missing_val_code) return 0; // substitute the mean, which in whitened coordinates is 0
184 if(stddev>0.0) return (value-mean)/stddev; else return 0;
185}
186
187template<typename DATATYPE, typename IDXTYPE>
189{
190 assert(learn_loop==2 && count1==count2);
191 if(missing_values && value==_missing_val_code) {value=0; return;} // substitute the mean, which in whitened coordinates is 0
192 if(stddev>0.0) value=(value-mean)/stddev; else value=0;
193}
194
195} // namespace
196#endif // FSTDATASCALERWHITE_H ///:~
Implements data whitening (as result, each feature mean value is 0 with normalized stddev).
Definition data_scaler_white.hpp:108
IDXTYPE count2
number of non-missing values fed to learn() during pass 2 (variance accumulation)
Definition data_scaler_white.hpp:130
Data_Scaler_white(const Data_Scaler_white &ds)
copies configuration and any already-learned statistics
Definition data_scaler_white.hpp:112
const DATATYPE _missing_val_code
the numerical code that marks a missing value (only meaningful when missing_values is true)
Definition data_scaler_white.hpp:135
virtual DATATYPE scale(const DATATYPE &value) override
returns (value-mean)/stddev; 0 for a missing value (which is the mean in whitened coordinates) and 0 ...
Definition data_scaler_white.hpp:180
virtual Data_Scaler_white * duplicate() const override
Definition data_scaler_white.hpp:122
bool missing_values
whether values equal to _missing_val_code are substituted (scaled to 0) rather than learned/scaled no...
Definition data_scaler_white.hpp:134
int learn_loop
which of the two learn() passes is active: 1 = accumulating the mean, 2 = accumulating the variance a...
Definition data_scaler_white.hpp:127
Data_Scaler_white(const int dims=1)
no missing-value substitution; dims must be 1
Definition data_scaler_white.hpp:110
IDXTYPE count1
number of non-missing values fed to learn() during pass 1 (mean accumulation)
Definition data_scaler_white.hpp:128
Data_Scaler_white(const DATATYPE missing_val_code, const int dims)
values equal to missing_val_code are substituted (scaled value 0) rather than learned/scaled normally...
Definition data_scaler_white.hpp:111
DATATYPE mean
running sum during pass 1, finalized to the per-feature mean by startNextLoop()
Definition data_scaler_white.hpp:129
virtual void learn(const DATATYPE &value) override
accumulates value into the mean (pass 1) or its squared deviation from that mean (pass 2),...
Definition data_scaler_white.hpp:164
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the scaler and its configuration; base default writes nothi...
Definition data_scaler_white.hpp:124
virtual bool startNextLoop() override
finalizes the mean (after pass 1) or the standard deviation (after pass 2); returns false once both p...
Definition data_scaler_white.hpp:146
virtual bool startFirstLoop() override
resets the mean accumulator and begins pass 1
Definition data_scaler_white.hpp:139
DATATYPE stddev
running sum of squared deviations during pass 2, finalized to the per-feature standard deviation by s...
Definition data_scaler_white.hpp:131
virtual void scale_inplace(DATATYPE &value) override
the in-place form of scale()
Definition data_scaler_white.hpp:188
virtual int learn_loops() const override
two passes: the variance of pass 2 needs the mean from pass 1
Definition data_scaler_white.hpp:114
Data_Scaler(const int dims=1)
Definition data_scaler.hpp:97
FST4 specific error passing class.
Definition error.hpp:61
Defines interface for data scaling implementations (for use in data accessors).
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.