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:
42  PCA(const bool scaleData = false,
43  const DecompositionPolicy& decomposition = DecompositionPolicy());
44 
54  void Apply(const arma::mat& data,
55  arma::mat& transformedData,
56  arma::vec& eigVal,
57  arma::mat& eigvec);
58 
67  void Apply(const arma::mat& data,
68  arma::mat& transformedData,
69  arma::vec& eigVal);
70 
82  double Apply(arma::mat& data, const size_t newDimension);
83 
85  inline double Apply(arma::mat& data, const int newDimension)
86  {
87  return Apply(data, size_t(newDimension));
88  }
89 
105  double Apply(arma::mat& data, const double varRetained);
106 
109  bool ScaleData() const { return scaleData; }
112  bool& ScaleData() { return scaleData; }
113 
114  private:
116  void ScaleData(arma::mat& centeredData)
117  {
118  if (scaleData)
119  {
120  // Scaling the data is when we reduce the variance of each dimension
121  // to 1. We do this by dividing each dimension by its standard
122  // deviation.
123  arma::vec stdDev = arma::stddev(
124  centeredData, 0, 1 /* for each dimension */);
125 
126  // If there are any zeroes, make them very small.
127  for (size_t i = 0; i < stdDev.n_elem; ++i)
128  if (stdDev[i] == 0)
129  stdDev[i] = 1e-50;
130 
131  centeredData /= arma::repmat(stdDev, 1, centeredData.n_cols);
132  }
133  }
134 
137  bool scaleData;
138 
140  DecompositionPolicy decomposition;
141 }; // class PCA
142 
143 } // namespace pca
144 } // namespace mlpack
145 
146 // Include implementation.
147 #include "pca_impl.hpp"
148 
149 #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.
.hpp
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:112
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:109
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:85