max_abs_scaler.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_DATA_MAX_ABS_SCALE_HPP
13 #define MLPACK_CORE_DATA_MAX_ABS_SCALE_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace data {
19 
47 {
48  public:
54  template<typename MatType>
55  void Fit(const MatType& input)
56  {
57  itemMin = arma::min(input, 1);
58  itemMax = arma::max(input, 1);
59  scale = arma::max(arma::abs(itemMin), arma::abs(itemMax));
60  // Handling zeros in scale vector.
61  scale.for_each([](arma::vec::elem_type& val) { val =
62  (val == 0) ? 1 : val; });
63  }
64 
71  template<typename MatType>
72  void Transform(const MatType& input, MatType& output)
73  {
74  if (scale.is_empty())
75  {
76  throw std::runtime_error("Call Fit() before Transform(), please"
77  " refer to the documentation.");
78  }
79  output.copy_size(input);
80  output = input.each_col() / scale;
81  }
82 
89  template<typename MatType>
90  void InverseTransform(const MatType& input, MatType& output)
91  {
92  output.copy_size(input);
93  output = input.each_col() % scale;
94  }
95 
97  const arma::vec& ItemMin() const { return itemMin; }
99  const arma::vec& ItemMax() const { return itemMax; }
101  const arma::vec& Scale() const { return scale; }
102 
103  template<typename Archive>
104  void serialize(Archive& ar, const uint32_t /* version */)
105  {
106  ar(CEREAL_NVP(itemMin));
107  ar(CEREAL_NVP(itemMax));
108  ar(CEREAL_NVP(scale));
109  }
110  private:
111  // Vector which holds minimum of each feature.
112  arma::vec itemMin;
113  // Vector which holds maximum of each feature.
114  arma::vec itemMax;
115  // Vector which is used to scale up each feature.
116  arma::vec scale;
117 }; // class MaxAbsScaler
118 
119 } // namespace data
120 } // namespace mlpack
121 
122 #endif
const arma::vec & ItemMax() const
Get the Max row vector.
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Transform(const MatType &input, MatType &output)
Function to scale features.
void serialize(Archive &ar, const uint32_t)
void InverseTransform(const MatType &input, MatType &output)
Function to retrieve original dataset.
const arma::vec & Scale() const
Get the Scale row vector.
A simple MaxAbs Scaler class.
void Fit(const MatType &input)
Function to fit features, to find out the min max and scale.
const arma::vec & ItemMin() const
Get the Min row vector.