[mlpack-svn] r10228 - in mlpack/trunk/src/mlpack/methods: . pca

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Nov 10 01:23:14 EST 2011


Author: ajinkya
Date: 2011-11-10 01:23:14 -0500 (Thu, 10 Nov 2011)
New Revision: 10228

Added:
   mlpack/trunk/src/mlpack/methods/pca/
   mlpack/trunk/src/mlpack/methods/pca/CMakeLists.txt
   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:
Ticket #126 : PCA code

Added: mlpack/trunk/src/mlpack/methods/pca/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/CMakeLists.txt	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/pca/CMakeLists.txt	2011-11-10 06:23:14 UTC (rev 10228)
@@ -0,0 +1,25 @@
+cmake_minimum_required(VERSION 2.8)
+
+# Define the files we need to compile
+# Anything not in this list will not be compiled into MLPACK.
+set(SOURCES
+  pca.hpp
+  pca.cpp
+)
+
+# Add directory name to sources.
+set(DIR_SRCS)
+foreach(file ${SOURCES})
+  set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
+endforeach()
+# Append sources (with directory name) to list of all MLPACK sources (used at
+# the parent scope).
+set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
+
+add_executable(pca
+  pca_test.cpp
+)
+
+target_link_libraries(pca
+  mlpack
+)

Added: mlpack/trunk/src/mlpack/methods/pca/pca.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca.cpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/pca/pca.cpp	2011-11-10 06:23:14 UTC (rev 10228)
@@ -0,0 +1,19 @@
+#include "pca.hpp"
+#include <mlpack/core.h>
+
+namespace mlpack {
+namespace 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;*/
+	}
+};
+};

Added: mlpack/trunk/src/mlpack/methods/pca/pca.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca.hpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/pca/pca.hpp	2011-11-10 06:23:14 UTC (rev 10228)
@@ -0,0 +1,13 @@
+#include <mlpack/core.h>
+namespace mlpack {
+namespace pca {
+
+class PCA {
+
+	public:
+		PCA();
+		void Apply(arma::mat& coeff, arma::mat& score, arma::mat& data);
+
+};
+};
+};

Added: mlpack/trunk/src/mlpack/methods/pca/pca_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/pca/pca_test.cpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/pca/pca_test.cpp	2011-11-10 06:23:14 UTC (rev 10228)
@@ -0,0 +1,57 @@
+#include "pca.hpp"
+
+using namespace std;
+using namespace arma;
+using namespace mlpack;
+using namespace mlpack::pca;
+
+int main(int argc, char *argv[])
+{
+	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;
+
+	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;
+
+	mat score_actual;
+
+	score_actual	<< -0.0144 << -0.2645 << 0 << endr
+					<<  0.4819 <<  0.1262 << 0 << endr
+					<< -0.4675 <<  0.1383 << 0 << endr;
+
+	mat coeff;
+	mat score;
+
+
+	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;
+}




More information about the mlpack-svn mailing list