[mlpack-svn] r10303 - mlpack/trunk/src/mlpack/methods/pca

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Nov 16 16:29:55 EST 2011


Author: ajinkya
Date: 2011-11-16 16:29:55 -0500 (Wed, 16 Nov 2011)
New Revision: 10303

Modified:
   mlpack/trunk/src/mlpack/methods/pca/pca.cpp
   mlpack/trunk/src/mlpack/methods/pca/pca.hpp
Log:
Code refactoring. Added comments to the methods.

Modified: mlpack/trunk/src/mlpack/methods/pca/pca.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca.cpp	2011-11-16 21:17:59 UTC (rev 10302)
+++ mlpack/trunk/src/mlpack/methods/pca/pca.cpp	2011-11-16 21:29:55 UTC (rev 10303)
@@ -16,13 +16,20 @@
 {
 }
 
+/**
+ * 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
+ */
 void PCA::Apply(const arma::mat& data, arma::mat& transformedData,
            arma::vec& eigVal, arma::mat& coeffs)
 {
   arma::mat transData = trans(data);
   arma::vec means = mean(data, 1);
   arma::mat meanSubData = data - means * arma::ones<arma::rowvec>(data.n_cols);
-
   arma::mat covMat = ccov(meanSubData);
   arma::eig_sym(eigVal, coeffs, covMat);
 
@@ -31,12 +38,15 @@
     eigVal.swap_rows(i, (n_eigVal-1)-i);
 
   coeffs = arma::fliplr(coeffs);
-
   transformedData = trans(coeffs) * data;
 }
 
 /**
+ * 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
  */
 void PCA::Apply(const arma::mat& data, arma::mat& transformedData,
            arma::vec& eigVal)
@@ -44,7 +54,6 @@
   arma::mat transData = trans(data);
   arma::vec means = mean(data, 1);
   arma::mat meanSubData = data - means * arma::ones<arma::rowvec>(data.n_cols);
-
   arma::mat covMat = ccov(meanSubData);
   arma::mat eigVec;
   arma::eig_sym(eigVal, eigVec, covMat);
@@ -54,16 +63,24 @@
     eigVal.swap_rows(i, (n_eigVal-1)-i);
 
   eigVec = arma::fliplr(eigVec);
-
   transformedData = trans(eigVec) * data;
 }
 
+/**
+ * Apply Dimensionality Reduction using Principal Component Analysis
+ * to the provided data set.
+ *
+ * @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.
+ */
 void PCA::Apply(arma::mat& data, const int newDimension)
 {
   arma::mat transData = trans(data);
   arma::vec means = mean(data, 1);
   arma::mat meanSubData = data - means * arma::ones<arma::rowvec>(data.n_cols);
-
   arma::mat covMat = ccov(meanSubData);
   arma::mat eigVec;
   arma::vec eigVal;
@@ -74,14 +91,8 @@
     eigVal.swap_rows(i, (n_eigVal-1)-i);
 
   eigVec = arma::fliplr(eigVec);
-
   eigVec.shed_cols(newDimension, eigVec.n_cols - 1);
-  cout << eigVec << endl;
-  cout << data << endl;
-
   data = trans(eigVec) * data;
-
-  cout << data << endl;
 }
 
 PCA::~PCA()

Modified: mlpack/trunk/src/mlpack/methods/pca/pca.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca.hpp	2011-11-16 21:17:59 UTC (rev 10302)
+++ mlpack/trunk/src/mlpack/methods/pca/pca.hpp	2011-11-16 21:29:55 UTC (rev 10303)
@@ -14,25 +14,38 @@
   PCA();
 
   /**
-   * Apply Armadillo's Principal Component Analysis to the provided data set.
+   * Apply Principal Component Analysis to the provided data set.
    *
    * @param data - Data matrix
-   * @param coeff - PCA Loadings
-   * @param score - contains the coordinates of the original data in the new coordinate system defined by the principal components
+   * @param transformedData - Data with PCA applied
+   * @param eigVal - contains eigen values in a column vector
+   * @param coeff - PCA Loadings/Coeffs/EigenVectors
    */
-  void Apply(arma::mat& data, const int newDimension);
+  void Apply(const arma::mat& data, arma::mat& transformedData, arma::vec&
+             eigVal, arma::mat& coeff);
+
+  /**
+   * 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
+   */
   void Apply(const arma::mat& data, arma::mat& transformedData,
              arma::vec& eigVal);
-  void Apply(const arma::mat& data, arma::mat& transformedData, arma::vec&
-             eigVal, arma::mat& coeffs);
 
+  /**
+   * Apply Dimensionality Reduction using Principal Component Analysis
+   * to the provided data set.
+   *
+   * @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.
+   */
+  void Apply(arma::mat& data, const int newDimension);
 
-  /*
-
-
-  // And for someone who wants even more.
-  ;*/
-
   /**
    * Delete PCA object
    */




More information about the mlpack-svn mailing list