[mlpack-svn] r10252 - mlpack/trunk/src/mlpack/methods/pca
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Nov 11 16:29:56 EST 2011
Author: ajinkya
Date: 2011-11-11 16:29:55 -0500 (Fri, 11 Nov 2011)
New Revision: 10252
Modified:
mlpack/trunk/src/mlpack/methods/pca/pca.cpp
mlpack/trunk/src/mlpack/methods/pca/pca.hpp
mlpack/trunk/src/mlpack/methods/pca/pca_test.cpp
Log:
added comments and made some formatting changes
Modified: mlpack/trunk/src/mlpack/methods/pca/pca.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca.cpp 2011-11-11 21:11:45 UTC (rev 10251)
+++ mlpack/trunk/src/mlpack/methods/pca/pca.cpp 2011-11-11 21:29:55 UTC (rev 10252)
@@ -1,3 +1,9 @@
+/**
+ * @file pca.cpp
+ *
+ * Implementation of PCA class to perform Principal Components Analysis on the
+ * specified data set.
+ */
#include "pca.hpp"
#include <mlpack/core.h>
@@ -4,16 +10,22 @@
namespace mlpack {
namespace pca {
- PCA::PCA(){}
+PCA::PCA()
+{
+}
- void PCA::Apply(arma::mat& coeff, arma::mat& score, arma::mat& data)
- {
- arma::princomp(coeff, score, arma::trans(data));
- /*arma::vec eigval;
- arma::mat eigvec;
- arma::mat cov_mat = arma::cov(m_data);
- arma::eig_sym(eigval, eigvec, cov_mat);
- data_transformed = arma::trans(eigvec) * m_data;*/
- }
-};
-};
+/**
+ * Apply Armadillo's Principal Component Analysis on the data set.
+ */
+void PCA::Apply(const arma::mat& data, arma::mat& coeff, arma::mat& score)
+{
+ //Armadillo's PCA api
+ arma::princomp(coeff, score, arma::trans(data));
+}
+
+PCA::~PCA()
+{
+}
+
+}; // namespace mlpack
+}; // namespace pca
Modified: mlpack/trunk/src/mlpack/methods/pca/pca.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca.hpp 2011-11-11 21:11:45 UTC (rev 10251)
+++ mlpack/trunk/src/mlpack/methods/pca/pca.hpp 2011-11-11 21:29:55 UTC (rev 10252)
@@ -1,3 +1,9 @@
+/**
+ * @file pca.hpp
+ *
+ * Defines the PCA class to perform Principal Components Analysis on the
+ * specified data set.
+ */
#include <mlpack/core.h>
namespace mlpack {
namespace pca {
@@ -2,10 +8,24 @@
-class PCA {
+class PCA
+{
+ public:
+ PCA();
- public:
- PCA();
- void Apply(arma::mat& coeff, arma::mat& score, arma::mat& data);
+ /**
+ * Apply Armadillo's 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
+ */
+ void Apply(const arma::mat& data, arma::mat& coeff, arma::mat& score);
-};
-};
-};
+ /**
+ * Delete PCA object
+ */
+ ~PCA();
+
+}; // class PCA
+
+}; // namespace pca
+}; // namespace mlpack
Modified: mlpack/trunk/src/mlpack/methods/pca/pca_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca_test.cpp 2011-11-11 21:11:45 UTC (rev 10251)
+++ mlpack/trunk/src/mlpack/methods/pca/pca_test.cpp 2011-11-11 21:29:55 UTC (rev 10252)
@@ -1,3 +1,9 @@
+/**
+ * @file pca_test.cpp
+ *
+ * Test file for PCA class
+ *
+ */
#include "pca.hpp"
using namespace std;
@@ -7,51 +13,49 @@
int main(int argc, char *argv[])
{
- int n_rows;
- int n_cols;
+ int n_rows;
+ int n_cols;
- mat data;
- data << 0.8402 << 0.7984 << 0.3352 << endr
- << 0.3944 << 0.9116 << 0.7682 << endr
- << 0.7831 << 0.1976 << 0.2778 << endr;
+ //input data set
+ mat data;
+ data << 0.8402 << 0.7984 << 0.3352 << endr
+ << 0.3944 << 0.9116 << 0.7682 << endr
+ << 0.7831 << 0.1976 << 0.2778 << endr;
- mat coeff_actual;
+ //PCA loadings/coefficient matrix
+ mat coeff_actual;
+ coeff_actual << -0.41721 << -0.610904 << 0.672854 << endr
+ << 0.743785 << -0.654959 << -0.133446 << endr
+ << 0.522226 << 0.444775 << 0.727636 << endr;
- coeff_actual << -0.41721 << -0.610904 << 0.672854 << endr
- << 0.743785 << -0.654959 << -0.133446 << endr
- << 0.522226 << 0.444775 << 0.727636 << endr;
+ //PCA score matrix for transformed
+ mat score_actual;
+ score_actual << -0.0144 << -0.2645 << 0 << endr
+ << 0.4819 << 0.1262 << 0 << endr
+ << -0.4675 << 0.1383 << 0 << endr;
- mat score_actual;
+ mat coeff;
+ mat score;
- score_actual << -0.0144 << -0.2645 << 0 << endr
- << 0.4819 << 0.1262 << 0 << endr
- << -0.4675 << 0.1383 << 0 << endr;
+ //MLPACK data set is organized with rows as attributes and columns as data
+ //points. Hence the need to transpose the matrix before applying Armadillo's
+ //princomp PCA api
+ mat trans_data = arma::trans(data);
+ mlpack::pca::PCA p;
+ p.Apply(trans_data, coeff, score);
- mat coeff;
- mat score;
+ n_rows = data.n_rows;
+ n_cols = data.n_cols;
+ //verify the PCA results based on the loadings(coeff matrix) and the score
+ for(int i = 0; i < n_rows; i++)
+ {
+ for(int j = 0; j < n_cols; j++)
+ {
+ assert(fabs(coeff_actual(i, j) - coeff(i, j)) < 0.0001);
+ assert(fabs(score_actual(i, j) - score(i, j)) < 0.0001);
+ }
+ }
- mat trans_data = arma::trans(data);
- mlpack::pca::PCA p;
- p.Apply(coeff, score, trans_data);
-
- n_rows = data.n_rows;
- n_cols = data.n_cols;
-
- /*cout << "test matrix : " << endl << data;
-
- cout << "coeff : " << endl << coeff;
-
- cout << "score : " << endl << score;*/
-
- for(int i = 0; i < n_rows; i++)
- {
- for(int j = 0; j < n_cols; j++)
- {
- assert(fabs(coeff_actual(i, j) - coeff(i, j)) < 0.0001);
- assert(fabs(score_actual(i, j) - score(i, j)) < 0.0001);
- }
- }
-
- return 0;
+ return 0;
}
More information about the mlpack-svn
mailing list