mlpack
|
A simple linear regression algorithm using ordinary least squares. More...
#include <linear_regression.hpp>
Public Member Functions | |
LinearRegression (const arma::mat &predictors, const arma::rowvec &responses, const double lambda=0, const bool intercept=true) | |
Creates the model. More... | |
LinearRegression (const arma::mat &predictors, const arma::rowvec &responses, const arma::rowvec &weights, const double lambda=0, const bool intercept=true) | |
Creates the model with weighted learning. More... | |
LinearRegression () | |
Empty constructor. More... | |
double | Train (const arma::mat &predictors, const arma::rowvec &responses, const bool intercept=true) |
Train the LinearRegression model on the given data. More... | |
double | Train (const arma::mat &predictors, const arma::rowvec &responses, const arma::rowvec &weights, const bool intercept=true) |
Train the LinearRegression model on the given data and weights. More... | |
void | Predict (const arma::mat &points, arma::rowvec &predictions) const |
Calculate y_i for each data point in points. More... | |
double | ComputeError (const arma::mat &points, const arma::rowvec &responses) const |
Calculate the L2 squared error on the given predictors and responses using this linear regression model. More... | |
const arma::vec & | Parameters () const |
Return the parameters (the b vector). | |
arma::vec & | Parameters () |
Modify the parameters (the b vector). | |
double | Lambda () const |
Return the Tikhonov regularization parameter for ridge regression. | |
double & | Lambda () |
Modify the Tikhonov regularization parameter for ridge regression. | |
bool | Intercept () const |
Return whether or not an intercept term is used in the model. | |
template<typename Archive > | |
void | serialize (Archive &ar, const uint32_t) |
Serialize the model. | |
A simple linear regression algorithm using ordinary least squares.
Optionally, this class can perform ridge regression, if the lambda parameter is set to a number greater than zero.
LinearRegression::LinearRegression | ( | const arma::mat & | predictors, |
const arma::rowvec & | responses, | ||
const double | lambda = 0 , |
||
const bool | intercept = true |
||
) |
Creates the model.
predictors | X, matrix of data points. |
responses | y, the measured data for each point in X. |
lambda | Regularization constant for ridge regression. |
intercept | Whether or not to include an intercept term. |
LinearRegression::LinearRegression | ( | const arma::mat & | predictors, |
const arma::rowvec & | responses, | ||
const arma::rowvec & | weights, | ||
const double | lambda = 0 , |
||
const bool | intercept = true |
||
) |
Creates the model with weighted learning.
predictors | X, matrix of data points. |
responses | y, the measured data for each point in X. |
weights | Observation weights (for boosting). |
lambda | Regularization constant for ridge regression. |
intercept | Whether or not to include an intercept term. |
|
inline |
double LinearRegression::ComputeError | ( | const arma::mat & | points, |
const arma::rowvec & | responses | ||
) | const |
Calculate the L2 squared error on the given predictors and responses using this linear regression model.
This calculation returns
\[ (1 / n) * \| y - X B \|^2_2 \]
where \( y \) is the responses vector, \( X \) is the matrix of predictors, and \( B \) is the parameters of the trained linear regression model.
As this number decreases to 0, the linear regression fit is better.
points | Matrix of predictors (X). |
responses | Transposed vector of responses (y^T). |
void LinearRegression::Predict | ( | const arma::mat & | points, |
arma::rowvec & | predictions | ||
) | const |
Calculate y_i for each data point in points.
points | the data points to calculate with. |
predictions | y, will contain calculated values on completion. |
double LinearRegression::Train | ( | const arma::mat & | predictors, |
const arma::rowvec & | responses, | ||
const bool | intercept = true |
||
) |
Train the LinearRegression model on the given data.
Careful! This will completely ignore and overwrite the existing model. This particular implementation does not have an incremental training algorithm. To set the regularization parameter lambda, call Lambda() or set a different value in the constructor.
predictors | X, the matrix of data points to train the model on. |
responses | y, the responses to the data points. |
intercept | Whether or not to fit an intercept term. |
double LinearRegression::Train | ( | const arma::mat & | predictors, |
const arma::rowvec & | responses, | ||
const arma::rowvec & | weights, | ||
const bool | intercept = true |
||
) |
Train the LinearRegression model on the given data and weights.
Careful! This will completely ignore and overwrite the existing model. This particular implementation does not have an incremental training algorithm. To set the regularization parameter lambda, call Lambda() or set a different value in the constructor.
predictors | X, the matrix of data points to train the model on. |
responses | y, the responses to the data points. |
weights | Observation weights (for boosting). |
intercept | Whether or not to fit an intercept term. |