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

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Sun Nov 27 15:41:13 EST 2011


Author: rcurtin
Date: 2011-11-27 15:41:12 -0500 (Sun, 27 Nov 2011)
New Revision: 10428

Added:
   mlpack/trunk/src/mlpack/methods/pca/pca_main.cpp
Modified:
   mlpack/trunk/src/mlpack/methods/pca/CMakeLists.txt
   mlpack/trunk/src/mlpack/methods/pca/pca.cpp
   mlpack/trunk/src/mlpack/methods/pca/pca.hpp
Log:
Main executable for PCA.  A couple little tweaks to the PCA code here and there.


Modified: mlpack/trunk/src/mlpack/methods/pca/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/CMakeLists.txt	2011-11-27 07:59:03 UTC (rev 10427)
+++ mlpack/trunk/src/mlpack/methods/pca/CMakeLists.txt	2011-11-27 20:41:12 UTC (rev 10428)
@@ -16,3 +16,9 @@
 # the parent scope).
 set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
 
+add_executable(pca
+  pca_main.cpp
+)
+target_link_libraries(pca
+  mlpack
+)

Modified: mlpack/trunk/src/mlpack/methods/pca/pca.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca.cpp	2011-11-27 07:59:03 UTC (rev 10427)
+++ mlpack/trunk/src/mlpack/methods/pca/pca.cpp	2011-11-27 20:41:12 UTC (rev 10428)
@@ -1,5 +1,6 @@
 /**
  * @file pca.cpp
+ * @author Ajinkya Kale
  *
  * Implementation of PCA class to perform Principal Components Analysis on the
  * specified data set.

Modified: mlpack/trunk/src/mlpack/methods/pca/pca.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca.hpp	2011-11-27 07:59:03 UTC (rev 10427)
+++ mlpack/trunk/src/mlpack/methods/pca/pca.hpp	2011-11-27 20:41:12 UTC (rev 10428)
@@ -1,10 +1,15 @@
 /**
  * @file pca.hpp
+ * @author Ajinkya Kale
  *
  * Defines the PCA class to perform Principal Components Analysis on the
  * specified data set.
  */
+#ifndef __MLPACK_METHODS_PCA_PCA_HPP
+#define __MLPACK_METHODS_PCA_PCA_HPP
+
 #include <mlpack/core.hpp>
+
 namespace mlpack {
 namespace pca {
 
@@ -55,3 +60,5 @@
 
 }; // namespace pca
 }; // namespace mlpack
+
+#endif

Added: mlpack/trunk/src/mlpack/methods/pca/pca_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca_main.cpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/pca/pca_main.cpp	2011-11-27 20:41:12 UTC (rev 10428)
@@ -0,0 +1,59 @@
+/**
+ * @file pca_main.cpp
+ * @author Ryan Curtin
+ *
+ * Main executable to run PCA.
+ */
+#include <mlpack/core.hpp>
+
+#include "pca.hpp"
+
+using namespace mlpack;
+using namespace mlpack::pca;
+using namespace std;
+
+// Document program.
+PROGRAM_INFO("Principal Components Analysis", "This program performs principal "
+    "components analysis on the given dataset.  It will transform the data "
+    "onto its principal components, optionally performing dimensionality "
+    "reduction by ignoring the principal components with the smallest "
+    "eigenvalues.", "");
+
+// Parameters for program.
+PARAM_STRING_REQ("input_file", "Input dataset to perform PCA on.", "");
+PARAM_STRING_REQ("output_file", "Output dataset to perform PCA on.", "");
+PARAM_INT("new_dimensionality", "Desired dimensionality of output dataset.",
+    "", 0);
+
+int main(int argc, char** argv)
+{
+  // Parse commandline.
+  CLI::ParseCommandLine(argc, argv);
+
+  // Load input dataset.
+  string inputFile = CLI::GetParam<string>("input_file");
+  arma::mat dataset;
+  data::Load(inputFile.c_str(), dataset);
+
+  // Find out what dimension we want.
+  size_t newDimension = dataset.n_rows; // No reduction, by default.
+  if (CLI::HasParam("new_dimensionality"))
+  {
+    // Validate the parameter.
+    newDimension = (size_t) CLI::GetParam<int>("new_dimensionality");
+    if (newDimension < 1);
+    {
+      Log::Fatal << "Invalid value for new dimensionality (" << newDimension
+          << ")!  Must be greater than or equal to 1." << std::endl;
+    }
+  }
+
+  // Perform PCA.
+  PCA p;
+  Log::Info << "Performing PCA on dataset..." << std::endl;
+  p.Apply(dataset, newDimension);
+
+  // Now save the results.
+  string outputFile = CLI::GetParam<string>("output_file");
+  data::Save(outputFile.c_str(), dataset);
+}




More information about the mlpack-svn mailing list