Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
data_scaler_to01.hpp
Go to the documentation of this file.
1#ifndef FSTDATASCALERTO01_H
2#define FSTDATASCALERTO01_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 <cmath>
51#include "error.hpp"
52#include "global.hpp"
53#include "data_scaler.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
100template<typename DATATYPE>
101class Data_Scaler_to01 : public Data_Scaler<DATATYPE> {
102public:
103 Data_Scaler_to01(const int dims=1): first_learn(true), missing_values(false), _missing_val_code(0) {if(dims!=1) throw fst_error("Data_Scaler_to01() implemented for 1-dimensional data only."); notify("Data_Scaler_to01 constructor.");}
104 Data_Scaler_to01(const DATATYPE missing_val_code, const int dims): first_learn(true), missing_values(true), _missing_val_code(missing_val_code) {if(dims!=1) throw fst_error("Data_Scaler_to01() implemented for 1-dimensional data only."); notify("Data_Scaler_to01 constructor.");}
105 Data_Scaler_to01(const Data_Scaler_to01 &ds) : first_learn(ds.first_learn), min(ds.min), max(ds.max), missing_values(ds.missing_values), _missing_val_code(ds._missing_val_code), count(ds.count), sum(ds.sum), avg(ds.avg) {notify("Data_Scaler_to01 copy constructor.");}
106 virtual ~Data_Scaler_to01() {notify("Data_Scaler_to01 destructor.");}
107 virtual int learn_loops() const override {return 1;}
108 virtual bool startFirstLoop() override {max=min=0; first_learn=true; sum=0; count=0; return true;}
109 virtual bool startNextLoop() override {return false;}
110 virtual void learn(const DATATYPE& value) override;
111
112 virtual DATATYPE scale(const DATATYPE& value) override;
113 virtual void scale_inplace(DATATYPE& value) override;
114
115 virtual Data_Scaler_to01* duplicate() const override {return new Data_Scaler_to01(*this);}
116
117 virtual std::ostream& print(std::ostream& os) const override {os << "Data_Scaler_to01("; if(missing_values) os << "missing_value=" << _missing_val_code; os << ")"; return os;}
118
119protected:
121 DATATYPE min;
122 DATATYPE max;
123
124 // missing value substitution support
126 const DATATYPE _missing_val_code;
127 long count;
128 DATATYPE sum;
129 DATATYPE avg;
130};
131
132template<typename DATATYPE>
133void Data_Scaler_to01<DATATYPE>::learn(const DATATYPE& value)
134{
135 if(!missing_values || value!=_missing_val_code) {
136 if(first_learn) {min=max=value; first_learn=false;} else {
137 if(value>max) max=value;
138 if(value<min) min=value;
139 }
140 sum+=value; count++;
141 avg=sum/(DATATYPE)count;
142 }
143}
144
145template<typename DATATYPE>
146DATATYPE Data_Scaler_to01<DATATYPE>::scale(const DATATYPE& value)
147{
148 if(count==0) // all values missing
149 return 0;
150 else if(missing_values && value==_missing_val_code)
151 {
152 if(max>min) return (avg-min)/(max-min); else return 0;
153 } else {
154 if(max>min) return (value-min)/(max-min); else return 0;
155 }
156}
157
158template<typename DATATYPE>
160{
161 if(count==0) // all values missing
162 value=0;
163 else if(missing_values && value==_missing_val_code)
164 {
165 if(max>min) value=(avg-min)/(max-min); else value=0;
166 } else {
167 if(max>min) value=(value-min)/(max-min); else value=0;
168 }
169}
170
171} // namespace
172#endif // FSTDATASCALERTO01_H ///:~
Implements data normalization (of all feature values) to interval [0,1].
Definition data_scaler_to01.hpp:101
bool first_learn
true until the first learn() call of the current pass has initialized min/max
Definition data_scaler_to01.hpp:120
virtual bool startNextLoop() override
one pass is always enough
Definition data_scaler_to01.hpp:109
virtual int learn_loops() const override
one pass suffices: min, max and the mean are all accumulated together
Definition data_scaler_to01.hpp:107
Data_Scaler_to01(const DATATYPE missing_val_code, const int dims)
values equal to missing_val_code are substituted by the per-feature mean; dims must be 1
Definition data_scaler_to01.hpp:104
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_to01.hpp:117
virtual void scale_inplace(DATATYPE &value) override
the in-place form of scale()
Definition data_scaler_to01.hpp:159
Data_Scaler_to01(const int dims=1)
no missing-value substitution; dims must be 1
Definition data_scaler_to01.hpp:103
DATATYPE sum
running sum of non-missing values, used to derive avg
Definition data_scaler_to01.hpp:128
long count
number of non-missing values fed to learn() for the current feature
Definition data_scaler_to01.hpp:127
const DATATYPE _missing_val_code
the numerical code that marks a missing value (only meaningful when missing_values is true)
Definition data_scaler_to01.hpp:126
virtual Data_Scaler_to01 * duplicate() const override
Definition data_scaler_to01.hpp:115
DATATYPE min
minimum non-missing value seen for the current feature
Definition data_scaler_to01.hpp:121
virtual DATATYPE scale(const DATATYPE &value) override
returns (value-min)/(max-min); the mean's scaled position for a missing value; 0 if the feature is co...
Definition data_scaler_to01.hpp:146
Data_Scaler_to01(const Data_Scaler_to01 &ds)
copies configuration and any already-learned statistics
Definition data_scaler_to01.hpp:105
virtual void learn(const DATATYPE &value) override
accumulates value into the per-feature min, max and mean, unless it is the missing-value code
Definition data_scaler_to01.hpp:133
DATATYPE max
maximum non-missing value seen for the current feature
Definition data_scaler_to01.hpp:122
bool missing_values
whether values equal to _missing_val_code are substituted rather than learned/scaled normally
Definition data_scaler_to01.hpp:125
DATATYPE avg
running mean of non-missing values (sum/count); substituted, then scaled, for missing entries
Definition data_scaler_to01.hpp:129
virtual bool startFirstLoop() override
resets the per-feature accumulators and requests the single pass
Definition data_scaler_to01.hpp:108
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.