[mlpack-svn] r10369 - 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 23 18:43:22 EST 2011
Author: rcurtin
Date: 2011-11-23 18:43:22 -0500 (Wed, 23 Nov 2011)
New Revision: 10369
Modified:
mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp
mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.hpp
mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression_main.cpp
Log:
Update formatting of linear_regression to reflect decisions made in #153.
Modified: mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp 2011-11-23 23:38:09 UTC (rev 10368)
+++ mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp 2011-11-23 23:43:22 UTC (rev 10369)
@@ -4,7 +4,7 @@
namespace linear_regression {
LinearRegression::LinearRegression(arma::mat& predictors,
- const arma::colvec& responses)
+ const arma::colvec& responses)
{
/*
@@ -32,7 +32,7 @@
arma::mat Q, R;
arma::qr(Q, R, arma::trans(predictors));
- // We compute the parameters, B, like so:
+ // 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);
@@ -47,10 +47,10 @@
}
LinearRegression::~LinearRegression()
-{
-}
+{ }
-void LinearRegression::predict(arma::rowvec& predictions, const arma::mat& points)
+void LinearRegression::predict(arma::rowvec& predictions,
+ const arma::mat& points)
{
// We get the number of columns and rows of the dataset.
size_t n_cols, n_rows;
@@ -65,13 +65,13 @@
predictions += parameters(0);
// Now we iterate through the dimensions of the data and parameters.
- for(size_t i = 1; i < n_rows + 1; ++i)
+ for (size_t i = 1; i < n_rows + 1; ++i)
{
// Now we iterate through each row, or point, of the data.
- for(size_t j = 0; j < n_cols; ++j)
+ for (size_t j = 0; j < n_cols; ++j)
{
- // Increment each prediction value by x_i * a_i, or the next dimensional
- // coefficient and x value.
+ // 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);
}
Modified: mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.hpp 2011-11-23 23:38:09 UTC (rev 10368)
+++ mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.hpp 2011-11-23 23:43:22 UTC (rev 10369)
@@ -1,57 +1,78 @@
-#ifndef __MLPACK_METHODS_LINEAR_REGRESSCLIN_HPP
-#define __MLPACK_METHODS_LINEAR_REGRESSCLIN_HPP
+/**
+ * @file linear_regression.hpp
+ * @author James Cline
+ *
+ * Simple least-squares linear regression.
+ */
+#ifndef __MLPACK_METHODS_LINEAR_REGRESSION_LINEAR_REGRESSION_HPP
+#define __MLPACK_METHODS_LINEAR_REGRESSION_LINEAR_REGRESSION_HPP
#include <mlpack/core.h>
+
namespace mlpack {
namespace linear_regression {
-/**
- * A simple linear regresion algorithm using ordinary least squares.
+/**
+ * A simple linear regresion algorithm using ordinary least squares.
*/
class LinearRegression
{
- public:
- /** Creates the model.
- * @param predictors X, matrix of data points to create B with.
- * @param responses y, the measured data for each point in X
- */
- LinearRegression(arma::mat& predictors, const arma::colvec& responses);
+ public:
+ /**
+ * Creates the model.
+ *
+ * @param predictors X, matrix of data points to create B with.
+ * @param responses y, the measured data for each point in X
+ */
+ LinearRegression(arma::mat& predictors, const arma::colvec& responses);
- /** Initialize the model from a file.
- * @param filename the name of the file to load the model from.
- */
- LinearRegression(const std::string& filename);
+ /**
+ * Initialize the model from a file.
+ *
+ * @param filename the name of the file to load the model from.
+ */
+ LinearRegression(const std::string& filename);
- /** Destructor - no work done. */
- ~LinearRegression();
+ /**
+ * Destructor - no work done.
+ */
+ ~LinearRegression();
- /** Calculate y_i for each data point in points.
- * @param predictions y, will contain calculated values on completion.
- * @param points the data points to calculate with.
- */
- void predict(arma::rowvec& predictions, const arma::mat& points);
+ /**
+ * Calculate y_i for each data point in points.
+ *
+ * @param predictions y, will contain calculated values on completion.
+ * @param points the data points to calculate with.
+ */
+ void predict(arma::rowvec& predictions, const arma::mat& points);
- /** Returns the model.
- * @return the parameters which describe the least squares solution.
- */
- arma::vec getParameters();
+ /**
+ * Returns the model.
+ *
+ * @return the parameters which describe the least squares solution.
+ */
+ arma::vec getParameters();
- /** Saves the model.
- * @param filename the name of the file to load the model from.
- */
- bool save(const std::string& filename);
+ /**
+ * Saves the model.
+ *
+ * @param filename the name of the file to load the model from.
+ */
+ bool save(const std::string& filename);
- /** Loads the model.
- * @param filename the name of the file to load the model from.
- */
- bool load(const std::string& filename);
+ /**
+ * Loads the model.
+ *
+ * @param filename the name of the file to load the model from.
+ */
+ bool load(const std::string& filename);
- private:
- /** The calculated B.
- * Initialized and filled by constructor to hold the least squares solution.
- */
- arma::vec parameters;
-
+ private:
+ /**
+ * The calculated B.
+ * Initialized and filled by constructor to hold the least squares solution.
+ */
+ arma::vec parameters;
};
}; // namespace linear_regression
Modified: mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression_main.cpp 2011-11-23 23:38:09 UTC (rev 10368)
+++ mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression_main.cpp 2011-11-23 23:43:22 UTC (rev 10369)
@@ -1,3 +1,9 @@
+/**
+ * @file linear_regression_main.cpp
+ * @author James Cline
+ *
+ * Main function for least-squares linear regression.
+ */
#include <mlpack/core.h>
#include "linear_regression.hpp"
@@ -6,16 +12,18 @@
PARAM_STRING_REQ("train", "A file containing X", "linear_regression");
PARAM_STRING_REQ("test", "A file containing data points to predict on",
"linear_regression");
-PARAM_STRING("responses", "A file containing the y values for X, if not present,\
-it is assumed the last column of train contains these values.", "linear_regression",
- "");
-PARAM_MODULE("linear_regression", "Ordinary least squares linear regression, y=BX");
-PROGRAM_INFO("Simple Linear Regression", "An implementation of simple linear \
-regression using ordinary least squares.", "linear_regression");
+PARAM_STRING("responses", "A file containing the y values for X; if not "
+ "present, it is assumed the last column of train contains these values.",
+ "linear_regression", "");
+PARAM_MODULE("linear_regression",
+ "Ordinary least squares linear regression: y = BX");
+
+PROGRAM_INFO("Simple Linear Regression", "An implementation of simple linear "
+ "regression using ordinary least squares.", "linear_regression");
+
int main(int argc, char* argv[])
{
-
arma::vec B;
arma::colvec responses;
arma::mat predictors, file, points;
@@ -24,17 +32,17 @@
CLI::ParseCommandLine(argc, argv);
const std::string train_name =
- CLI::GetParam<std::string>("linear_regression/train");
+ CLI::GetParam<std::string>("linear_regression/train");
const std::string test_name =
- CLI::GetParam<std::string>("linear_regression/test");
+ CLI::GetParam<std::string>("linear_regression/test");
const std::string response_name =
- CLI::GetParam<std::string>("linear_regression/responses");
+ CLI::GetParam<std::string>("linear_regression/responses");
data::Load(train_name.c_str(), file, true);
size_t n_cols = file.n_cols,
n_rows = file.n_rows;
- if(response_name == "")
+ if (response_name == "")
{
predictors = file.submat(0,0, n_rows-2, n_cols-1);
// The initial predictors for y, Nx1
@@ -46,27 +54,21 @@
predictors = file;
// The initial predictors for y, Nx1
data::Load(response_name.c_str(), responses, true);
- if(responses.n_rows > 1)
- {
- std::cerr << "Error: The responses must have one column.\n";
- return 0;
- }
- if(responses.n_cols != n_cols)
- {
- std::cerr << "Error: The responses must have the same number of rows as\
- the training file.\n";
- return 0;
- }
+
+ if (responses.n_rows > 1)
+ Log::Fatal << "The responses must have one column.\n";
+
+ if (responses.n_cols != n_cols)
+ Log::Fatal << "The responses must have the same number of rows as the "
+ "training file.\n";
}
data::Load(test_name.c_str(), points, true);
- if(points.n_rows != n_rows)
- {
- std::cerr << "Error: The test data must have the same number of cols as\
- the training file.\n";
- return 0;
- }
+ if (points.n_rows != n_rows)
+ Log::Fatal << "The test data must have the same number of columns as the "
+ "training file.\n";
+
arma::rowvec predictions;
linear_regression::LinearRegression lr(predictors, responses);
More information about the mlpack-svn
mailing list