12 #ifndef MLPACK_CORE_DATA_SCALE_HPP 13 #define MLPACK_CORE_DATA_SCALE_HPP 62 if (scaleMin > scaleMax)
64 throw std::runtime_error(
"Range is not appropriate");
73 template<
typename MatType>
74 void Fit(
const MatType& input)
76 itemMin = arma::min(input, 1);
77 itemMax = arma::max(input, 1);
78 scale = itemMax - itemMin;
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;
94 template<
typename MatType>
95 void Transform(
const MatType& input, MatType& output)
97 if (scalerowmin.is_empty() || scale.is_empty())
99 throw std::runtime_error(
"Call Fit() before Transform(), please" 100 " refer to the documentation.");
102 output.copy_size(input);
103 output = (input.each_col() % scale).each_col() + scalerowmin;
112 template<
typename MatType>
115 output.copy_size(input);
116 output = (input.each_col() - scalerowmin).each_col() / scale;
120 const arma::vec&
ItemMin()
const {
return itemMin; }
122 const arma::vec&
ItemMax()
const {
return itemMax; }
124 const arma::vec&
Scale()
const {
return scale; }
130 template<
typename Archive>
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));
153 arma::vec scalerowmin;
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.