Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
data_scaler_void.hpp
Go to the documentation of this file.
1#ifndef FSTDATASCALERVOID_H
2#define FSTDATASCALERVOID_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
93template<typename DATATYPE>
94class Data_Scaler_void : public Data_Scaler<DATATYPE> {
95public:
96 Data_Scaler_void(const int dims=1):missing_values(false), _missing_val_code(0), count(0), sum(0), avg(0) {if(dims!=1) throw fst_error("Data_Scaler_void() implemented for 1-dimensional data only."); notify("Data_Scaler_void constructor.");}
97 Data_Scaler_void(const DATATYPE missing_val_code, const int dims):missing_values(true), _missing_val_code(missing_val_code), count(0), sum(0), avg(0) {if(dims!=1) throw fst_error("Data_Scaler_void() implemented for 1-dimensional data only."); notify("Data_Scaler_void constructor.");}
98 Data_Scaler_void(const Data_Scaler_void &ds):missing_values(ds.missing_values), _missing_val_code(ds._missing_val_code), count(ds.count), sum(ds.sum), avg(ds.avg) {notify("Data_Scaler_void copy constructor.");}
99 virtual ~Data_Scaler_void() {notify("Data_Scaler_void destructor.");}
100 virtual int learn_loops() const override {if(missing_values) return 1; else return 0;}
103 virtual bool startFirstLoop() override {sum=0; count=0; return true;}
104 virtual bool startNextLoop() override {return false;}
105 virtual void learn(const DATATYPE& value) override;
106
107 virtual DATATYPE scale(const DATATYPE& value) override;
108 virtual void scale_inplace(DATATYPE& value) override;
109
110 virtual Data_Scaler_void* duplicate() const override {return new Data_Scaler_void(*this);}
111
112 virtual std::ostream& print(std::ostream& os) const override {os << "Data_Scaler_void("; if(missing_values) os << "missing_value=" << _missing_val_code; os << ")"; return os;}
113
114protected:
115 // missing value substitution support
117 const DATATYPE _missing_val_code;
118 long count;
119 DATATYPE sum;
120 DATATYPE avg;
121};
122
123template<typename DATATYPE>
124void Data_Scaler_void<DATATYPE>::learn(const DATATYPE& value)
125{
126 if(missing_values && value!=_missing_val_code) {
127 sum+=value; count++;
128 avg=sum/(DATATYPE)count;
129 }
130}
131
132template<typename DATATYPE>
133DATATYPE Data_Scaler_void<DATATYPE>::scale(const DATATYPE& value)
134{
135 if(missing_values && value==_missing_val_code) {
136 if(count==0) // all values missing
137 return 0;
138 else return avg;
139 } else return value;
140}
141
142template<typename DATATYPE>
144{
145 if(missing_values && value==_missing_val_code) {
146 if(count==0) // all values missing
147 value=0;
148 else value=avg;
149 }
150}
151
152} // namespace
153#endif // FSTDATASCALERVOID_H ///:~
Void data scaler, to bypass data normalization.
Definition data_scaler_void.hpp:94
Data_Scaler_void(const Data_Scaler_void &ds)
copies configuration and any already-learned statistics
Definition data_scaler_void.hpp:98
virtual void scale_inplace(DATATYPE &value) override
the in-place form of scale(); leaves value untouched unless it is the missing-value code
Definition data_scaler_void.hpp:143
DATATYPE sum
running sum of non-missing values, used to derive avg
Definition data_scaler_void.hpp:119
bool missing_values
whether values equal to _missing_val_code are substituted by the per-feature mean; when false,...
Definition data_scaler_void.hpp:116
const DATATYPE _missing_val_code
the numerical code that marks a missing value (only meaningful when missing_values is true)
Definition data_scaler_void.hpp:117
virtual int learn_loops() const override
Definition data_scaler_void.hpp:100
Data_Scaler_void(const int dims=1)
no missing-value substitution (pure bypass); dims must be 1
Definition data_scaler_void.hpp:96
virtual DATATYPE scale(const DATATYPE &value) override
returns value unchanged, or the per-feature mean of the available values if value is the missing-valu...
Definition data_scaler_void.hpp:133
virtual Data_Scaler_void * duplicate() const override
Definition data_scaler_void.hpp:110
Data_Scaler_void(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_void.hpp:97
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_void.hpp:112
virtual bool startNextLoop() override
one pass is always enough
Definition data_scaler_void.hpp:104
long count
number of non-missing values fed to learn() for the current feature
Definition data_scaler_void.hpp:118
DATATYPE avg
running mean of non-missing values (sum/count); substituted for missing entries
Definition data_scaler_void.hpp:120
virtual void learn(const DATATYPE &value) override
accumulates value into the per-feature mean, unless it is the missing-value code or substitution is o...
Definition data_scaler_void.hpp:124
virtual bool startFirstLoop() override
Resets the mean accumulator and always requests one pass, even in the identity case – the pass is the...
Definition data_scaler_void.hpp:103
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.