Skip to content
Permalink
Browse files

change of api, introduced PARAM_MODEL_IN() and OUT(), introduce .fit,…

… change in documentation
  • Loading branch information
jeffin143 committed Jun 8, 2019
1 parent 3e338a7 commit 5fd076328a1ffc0186b1736ae461e3e34c9fbf8a
@@ -4,7 +4,7 @@
# Project related configuration options
#---------------------------------------------------------------------------
PROJECT_NAME = mlpack
PROJECT_NUMBER = 3.1.0
PROJECT_NUMBER = 3.1.1
OUTPUT_DIRECTORY = ./doc
CREATE_SUBDIRS = NO
OUTPUT_LANGUAGE = English
@@ -1,5 +1,7 @@
### mlpack 3.1.2
###### ????-??-??
* Add Multiple Pole Balancing Environment (#1901).

* Added functionality for scaling of data (#1876).

### mlpack 3.1.1
@@ -7,6 +7,8 @@ set(SOURCES
mean_normalization.hpp
pcawhitening.hpp
zcawhitening.hpp
scaling_model.hpp
scaling_model_impl.hpp
)

# Add directory name to sources.
@@ -32,8 +32,11 @@ namespace data {
* Load("train.csv", input);
* arma::mat output;
*
* // Scale the features.
* // Fit the features.
* MaxAbsScaler scale;
* scale.Fit(input)
*
* // Scale the features.
* scale.Transform(input, output);
*
* // Retransform the input.
@@ -44,21 +47,31 @@ class MaxAbsScaler
{
public:
/**
* Function to scale features.
* Function to fit features, to find out the min max and scale.
*
* @param input Dataset to scale features.
* @param output Output matrix with scaled features.
* @param input Dataset to fit.
*/
template<typename MatType>
void Transform(const MatType& input, MatType& output)
void Fit(const MatType& input)
{
output.copy_size(input);
itemMin = arma::min(input, 1);
itemMax = arma::max(input, 1);
scale = arma::max(arma::abs(itemMin), arma::abs(itemMax));
// Handling zeros in scale vector.
scale.for_each([](arma::vec::elem_type& val) { val =
(val == 0) ? 1 : val; });
}

/**
* Function to scale features.
*
* @param input Dataset to scale features.
* @param output Output matrix with scaled features.
*/
template<typename MatType>
void Transform(const MatType& input, MatType& output)
{
output.copy_size(input);
output = input.each_col() / scale;
}

@@ -82,6 +95,13 @@ class MaxAbsScaler
//! Get the Scale row vector.
const arma::vec& Scale() const { return scale; }

template<typename Archive>
void serialize(Archive& ar, const unsigned int /* version */)
{
ar & BOOST_SERIALIZATION_NVP(itemMin);
ar & BOOST_SERIALIZATION_NVP(itemMax);
ar & BOOST_SERIALIZATION_NVP(scale);
}
private:
// Vector which holds minimum of each feature.
arma::vec itemMin;
@@ -32,8 +32,11 @@ namespace data {
* Load("train.csv", input);
* arma::mat output;
*
* // Scale the features.
* // Fit the features.
* MeanNormalization scale;
* scale.Fit(input)
*
* // Scale the features.
* scale.Transform(input, output);
*
* // Retransform the input.
@@ -44,22 +47,32 @@ class MeanNormalization
{
public:
/**
* Function to scale features.
* Function to fit features, to find out the min max and scale.
*
* @param input Dataset to scale features.
* @param output Output matrix with scaled features.
* @param input Dataset to fit.
*/
template<typename MatType>
void Transform(const MatType& input, MatType& output)
void Fit(const MatType& input)
{
output.copy_size(input);
itemMean = arma::mean(input, 1);
itemMin = arma::min(input, 1);
itemMax = arma::max(input, 1);
scale = itemMax - itemMin;
// Handling zeros in scale vector.
scale.for_each([](arma::vec::elem_type& val) { val =
(val == 0) ? 1 : val; });
}

/**
* Function to scale features.
*
* @param input Dataset to scale features.
* @param output Output matrix with scaled features.
*/
template<typename MatType>
void Transform(const MatType& input, MatType& output)
{
output.copy_size(input);
output = (input.each_col() - itemMean).each_col() / scale;
}

@@ -85,6 +98,15 @@ class MeanNormalization
//! Get the Scale row vector.
const arma::vec& Scale() const { return scale; }

template<typename Archive>
void serialize(Archive& ar, const unsigned int /* version */)
{
ar & BOOST_SERIALIZATION_NVP(itemMin);
ar & BOOST_SERIALIZATION_NVP(itemMax);
ar & BOOST_SERIALIZATION_NVP(scale);
ar & BOOST_SERIALIZATION_NVP(itemMean);
}

private:
// Vector which holds mean of each feature.
arma::vec itemMean;
@@ -34,8 +34,11 @@ namespace data {
* Load("train.csv", input);
* arma::mat output;
*
* // Scale the features.
* // Fit the features.
* MinMaxScaler scale;
* scale.Fit(input)
*
* // Scale the features.
* scale.Transform(input, output);
*
* // Retransform the input.
@@ -58,15 +61,13 @@ class MinMaxScaler
}

/**
* Function to scale features.
* Function to fit features, to find out the min max and scale.
*
* @param input Dataset to scale features.
* @param output Output matrix with scaled features.
* @param input Dataset to fit.
*/
template<typename MatType>
void Transform(const MatType& input, MatType& output)
void Fit(const MatType& input)
{
output.copy_size(input);
itemMin = arma::min(input, 1);
itemMax = arma::max(input, 1);
scale = itemMax - itemMin;
@@ -77,6 +78,18 @@ class MinMaxScaler
scalerowmin.copy_size(itemMin);
scalerowmin.fill(scaleMin);
scalerowmin = scalerowmin - itemMin % scale;
}

/**
* Function to scale features.
*
* @param input Dataset to scale features.
* @param output Output matrix with scaled features.
*/
template<typename MatType>
void Transform(const MatType& input, MatType& output)
{
output.copy_size(input);
output = (input.each_col() % scale).each_col() + scalerowmin;
}

@@ -104,6 +117,17 @@ class MinMaxScaler
//! Get the lower range parameter.
const double ScaleMin() const { return scaleMin; }

template<typename Archive>
void serialize(Archive& ar, const unsigned int /* version */)
{
ar & BOOST_SERIALIZATION_NVP(itemMin);
ar & BOOST_SERIALIZATION_NVP(itemMax);
ar & BOOST_SERIALIZATION_NVP(scale);
ar & BOOST_SERIALIZATION_NVP(scaleMin);
ar & BOOST_SERIALIZATION_NVP(scaleMax);
ar & BOOST_SERIALIZATION_NVP(scalerowmin);
}

private:
// Vector which holds minimum of each feature.
arma::vec itemMin;
@@ -32,8 +32,11 @@ namespace data {
* Load("train.csv", input);
* arma::mat output;
*
* // Scale the features.
* // Fit the features.
* PcaWhitening scale;
* scale.Fit(input)
*
* // Scale the features.
* scale.Transform(input, output);
*
* // Retransform the input.
@@ -53,6 +56,22 @@ class PcaWhitening
epsilon = eps;
}

/**
* Function to fit features, to find out the min max and scale.
*
* @param input Dataset to fit.
*/
template<typename MatType>
void Fit(const MatType& input)
{
itemMean = arma::mean(input, 1);
// Get eigenvectors and eigenvalues of covariance of input matrix.
eig_sym(eigenValues, eigenVectors, mlpack::math::ColumnCovariance(
input.each_col() - itemMean));
for (size_t i = 0; i < eigenValues.n_elem; i++)
eigenValues(i) = eigenValues(i) + epsilon;
}

/**
* Function for PCA whitening.
*
@@ -63,12 +82,7 @@ class PcaWhitening
void Transform(const MatType& input, MatType& output)
{
output.copy_size(input);
itemMean = arma::mean(input, 1);
output = (input.each_col() - itemMean);
// Get eigenvectors and eigenvalues of covariance of input matrix.
eig_sym(eigenValues, eigenVectors, mlpack::math::ColumnCovariance(output));
for (size_t i = 0; i < eigenValues.n_elem; i++)
eigenValues(i) = eigenValues(i) + epsilon;
output = arma::diagmat(1.0 / (arma::sqrt(eigenValues))) * eigenVectors.t()
* output;
}
@@ -96,6 +110,15 @@ class PcaWhitening
//! Get the Regularisation Parameter.
const double& Epsilon() const { return epsilon; }

template<typename Archive>
void serialize(Archive& ar, const unsigned int /* version */)
{
ar & BOOST_SERIALIZATION_NVP(eigenValues);
ar & BOOST_SERIALIZATION_NVP(eigenVectors);
ar & BOOST_SERIALIZATION_NVP(itemMean);
ar & BOOST_SERIALIZATION_NVP(epsilon);
}

private:
// Vector which holds mean of each feature.
arma::vec itemMean;

0 comments on commit 5fd0763

Please sign in to comment.
You can’t perform that action at this time.