pca_whitening.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_DATA_PCA_WHITENING_SCALE_HPP
13 #define MLPACK_CORE_DATA_PCA_WHITENING_SCALE_HPP
14 
15 #include <mlpack/prereqs.hpp>
18 
19 namespace mlpack {
20 namespace data {
21 
48 {
49  public:
55  PCAWhitening(double eps = 0.00005)
56  {
57  epsilon = eps;
58  // Ensure scaleMin is smaller than scaleMax.
59  if (epsilon < 0)
60  {
61  throw std::runtime_error("Regularization parameter is not correct");
62  }
63  }
64 
70  template<typename MatType>
71  void Fit(const MatType& input)
72  {
73  itemMean = arma::mean(input, 1);
74  // Get eigenvectors and eigenvalues of covariance of input matrix.
75  eig_sym(eigenValues, eigenVectors, mlpack::math::ColumnCovariance(
76  input.each_col() - itemMean));
77  eigenValues += epsilon;
78  }
79 
86  template<typename MatType>
87  void Transform(const MatType& input, MatType& output)
88  {
89  if (eigenValues.is_empty() || eigenVectors.is_empty())
90  {
91  throw std::runtime_error("Call Fit() before Transform(), please"
92  " refer to the documentation.");
93  }
94  output.copy_size(input);
95  output = (input.each_col() - itemMean);
96  output = arma::diagmat(1.0 / (arma::sqrt(eigenValues))) * eigenVectors.t()
97  * output;
98  }
99 
106  template<typename MatType>
107  void InverseTransform(const MatType& input, MatType& output)
108  {
109  output = arma::diagmat(arma::sqrt(eigenValues)) * inv(eigenVectors.t())
110  * input;
111  output = (output.each_col() + itemMean);
112  }
113 
115  const arma::vec& ItemMean() const { return itemMean; }
117  const arma::vec& EigenValues() const { return eigenValues; }
119  const arma::mat& EigenVectors() const { return eigenVectors; }
121  const double& Epsilon() const { return epsilon; }
122 
123  template<typename Archive>
124  void serialize(Archive& ar, const unsigned int /* version */)
125  {
126  ar & BOOST_SERIALIZATION_NVP(eigenValues);
127  ar & BOOST_SERIALIZATION_NVP(eigenVectors);
128  ar & BOOST_SERIALIZATION_NVP(itemMean);
129  ar & BOOST_SERIALIZATION_NVP(epsilon);
130  }
131 
132  private:
133  // Vector which holds mean of each feature.
134  arma::vec itemMean;
135  // Mat which hold the eigenvectors.
136  arma::mat eigenVectors;
137  // Regularization Paramter.
138  double epsilon;
139  // Vector which hold the eigenvalues.
140  arma::vec eigenValues;
141 }; // class PCAWhitening
142 
143 } // namespace data
144 } // namespace mlpack
145 
146 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
arma::Mat< eT > ColumnCovariance(const arma::Mat< eT > &A, const size_t norm_type=0)
const arma::vec & ItemMean() const
Get the mean row vector.
void Fit(const MatType &input)
Function to fit features, to find out the min max and scale.
A simple PCAWhitening class.
const arma::vec & EigenValues() const
Get the eigenvalues vector.
void serialize(Archive &ar, const unsigned int)
void InverseTransform(const MatType &input, MatType &output)
Function to retrieve original dataset.
const double & Epsilon() const
Get the regularization parameter.
PCAWhitening(double eps=0.00005)
A constructor to set the regularization parameter.
const arma::mat & EigenVectors() const
Get the eigenvector.
void Transform(const MatType &input, MatType &output)
Function for PCA whitening.