[mlpack-svn] r15054 - mlpack/trunk/src/mlpack/methods/pca
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri May 10 16:11:20 EDT 2013
Author: rcurtin
Date: 2013-05-10 16:11:20 -0400 (Fri, 10 May 2013)
New Revision: 15054
Modified:
mlpack/trunk/src/mlpack/methods/pca/pca.cpp
mlpack/trunk/src/mlpack/methods/pca/pca.hpp
mlpack/trunk/src/mlpack/methods/pca/pca_main.cpp
Log:
Minor formatting fixes and better documentation.
Modified: mlpack/trunk/src/mlpack/methods/pca/pca.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca.cpp 2013-05-10 20:04:48 UTC (rev 15053)
+++ mlpack/trunk/src/mlpack/methods/pca/pca.cpp 2013-05-10 20:11:20 UTC (rev 15054)
@@ -30,16 +30,16 @@
arma::vec& eigVal,
arma::mat& coeffs) const
{
- //Original transpose op goes here.
+ // Original transpose op goes here.
arma::mat covMat = ccov(data);
- //Centering is built into ccov
- if (scaleData)
+ // Centering is built into ccov().
+ if (scaleData)
{
covMat = covMat / (arma::ones<arma::colvec>(covMat.n_rows))
* stddev(covMat, 0, 0);
}
-
+
arma::eig_sym(eigVal, coeffs, covMat);
int nEigVal = eigVal.n_elem;
@@ -89,9 +89,5 @@
data.shed_rows(newDimension, data.n_rows - 1);
}
-PCA::~PCA()
-{
-}
-
}; // namespace mlpack
}; // namespace pca
Modified: mlpack/trunk/src/mlpack/methods/pca/pca.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca.hpp 2013-05-10 20:04:48 UTC (rev 15053)
+++ mlpack/trunk/src/mlpack/methods/pca/pca.hpp 2013-05-10 20:11:20 UTC (rev 15054)
@@ -13,49 +13,58 @@
namespace mlpack {
namespace pca {
+/**
+ * This class implements principal components analysis (PCA). This is a common,
+ * widely-used technique that is often used for either dimensionality reduction
+ * or transforming data into a better basis. Further information on PCA can be
+ * found in almost any statistics or machine learning textbook, and all over the
+ * internet.
+ */
class PCA
{
public:
+ /**
+ * Create the PCA object, specifying if the data should be scaled in each
+ * dimension by standard deviation when PCA is performed.
+ *
+ * @param scaleData Whether or not to scale the data.
+ */
PCA(const bool scaleData = false);
/**
* Apply Principal Component Analysis to the provided data set.
*
- * @param data - Data matrix
- * @param transformedData - Data with PCA applied
- * @param eigVal - contains eigen values in a column vector
- * @param coeff - PCA Loadings/Coeffs/EigenVectors
+ * @param data Data matrix.
+ * @param transformedData Matrix to put results of PCA into.
+ * @param eigval Vector to put eigenvalues into.
+ * @param eigvec Matrix to put eigenvectors (loadings) into.
*/
- void Apply(const arma::mat& data, arma::mat& transformedData, arma::vec&
- eigVal, arma::mat& coeff) const;
+ void Apply(const arma::mat& data,
+ arma::mat& transformedData,
+ arma::vec& eigval,
+ arma::mat& eigvec) const;
/**
* Apply Principal Component Analysis to the provided data set.
*
- * @param data - Data matrix
- * @param transformedData - Data with PCA applied
- * @param eigVal - contains eigen values in a column vector
+ * @param data Data matrix.
+ * @param transformedData Matrix to store results of PCA in.
+ * @param eigval Vector to put eigenvalues into.
*/
- void Apply(const arma::mat& data, arma::mat& transformedData,
+ void Apply(const arma::mat& data,
+ arma::mat& transformedData,
arma::vec& eigVal) const;
/**
- * Apply Dimensionality Reduction using Principal Component Analysis
- * to the provided data set.
+ * Use PCA for dimensionality reduction on the given dataset. This will save
+ * the newDimension largest principal components of the data and remove the
+ * rest.
*
- * @param data - M x N Data matrix
- * @param newDimension - matrix consisting of N column vectors,
- * where each vector is the projection of the corresponding data vector
- * from data matrix onto the basis vectors contained in the columns of
- * coeff/eigen vector matrix with only newDimension number of columns chosen.
+ * @param data Data matrix.
+ * @param newDimension New dimension of the data.
*/
void Apply(arma::mat& data, const size_t newDimension) const;
- /**
- * Delete PCA object
- */
- ~PCA();
-
//! Get whether or not this PCA object will scale (by standard deviation) the
//! data when PCA is performed.
bool ScaleData() const { return scaleData; }
@@ -64,7 +73,10 @@
bool& ScaleData() { return scaleData; }
private:
+ //! Whether or not the data will be scaled by standard deviation when PCA is
+ //! performed.
bool scaleData;
+
}; // class PCA
}; // namespace pca
Modified: mlpack/trunk/src/mlpack/methods/pca/pca_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca_main.cpp 2013-05-10 20:04:48 UTC (rev 15053)
+++ mlpack/trunk/src/mlpack/methods/pca/pca_main.cpp 2013-05-10 20:11:20 UTC (rev 15054)
@@ -39,7 +39,7 @@
data::Load(inputFile.c_str(), dataset);
// Find out what dimension we want.
- size_t newDimension = dataset.n_rows; // No reduction, by default.
+ const size_t newDimension = dataset.n_rows; // No reduction, by default.
if (CLI::GetParam<int>("new_dimensionality") != 0)
{
// Validate the parameter.
More information about the mlpack-svn
mailing list