[mlpack-svn] r10226 - mlpack/trunk/src/mlpack/methods/linear_regression
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Nov 9 17:27:13 EST 2011
Author: jcline3
Date: 2011-11-09 17:27:13 -0500 (Wed, 09 Nov 2011)
New Revision: 10226
Modified:
mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp
Log:
Better comments, coding style improvements
Modified: mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp 2011-11-09 22:06:22 UTC (rev 10225)
+++ mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp 2011-11-09 22:27:13 UTC (rev 10226)
@@ -10,30 +10,34 @@
/*
* We want to calculate the a_i coefficients of:
* \sum_{i=0}^n (a_i * x_i^i)
- * We add a row of ones to get a_0, where x_0^0 = 1, the intercept.
+ * In order to get the intercept value, we will add a row of ones.
*/
- // The number of rows
+ // We store the number of rows of the predictors.
+ // Reminder: Armadillo stores the data transposed from how we think of it,
+ // that is, columns are actually rows (see: column major order).
size_t n_cols;
-
n_cols = predictors.n_cols;
- // Add a row of ones, to get the intercept
+ // Here we add the row of ones to the predictors.
arma::rowvec ones;
ones.ones(n_cols);
- predictors.insert_rows(0,ones);
+ predictors.insert_rows(0, ones);
- // Set the parameters to the correct size, all zeros.
+ // We set the parameters to the correct size and initialize them to zero.
parameters.zeros(n_cols);
- // Compute the QR decomposition
+ // We compute the QR decomposition of the predictors.
+ // We transpose the predictors because they are in column major order.
arma::mat Q, R;
- arma::qr(Q,R,arma::trans(predictors));
+ arma::qr(Q, R, arma::trans(predictors));
- // Compute the parameters, R*B=Q^T*responses
- arma::solve( parameters, R, arma::trans(Q)*responses);
+ // We compute the parameters, B, like so:
+ // R * B = Q^T * responses
+ // B = Q^T * responses * R^-1
+ arma::solve(parameters, R, arma::trans(Q) * responses);
- // Remove the added row.
+ // We now remove the row of ones we added so the user's data is unmodified.
predictors.shed_row(0);
}
@@ -48,26 +52,27 @@
void LinearRegression::predict(arma::rowvec& predictions, const arma::mat& points)
{
- // The number of columns and rows
+ // We get the number of columns and rows of the dataset.
size_t n_cols, n_rows;
n_cols = points.n_cols;
n_rows = points.n_rows;
- // Sanity check
+ // We want to be sure we have the correct number of dimensions in the dataset.
assert(n_rows == parameters.n_rows - 1);
predictions.zeros(n_cols);
- // Set to a_0
+ // We set all the predictions to the intercept value initially.
predictions += parameters(0);
- // Iterate through the dimensions
- for(size_t i = 1; i < n_rows+1; ++i)
+ // Now we iterate through the dimensions of the data and parameters.
+ for(size_t i = 1; i < n_rows + 1; ++i)
{
- // Iterate through the datapoints
+ // Now we iterate through each row, or point, of the data.
for(size_t j = 0; j < n_cols; ++j)
{
- // Add in the next term: a_i * x_i
- predictions(j) += parameters(i) * points(i-1,j);
+ // Increment each prediction value by x_i * a_i, or the next dimensional
+ // coefficient and x value.
+ predictions(j) += parameters(i) * points(i - 1, j);
}
}
More information about the mlpack-svn
mailing list