[mlpack-svn] r10798 - mlpack/trunk/src/mlpack/methods/pca
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Dec 14 14:05:19 EST 2011
Author: rcurtin
Date: 2011-12-14 14:05:19 -0500 (Wed, 14 Dec 2011)
New Revision: 10798
Modified:
mlpack/trunk/src/mlpack/methods/pca/pca.cpp
mlpack/trunk/src/mlpack/methods/pca/pca.hpp
Log:
Change variable names to match code specifications and mark a couple functions
const.
Modified: mlpack/trunk/src/mlpack/methods/pca/pca.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca.cpp 2011-12-14 18:17:27 UTC (rev 10797)
+++ mlpack/trunk/src/mlpack/methods/pca/pca.cpp 2011-12-14 19:05:19 UTC (rev 10798)
@@ -14,10 +14,9 @@
namespace pca {
PCA::PCA(const bool centerData, const bool scaleData) :
- centerData_(centerData),
- scaleData_(scaleData)
-{
-}
+ centerData(centerData),
+ scaleData(scaleData)
+{ }
/**
* Apply Principal Component Analysis to the provided data set.
@@ -27,33 +26,37 @@
* @param eigVal - contains eigen values in a column vector
* @param coeff - PCA Loadings/Coeffs/EigenVectors
*/
-void PCA::Apply(const arma::mat& data, arma::mat& transformedData,
- arma::vec& eigVal, arma::mat& coeffs)
+void PCA::Apply(const arma::mat& data,
+ arma::mat& transformedData,
+ arma::vec& eigVal,
+ arma::mat& coeffs) const
{
arma::mat transData = trans(data);
- if(centerData_)
+ if(centerData)
{
arma::rowvec means = arma::mean(transData, 0);
transData = transData - arma::ones<arma::colvec>(transData.n_rows) * means;
}
- if(scaleData_)
+ if(scaleData)
{
- transData = transData / (arma::ones<arma::colvec>(transData.n_rows) * stddev(transData, 0, 0));
+ transData = transData / (arma::ones<arma::colvec>(transData.n_rows) *
+ stddev(transData, 0, 0));
}
arma::mat covMat = cov(transData);
arma::eig_sym(eigVal, coeffs, covMat);
int n_eigVal = eigVal.n_elem;
- for(int i = 0; i < floor(n_eigVal / 2); i++)
+ for (int i = 0; i < floor(n_eigVal / 2); i++)
eigVal.swap_rows(i, (n_eigVal - 1) - i);
coeffs = arma::fliplr(coeffs);
transformedData = trans(coeffs) * data;
arma::colvec transformedDataMean = arma::mean(transformedData, 1);
- transformedData = transformedData - (transformedDataMean * arma::ones<arma::rowvec>(transformedData.n_cols));
+ transformedData = transformedData - (transformedDataMean *
+ arma::ones<arma::rowvec>(transformedData.n_cols));
}
/**
@@ -63,12 +66,12 @@
* @param transformedData - Data with PCA applied
* @param eigVal - contains eigen values in a column vector
*/
-void PCA::Apply(const arma::mat& data, arma::mat& transformedData,
- arma::vec& eigVal)
+void PCA::Apply(const arma::mat& data,
+ arma::mat& transformedData,
+ arma::vec& eigVal) const
{
arma::mat coeffs;
- Apply(data, transformedData,
- eigVal, coeffs);
+ Apply(data, transformedData, eigVal, coeffs);
}
/**
@@ -81,7 +84,7 @@
* from data matrix onto the basis vectors contained in the columns of
* coeff/eigen vector matrix with only newDimension number of columns chosen.
*/
-void PCA::Apply(arma::mat& data, const int newDimension)
+void PCA::Apply(arma::mat& data, const int newDimension) const
{
arma::mat coeffs;
arma::vec eigVal;
Modified: mlpack/trunk/src/mlpack/methods/pca/pca.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca.hpp 2011-12-14 18:17:27 UTC (rev 10797)
+++ mlpack/trunk/src/mlpack/methods/pca/pca.hpp 2011-12-14 19:05:19 UTC (rev 10798)
@@ -18,38 +18,6 @@
public:
PCA(const bool centerData = false, const bool scaleData = false);
- /* Return whether or not this PCA object will center the data when PCA
- * is performed.
- */
- bool CenterData() const
- {
- return centerData_;
- }
-
- /* Modify whether or not this PCA object will center the data when PCA
- * is performed.
- */
- bool& CenterData()
- {
- return centerData_;
- }
-
- /* Return whether or not this PCA object will scale(by standard deviation) the data when PCA
- * is performed.
- */
- bool ScaleData() const
- {
- return scaleData_;
- }
-
- /* Modify whether or not this PCA object will scale(by standard deviation) the data when PCA
- * is performed.
- */
- bool& ScaleData()
- {
- return scaleData_;
- }
-
/**
* Apply Principal Component Analysis to the provided data set.
*
@@ -59,7 +27,7 @@
* @param coeff - PCA Loadings/Coeffs/EigenVectors
*/
void Apply(const arma::mat& data, arma::mat& transformedData, arma::vec&
- eigVal, arma::mat& coeff);
+ eigVal, arma::mat& coeff) const;
/**
* Apply Principal Component Analysis to the provided data set.
@@ -69,7 +37,7 @@
* @param eigVal - contains eigen values in a column vector
*/
void Apply(const arma::mat& data, arma::mat& transformedData,
- arma::vec& eigVal);
+ arma::vec& eigVal) const;
/**
* Apply Dimensionality Reduction using Principal Component Analysis
@@ -81,17 +49,29 @@
* from data matrix onto the basis vectors contained in the columns of
* coeff/eigen vector matrix with only newDimension number of columns chosen.
*/
- void Apply(arma::mat& data, const int newDimension);
+ void Apply(arma::mat& data, const int newDimension) const;
/**
* Delete PCA object
*/
~PCA();
- private:
- bool centerData_;
- bool scaleData_;
+ //! Get whether or not this PCA object will center the data.
+ bool CenterData() const { return centerData; }
+ //! Modify whether or not this PCA object will center the data when PCA is
+ //! is performed.
+ bool& CenterData() { return centerData; }
+ //! Get whether or not this PCA object will scale (by standard deviation) the
+ //! data when PCA is performed.
+ bool ScaleData() const { return scaleData; }
+ //! Modify whether or not this PCA object will scale (by standard deviation)
+ //! the data when PCA is performed.
+ bool& ScaleData() { return scaleData; }
+
+ private:
+ bool centerData;
+ bool scaleData;
}; // class PCA
}; // namespace pca
More information about the mlpack-svn
mailing list