min_max_scaler.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_DATA_SCALE_HPP
13 #define MLPACK_CORE_DATA_SCALE_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace data {
19 
49 {
50  public:
57  MinMaxScaler(const double min = 0, const double max = 1)
58  {
59  scaleMin = min;
60  scaleMax = max;
61  // Ensure scaleMin is smaller than scaleMax.
62  if (scaleMin > scaleMax)
63  {
64  throw std::runtime_error("Range is not appropriate");
65  }
66  }
67 
73  template<typename MatType>
74  void Fit(const MatType& input)
75  {
76  itemMin = arma::min(input, 1);
77  itemMax = arma::max(input, 1);
78  scale = itemMax - itemMin;
79  // Handle zeros in scale vector.
80  scale.for_each([](arma::vec::elem_type& val) { val =
81  (val == 0) ? 1 : val; });
82  scale = (scaleMax - scaleMin) / scale;
83  scalerowmin.copy_size(itemMin);
84  scalerowmin.fill(scaleMin);
85  scalerowmin = scalerowmin - itemMin % scale;
86  }
87 
94  template<typename MatType>
95  void Transform(const MatType& input, MatType& output)
96  {
97  if (scalerowmin.is_empty() || scale.is_empty())
98  {
99  throw std::runtime_error("Call Fit() before Transform(), please"
100  " refer to the documentation.");
101  }
102  output.copy_size(input);
103  output = (input.each_col() % scale).each_col() + scalerowmin;
104  }
105 
112  template<typename MatType>
113  void InverseTransform(const MatType& input, MatType& output)
114  {
115  output.copy_size(input);
116  output = (input.each_col() - scalerowmin).each_col() / scale;
117  }
118 
120  const arma::vec& ItemMin() const { return itemMin; }
122  const arma::vec& ItemMax() const { return itemMax; }
124  const arma::vec& Scale() const { return scale; }
126  double ScaleMax() const { return scaleMax; }
128  double ScaleMin() const { return scaleMin; }
129 
130  template<typename Archive>
131  void serialize(Archive& ar, const uint32_t /* version */)
132  {
133  ar(CEREAL_NVP(itemMin));
134  ar(CEREAL_NVP(itemMax));
135  ar(CEREAL_NVP(scale));
136  ar(CEREAL_NVP(scaleMin));
137  ar(CEREAL_NVP(scaleMax));
138  ar(CEREAL_NVP(scalerowmin));
139  }
140 
141  private:
142  // Vector which holds minimum of each feature.
143  arma::vec itemMin;
144  // Vector which holds maximum of each feature.
145  arma::vec itemMax;
146  // Scale vector which is used to scale up each feature.
147  arma::vec scale;
148  // Lower value for range.
149  double scaleMin;
150  // Upper value for range.
151  double scaleMax;
152  // Column vector of scalemin
153  arma::vec scalerowmin;
154 }; // class MinMaxScaler
155 
156 } // namespace data
157 } // namespace mlpack
158 
159 #endif
MinMaxScaler(const double min=0, const double max=1)
Default constructor.
Linear algebra utility functions, generally performed on matrices or vectors.
double ScaleMin() const
Get the lower range parameter.
The core includes that mlpack expects; standard C++ includes and Armadillo.
void InverseTransform(const MatType &input, MatType &output)
Function to retrieve original dataset.
const arma::vec & Scale() const
Get the Scale row vector.
void Fit(const MatType &input)
Function to fit features, to find out the min max and scale.
double ScaleMax() const
Get the upper range parameter.
const arma::vec & ItemMax() const
Get the Max row vector.
const arma::vec & ItemMin() const
Get the Min row vector.
void serialize(Archive &ar, const uint32_t)
void Transform(const MatType &input, MatType &output)
Function to scale features.
A simple MinMax Scaler class.