pca.hpp
Go to the documentation of this file.
1 
16 #ifndef MLPACK_METHODS_PCA_PCA_HPP
17 #define MLPACK_METHODS_PCA_PCA_HPP
18 
19 #include <mlpack/prereqs.hpp>
21 
22 namespace mlpack {
23 namespace pca {
24 
32 template<typename DecompositionPolicy = ExactSVDPolicy>
33 class PCA
34 {
35  public:
43  PCA(const bool scaleData = false,
44  const DecompositionPolicy& decomposition = DecompositionPolicy());
45 
55  void Apply(const arma::mat& data,
56  arma::mat& transformedData,
57  arma::vec& eigVal,
58  arma::mat& eigvec);
59 
68  void Apply(const arma::mat& data,
69  arma::mat& transformedData,
70  arma::vec& eigVal);
77  void Apply(const arma::mat& data,
78  arma::mat& transformedData);
79 
91  double Apply(arma::mat& data, const size_t newDimension);
92 
94  inline double Apply(arma::mat& data, const int newDimension)
95  {
96  return Apply(data, size_t(newDimension));
97  }
98 
114  double Apply(arma::mat& data, const double varRetained);
115 
118  bool ScaleData() const { return scaleData; }
121  bool& ScaleData() { return scaleData; }
122 
123  private:
125  void ScaleData(arma::mat& centeredData)
126  {
127  if (scaleData)
128  {
129  // Scaling the data is when we reduce the variance of each dimension
130  // to 1. We do this by dividing each dimension by its standard
131  // deviation.
132  arma::vec stdDev = arma::stddev(
133  centeredData, 0, 1 /* for each dimension */);
134 
135  // If there are any zeroes, make them very small.
136  for (size_t i = 0; i < stdDev.n_elem; ++i)
137  if (stdDev[i] == 0)
138  stdDev[i] = 1e-50;
139 
140  centeredData /= arma::repmat(stdDev, 1, centeredData.n_cols);
141  }
142  }
143 
146  bool scaleData;
147 
149  DecompositionPolicy decomposition;
150 }; // class PCA
151 
152 } // namespace pca
153 } // namespace mlpack
154 
155 // Include implementation.
156 #include "pca_impl.hpp"
157 
158 #endif
void Apply(const arma::mat &data, arma::mat &transformedData, arma::vec &eigVal, arma::mat &eigvec)
Apply Principal Component Analysis to the provided data set.
Linear algebra utility functions, generally performed on matrices or vectors.
bool & ScaleData()
Modify whether or not this PCA object will scale (by standard deviation) the data when PCA is perform...
Definition: pca.hpp:121
The core includes that mlpack expects; standard C++ includes and Armadillo.
This class implements principal components analysis (PCA).
Definition: pca.hpp:33
PCA(const bool scaleData=false, const DecompositionPolicy &decomposition=DecompositionPolicy())
Create the PCA object, specifying if the data should be scaled in each dimension by standard deviatio...
bool ScaleData() const
Get whether or not this PCA object will scale (by standard deviation) the data when PCA is performed...
Definition: pca.hpp:118
double Apply(arma::mat &data, const int newDimension)
This overload is here to make sure int gets casted right to size_t.
Definition: pca.hpp:94