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);
71 
83  double Apply(arma::mat& data, const size_t newDimension);
84 
86  inline double Apply(arma::mat& data, const int newDimension)
87  {
88  return Apply(data, size_t(newDimension));
89  }
90 
106  double Apply(arma::mat& data, const double varRetained);
107 
110  bool ScaleData() const { return scaleData; }
113  bool& ScaleData() { return scaleData; }
114 
115  private:
117  void ScaleData(arma::mat& centeredData)
118  {
119  if (scaleData)
120  {
121  // Scaling the data is when we reduce the variance of each dimension
122  // to 1. We do this by dividing each dimension by its standard
123  // deviation.
124  arma::vec stdDev = arma::stddev(
125  centeredData, 0, 1 /* for each dimension */);
126 
127  // If there are any zeroes, make them very small.
128  for (size_t i = 0; i < stdDev.n_elem; ++i)
129  if (stdDev[i] == 0)
130  stdDev[i] = 1e-50;
131 
132  centeredData /= arma::repmat(stdDev, 1, centeredData.n_cols);
133  }
134  }
135 
138  bool scaleData;
139 
141  DecompositionPolicy decomposition;
142 }; // class PCA
143 
144 } // namespace pca
145 } // namespace mlpack
146 
147 // Include implementation.
148 #include "pca_impl.hpp"
149 
150 #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.
Definition: add_to_po.hpp:21
bool & ScaleData()
Modify whether or not this PCA object will scale (by standard deviation) the data when PCA is perform...
Definition: pca.hpp:113
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:110
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:86