[mlpack-svn] r15835 - mlpack/trunk/src/mlpack/methods/linear_regression
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Sep 25 15:22:21 EDT 2013
Author: rcurtin
Date: Wed Sep 25 15:22:20 2013
New Revision: 15835
Log:
Integrate ComputeCost() as ComputeError() for #298, written by Sumedh.
Modified:
mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp
mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.hpp
Modified: mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp (original)
+++ mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp Wed Sep 25 15:22:20 2013
@@ -71,3 +71,29 @@
// Now add the intercept.
predictions += parameters(0);
}
+
+//! Compute the L2 squared error on the given predictors and responses.
+double LinearRegression::ComputeError(const arma::mat& predictors,
+ const arma::vec& responses) const
+{
+ // Get the number of columns and rows of the dataset.
+ const size_t nCols = predictors.n_cols;
+ const size_t nRows = predictors.n_rows;
+
+ // Ensure that we have the correct number of dimensions in the dataset.
+ if (nRows != parameters.n_rows - 1)
+ {
+ Log::Fatal << "The test data must have the same number of columns as the "
+ "training file." << std::endl;
+ }
+
+ // Calculate the differences between actual responses and predicted responses.
+ // We must also add the intercept (parameters(0)) to the predictions.
+ arma::vec temp = responses - arma::trans(
+ (arma::trans(parameters.subvec(1, parameters.n_elem - 1)) * predictors) +
+ parameters(0));
+
+ const double cost = arma::sum(arma::dot(temp, temp)) / nCols;
+
+ return cost;
+}
Modified: mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.hpp (original)
+++ mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.hpp Wed Sep 25 15:22:20 2013
@@ -53,6 +53,26 @@
*/
void Predict(const arma::mat& points, arma::vec& predictions);
+ /**
+ * Calculate the L2 squared error on the given predictors and responses using
+ * this linear regression model. This calculation returns
+ *
+ * \f[
+ * (1 / n) * \| y - X \theta \|^2_2
+ * \f]
+ *
+ * where \f$ y \f$ is the responses vector, \f$ X \f$ is the matrix of
+ * predictors, and \f$ \theta \f$ is the parameters of the trained linear
+ * regression model.
+ *
+ * As this number decreases to 0, the linear regression fit is better.
+ *
+ * @param predictors Matrix of predictors (X).
+ * @param responses Vector of responses (y).
+ */
+ double ComputeError(const arma::mat& points,
+ const arma::vec& responses) const;
+
//! Return the parameters (the b vector).
const arma::vec& Parameters() const { return parameters; }
//! Modify the parameters (the b vector).
More information about the mlpack-svn
mailing list