mlpack  master
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 
33 template<typename DecompositionPolicy = ExactSVDPolicy>
34 class PCAType
35 {
36  public:
43  PCAType(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 
146 
147 } // namespace pca
148 } // namespace mlpack
149 
150 // Include implementation.
151 #include "pca_impl.hpp"
152 
153 #endif
bool & ScaleData()
Modify whether or not this PCA object will scale (by standard deviation) the data when PCA is perform...
Definition: pca.hpp:113
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
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
This class implements principal components analysis (PCA).
Definition: pca.hpp:34
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Apply(const arma::mat &data, arma::mat &transformedData, arma::vec &eigVal, arma::mat &eigvec)
Apply Principal Component Analysis to the provided data set.
PCAType< ExactSVDPolicy > PCA
3.0.0 TODO: break reverse-compatibility by changing PCAType to PCA.
Definition: pca.hpp:145
DecompositionPolicy decomposition
Decomposition method used to perform principal components analysis.
Definition: pca.hpp:141
void ScaleData(arma::mat &centeredData)
Scaling the data is when we reduce the variance of each dimension to 1.
Definition: pca.hpp:117
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
bool scaleData
Whether or not the data will be scaled by standard deviation when PCA is performed.
Definition: pca.hpp:138
PCAType(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...