[mlpack-svn] r10225 - in mlpack/trunk/src/mlpack: methods/linear_regression tests

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Nov 9 17:06:22 EST 2011


Author: jcline3
Date: 2011-11-09 17:06:22 -0500 (Wed, 09 Nov 2011)
New Revision: 10225

Modified:
   mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp
   mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp
Log:
Linear regression changes & fixes

* Now uses QR decomposition to calculate parameters, should be faster
* predict() now runs the correct number of iterations (was one too few)

Calculates intercept et al correctly, now, to within .05 absolute error.


Modified: mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp	2011-11-09 19:41:55 UTC (rev 10224)
+++ mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp	2011-11-09 22:06:22 UTC (rev 10225)
@@ -13,25 +13,28 @@
    * We add a row of ones to get a_0, where x_0^0 = 1, the intercept.
    */
 
-  // The number of columns and rows
-  size_t n_cols, n_rows;
+  // The number of rows
+  size_t n_cols;
 
   n_cols = predictors.n_cols;
-  n_rows = predictors.n_rows;
 
   // Add a row of ones, to get the intercept
   arma::rowvec ones;
   ones.ones(n_cols);
   predictors.insert_rows(0,ones);
-  // We have an additional row, now
-  ++n_rows;
 
   // Set the parameters to the correct size, all zeros.
   parameters.zeros(n_cols);
 
-  // inverse( A^T * A ) * A^T * responses, where A = predictors
-  parameters = arma::inv((predictors * arma::trans(predictors))) *
-    predictors * responses;
+  // Compute the QR decomposition
+  arma::mat Q, R;
+  arma::qr(Q,R,arma::trans(predictors));
+
+  // Compute the parameters, R*B=Q^T*responses
+  arma::solve( parameters, R, arma::trans(Q)*responses);
+
+  // Remove the added row.
+  predictors.shed_row(0);
 }
 
 LinearRegression::LinearRegression(const std::string& filename)
@@ -56,8 +59,11 @@
   predictions.zeros(n_cols);
   // Set to a_0
   predictions += parameters(0);
-  for(size_t i = 1; i < n_rows; ++i)
+
+  // Iterate through the dimensions
+  for(size_t i = 1; i < n_rows+1; ++i)
   {
+    // Iterate through the datapoints
     for(size_t j = 0; j < n_cols; ++j)
     {
       // Add in the next term: a_i * x_i

Modified: mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp	2011-11-09 19:41:55 UTC (rev 10224)
+++ mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp	2011-11-09 22:06:22 UTC (rev 10225)
@@ -24,7 +24,6 @@
     // We'll randomly select some coefficients for the linear response.
     arma::vec coeffs;
     coeffs.randu(4);
-    coeffs(0) = 0;
 
     // Now generate each point.
     for (size_t row = 0; row < 3; row++)
@@ -51,14 +50,8 @@
 
     // Output result and verify we have less than 5% error from "correct" value
     // for each point
-    std::cout << "Actual model:\n" << coeffs << '\n';
-    std::cout << "Parameters:\n" << lr.getParameters() << '\n';
-    std::cout << "Predictors:\n" << predictors << '\n';
-    std::cout << "Points:\n" << points << '\n' << "Predictions:\n"
-        << predictions << '\n';
-    std::cout << "Correct:\n" << responses << '\n';
     for(size_t i = 0; i < predictions.n_cols; ++i)
-      BOOST_REQUIRE_CLOSE(predictions(i), responses(i), 5);
+      BOOST_REQUIRE_SMALL(predictions(i) - responses(i), .05);
   }
 
 BOOST_AUTO_TEST_SUITE_END();




More information about the mlpack-svn mailing list