mlpack
scaling_model_impl.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_DATA_SCALING_MODEL_IMPL_HPP
14 #define MLPACK_CORE_DATA_SCALING_MODEL_IMPL_HPP
15 
16 // In case it hasn't been included yet.
17 #include "scaling_model.hpp"
18 
19 namespace mlpack {
20 namespace data {
21 
22 ScalingModel::ScalingModel(const int minvalue,
23  const int maxvalue,
24  double epsilonvalue) :
25  scalerType(0),
26  minmaxscale(NULL),
27  maxabsscale(NULL),
28  meanscale(NULL),
29  standardscale(NULL),
30  pcascale(NULL),
31  zcascale(NULL),
32  minValue(minvalue),
33  maxValue(maxvalue),
34  epsilon(epsilonvalue)
35 {
36  // Nothing to do.
37 }
38 
41  scalerType(other.scalerType),
42  minmaxscale(other.minmaxscale == NULL ? NULL :
43  new data::MinMaxScaler(*other.minmaxscale)),
44  maxabsscale(other.maxabsscale == NULL ? NULL :
45  new data::MaxAbsScaler(*other.maxabsscale)),
46  meanscale(other.meanscale == NULL ? NULL :
47  new data::MeanNormalization(*other.meanscale)),
48  standardscale(other.standardscale == NULL ? NULL :
49  new data::StandardScaler(*other.standardscale)),
50  pcascale(other.pcascale == NULL ? NULL :
51  new data::PCAWhitening(*other.pcascale)),
52  zcascale(other.zcascale == NULL ? NULL :
53  new data::ZCAWhitening(*other.zcascale)),
54  minValue(other.minValue),
55  maxValue(other.maxValue),
56  epsilon(other.epsilon)
57 {
58  // Nothing to do.
59 }
60 
63  scalerType(other.scalerType),
64  minmaxscale(other.minmaxscale),
65  maxabsscale(other.maxabsscale),
66  meanscale(other.meanscale),
67  standardscale(other.standardscale),
68  pcascale(other.pcascale),
69  zcascale(other.zcascale),
70  minValue(other.minValue),
71  maxValue(other.maxValue),
72  epsilon(other.epsilon)
73 {
74  other.scalerType = 0;
75  other.minmaxscale = NULL;
76  other.maxabsscale = NULL;
77  other.meanscale = NULL;
78  other.standardscale = NULL;
79  other.pcascale = NULL;
80  other.zcascale = NULL;
81  other.minValue = 0;
82  other.maxValue = 1;
83  other.epsilon = 0.00005;
84 }
85 
88 {
89  if (this == &other)
90  {
91  return *this;
92  }
93  scalerType = other.scalerType;
94 
95  delete minmaxscale;
96  minmaxscale = (other.minmaxscale == NULL) ? NULL :
97  new data::MinMaxScaler(*other.minmaxscale);
98 
99  delete maxabsscale;
100  maxabsscale = (other.maxabsscale == NULL) ? NULL :
101  new data::MaxAbsScaler(*other.maxabsscale);
102 
103  delete standardscale;
104  standardscale = (other.standardscale == NULL) ? NULL :
105  new data::StandardScaler(*other.standardscale);
106 
107  delete meanscale;
108  meanscale = (other.meanscale == NULL) ? NULL :
109  new data::MeanNormalization(*other.meanscale);
110 
111  delete pcascale;
112  pcascale = (other.pcascale == NULL) ? NULL :
113  new data::PCAWhitening(*other.pcascale);
114 
115  delete zcascale;
116  zcascale = (other.zcascale == NULL) ? NULL :
117  new data::ZCAWhitening(*other.zcascale);
118 
119  minValue = other.minValue;
120  maxValue = other.maxValue;
121  epsilon = other.epsilon;
122 
123  return *this;
124 }
125 
128 {
129  if (this != &other)
130  {
131  scalerType = other.scalerType;
132  minmaxscale = other.minmaxscale;
133  maxabsscale = other.maxabsscale;
134  meanscale = other.meanscale;
135  standardscale = other.standardscale;
136  pcascale = other.pcascale;
137  zcascale = other.zcascale;
138  minValue = other.minValue;
139  maxValue = other.maxValue;
140  epsilon = other.epsilon;
141 
142  other.scalerType = 0;
143  other.minmaxscale = nullptr;
144  other.maxabsscale = nullptr;
145  other.meanscale = nullptr;
146  other.standardscale = nullptr;
147  other.pcascale = nullptr;
148  other.zcascale = nullptr;
149  other.minValue = 0;
150  other.maxValue = 1;
151  other.epsilon = 0.00005;
152  }
153  return *this;
154 }
155 
157 {
158  delete minmaxscale;
159  delete maxabsscale;
160  delete standardscale;
161  delete meanscale;
162  delete pcascale;
163  delete zcascale;
164 }
165 
166 template<typename MatType>
167 void ScalingModel::Fit(const MatType& input)
168 {
169  if (scalerType == ScalerTypes::STANDARD_SCALER)
170  {
171  delete standardscale;
172  standardscale = new data::StandardScaler();
173  standardscale->Fit(input);
174  }
175  else if (scalerType == ScalerTypes::MIN_MAX_SCALER)
176  {
177  delete minmaxscale;
178  minmaxscale = new data::MinMaxScaler(minValue, maxValue);
179  minmaxscale->Fit(input);
180  }
181  else if (scalerType == ScalerTypes::MEAN_NORMALIZATION)
182  {
183  delete meanscale;
184  meanscale = new data::MeanNormalization();
185  meanscale->Fit(input);
186  }
187  else if (scalerType == ScalerTypes::MAX_ABS_SCALER)
188  {
189  delete maxabsscale;
190  maxabsscale = new data::MaxAbsScaler();
191  maxabsscale->Fit(input);
192  }
193  else if (scalerType == ScalerTypes::PCA_WHITENING)
194  {
195  delete pcascale;
196  pcascale = new data::PCAWhitening(epsilon);
197  pcascale->Fit(input);
198  }
199  else if (scalerType == ScalerTypes::ZCA_WHITENING)
200  {
201  delete zcascale;
202  zcascale = new data::ZCAWhitening(epsilon);
203  zcascale->Fit(input);
204  }
205 }
206 
207 template<typename MatType>
208 void ScalingModel::Transform(const MatType& input, MatType& output)
209 {
210  if (scalerType == ScalerTypes::STANDARD_SCALER)
211  {
212  standardscale->Transform(input, output);
213  }
214  else if (scalerType == ScalerTypes::MIN_MAX_SCALER)
215  {
216  minmaxscale->Transform(input, output);
217  }
218  else if (scalerType == ScalerTypes::MEAN_NORMALIZATION)
219  {
220  meanscale->Transform(input, output);
221  }
222  else if (scalerType == ScalerTypes::MAX_ABS_SCALER)
223  {
224  maxabsscale->Transform(input, output);
225  }
226  else if (scalerType == ScalerTypes::PCA_WHITENING)
227  {
228  pcascale->Transform(input, output);
229  }
230  else if (scalerType == ScalerTypes::ZCA_WHITENING)
231  {
232  zcascale->Transform(input, output);
233  }
234 }
235 
236 template<typename MatType>
237 void ScalingModel::InverseTransform(const MatType& input, MatType& output)
238 {
239  if (scalerType == ScalerTypes::STANDARD_SCALER)
240  {
241  standardscale->InverseTransform(input, output);
242  }
243  else if (scalerType == ScalerTypes::MIN_MAX_SCALER)
244  {
245  minmaxscale->InverseTransform(input, output);
246  }
247  else if (scalerType == ScalerTypes::MEAN_NORMALIZATION)
248  {
249  meanscale->InverseTransform(input, output);
250  }
251  else if (scalerType == ScalerTypes::MAX_ABS_SCALER)
252  {
253  maxabsscale->InverseTransform(input, output);
254  }
255  else if (scalerType == ScalerTypes::PCA_WHITENING)
256  {
257  pcascale->InverseTransform(input, output);
258  }
259  else if (scalerType == ScalerTypes::ZCA_WHITENING)
260  {
261  zcascale->InverseTransform(input, output);
262  }
263 }
264 
265 } // namespace data
266 } // namespace mlpack
267 
268 #endif
void Fit(const MatType &input)
Function to fit features, to find out the min max and scale.
Definition: max_abs_scaler.hpp:55
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void Fit(const MatType &input)
Function to fit features, to find out the min max and scale.
Definition: zca_whitening.hpp:63
void InverseTransform(const MatType &input, MatType &output)
Function to retrieve original dataset.
Definition: pca_whitening.hpp:107
void InverseTransform(const MatType &input, MatType &output)
Function to retrieve original dataset.
Definition: mean_normalization.hpp:91
A simple Mean Normalization class.
Definition: mean_normalization.hpp:46
~ScalingModel()
Clean up memory.
Definition: scaling_model_impl.hpp:156
void Fit(const MatType &input)
Function to fit features, to find out the min max and scale.
Definition: min_max_scaler.hpp:74
ScalingModel(const int minvalue=0, const int maxvalue=1, double epsilonvalue=0.00005)
Create an object.
Definition: scaling_model_impl.hpp:22
ScalingModel & operator=(const ScalingModel &other)
Copy assignment operator.
Definition: scaling_model_impl.hpp:87
void InverseTransform(const MatType &input, MatType &output)
Function to retrieve original dataset.
Definition: standard_scaler.hpp:90
void InverseTransform(const MatType &input, MatType &output)
Function to retrieve original dataset.
Definition: max_abs_scaler.hpp:90
void Fit(const MatType &input)
Function to fit features, to find out the min max and scale.
Definition: standard_scaler.hpp:56
A simple PCAWhitening class.
Definition: pca_whitening.hpp:47
void Transform(const MatType &input, MatType &output)
Transform to scale features.
Definition: scaling_model_impl.hpp:208
void Transform(const MatType &input, MatType &output)
Function for ZCA whitening.
Definition: zca_whitening.hpp:75
void Transform(const MatType &input, MatType &output)
Function to scale features.
Definition: standard_scaler.hpp:72
The model to save to disk.
Definition: scaling_model.hpp:29
A simple MaxAbs Scaler class.
Definition: max_abs_scaler.hpp:46
void Transform(const MatType &input, MatType &output)
Function to scale features.
Definition: mean_normalization.hpp:73
void Fit(const MatType &input)
Function to fit features, to find out the min max and scale.
Definition: pca_whitening.hpp:71
A simple Standard Scaler class.
Definition: standard_scaler.hpp:47
void Transform(const MatType &input, MatType &output)
Function to scale features.
Definition: max_abs_scaler.hpp:72
void Transform(const MatType &input, MatType &output)
Function for PCA whitening.
Definition: pca_whitening.hpp:87
void InverseTransform(const MatType &input, MatType &output)
Function to retrieve original dataset.
Definition: zca_whitening.hpp:88
A simple MinMax Scaler class.
Definition: min_max_scaler.hpp:48
void Transform(const MatType &input, MatType &output)
Function to scale features.
Definition: min_max_scaler.hpp:95
void InverseTransform(const MatType &input, MatType &output)
Function to retrieve original dataset.
Definition: min_max_scaler.hpp:113
A simple ZCAWhitening class.
Definition: zca_whitening.hpp:47
void Fit(const MatType &input)
Function to fit features, to find out the min max and scale.
Definition: mean_normalization.hpp:55