16 #ifndef MLPACK_METHODS_PCA_PCA_IMPL_HPP 17 #define MLPACK_METHODS_PCA_PCA_IMPL_HPP 26 template<
typename DecompositionPolicy>
28 const bool scaleData,
const DecompositionPolicy& decomposition) :
30 decomposition(decomposition)
41 template<
typename DecompositionPolicy>
43 arma::mat& transformedData,
50 arma::mat centeredData;
56 decomposition.Apply(data, centeredData, transformedData, eigVal, eigvec,
69 template<
typename DecompositionPolicy>
71 arma::mat& transformedData,
75 Apply(data, transformedData, eigVal, eigvec);
84 template<
typename DecompositionPolicy>
86 arma::mat& transformedData)
90 Apply(data, transformedData, eigVal, eigvec);
104 template<
typename DecompositionPolicy>
106 const size_t newDimension)
109 if (newDimension == 0)
110 Log::Fatal <<
"PCA::Apply(): newDimension (" << newDimension <<
") cannot " 111 <<
"be zero!" << std::endl;
112 if (newDimension > data.n_rows)
113 Log::Fatal <<
"PCA::Apply(): newDimension (" << newDimension <<
") cannot " 114 <<
"be greater than the existing dimensionality of the data (" 115 << data.n_rows <<
")!" << std::endl;
123 arma::mat centeredData;
129 decomposition.Apply(data, centeredData, data, eigVal, eigvec, newDimension);
131 if (newDimension < eigvec.n_rows)
133 data.shed_rows(newDimension, data.n_rows - 1);
137 double eigDim = std::min(newDimension - 1, (
size_t) eigVal.n_elem - 1);
142 return (sum(eigVal.subvec(0, eigDim)) / sum(eigVal));
155 template<
typename DecompositionPolicy>
157 const double varRetained)
161 Log::Fatal <<
"PCA::Apply(): varRetained (" << varRetained <<
") must be " 162 <<
"greater than or equal to 0." << std::endl;
164 Log::Fatal <<
"PCA::Apply(): varRetained (" << varRetained <<
") should be " 165 <<
"less than or equal to 1." << std::endl;
170 Apply(data, data, eigVal, eigvec);
173 size_t newDimension = 0;
175 eigVal /= arma::sum(eigVal);
176 while ((varSum < varRetained) && (newDimension < eigVal.n_elem))
178 varSum += eigVal[newDimension];
183 if (newDimension < eigVal.n_elem)
184 data.shed_rows(newDimension, data.n_rows - 1);
static void Start(const std::string &name)
Start the given timer.
Definition: timers.cpp:28
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.
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...
Definition: pca_impl.hpp:27
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
void Apply(const arma::mat &data, arma::mat &transformedData, arma::vec &eigVal, arma::mat &eigvec)
Apply Principal Component Analysis to the provided data set.
Definition: pca_impl.hpp:42
static void Stop(const std::string &name)
Stop the given timer.
Definition: timers.cpp:36
void Center(const arma::mat &x, arma::mat &xCentered)
Creates a centered matrix, where centering is done by subtracting the sum over the columns (a column ...
Definition: lin_alg.cpp:43