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>
17 
18 namespace mlpack {
19 namespace data {
20 
47 {
48  public:
54  PCAWhitening(double eps = 0.00005)
55  {
56  epsilon = eps;
57  // Ensure scaleMin is smaller than scaleMax.
58  if (epsilon < 0)
59  {
60  throw std::runtime_error("Regularization parameter is not correct");
61  }
62  }
63 
69  template<typename MatType>
70  void Fit(const MatType& input)
71  {
72  itemMean = arma::mean(input, 1);
73  // Get eigenvectors and eigenvalues of covariance of input matrix.
74  eig_sym(eigenValues, eigenVectors, mlpack::math::ColumnCovariance(
75  input.each_col() - itemMean));
76  eigenValues += epsilon;
77  }
78 
85  template<typename MatType>
86  void Transform(const MatType& input, MatType& output)
87  {
88  if (eigenValues.is_empty() || eigenVectors.is_empty())
89  {
90  throw std::runtime_error("Call Fit() before Transform(), please"
91  " refer to the documentation.");
92  }
93  output.copy_size(input);
94  output = (input.each_col() - itemMean);
95  output = arma::diagmat(1.0 / (arma::sqrt(eigenValues))) * eigenVectors.t()
96  * output;
97  }
98 
105  template<typename MatType>
106  void InverseTransform(const MatType& input, MatType& output)
107  {
108  output = arma::diagmat(arma::sqrt(eigenValues)) * inv(eigenVectors.t())
109  * input;
110  output = (output.each_col() + itemMean);
111  }
112 
114  const arma::vec& ItemMean() const { return itemMean; }
116  const arma::vec& EigenValues() const { return eigenValues; }
118  const arma::mat& EigenVectors() const { return eigenVectors; }
120  const double& Epsilon() const { return epsilon; }
121 
122  template<typename Archive>
123  void serialize(Archive& ar, const unsigned int /* version */)
124  {
125  ar & BOOST_SERIALIZATION_NVP(eigenValues);
126  ar & BOOST_SERIALIZATION_NVP(eigenVectors);
127  ar & BOOST_SERIALIZATION_NVP(itemMean);
128  ar & BOOST_SERIALIZATION_NVP(epsilon);
129  }
130 
131  private:
132  // Vector which holds mean of each feature.
133  arma::vec itemMean;
134  // Mat which hold the eigenvectors.
135  arma::mat eigenVectors;
136  // Regularization Paramter.
137  double epsilon;
138  // Vector which hold the eigenvalues.
139  arma::vec eigenValues;
140 }; // class PCAWhitening
141 
142 } // namespace data
143 } // namespace mlpack
144 
145 #endif
.hpp
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.