mlpack  master
LinearRegression Class Reference

A simple linear regression algorithm using ordinary least squares. More...

Public Member Functions

mlpack_deprecated LinearRegression (const arma::mat &predictors, const arma::vec &responses, const double lambda=0, const bool intercept=true, const arma::vec &weights=arma::vec())
 Creates the model. More...

 
 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...

 
mlpack_deprecated double ComputeError (const arma::mat &points, const arma::vec &responses) const
 Calculate the L2 squared error on the given predictors and responses using this linear regression model. 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...

 
bool Intercept () const
 Return whether or not an intercept term is used in the model. More...

 
double Lambda () const
 Return the Tikhonov regularization parameter for ridge regression. More...

 
double & Lambda ()
 Modify the Tikhonov regularization parameter for ridge regression. More...

 
const arma::vec & Parameters () const
 Return the parameters (the b vector). More...

 
arma::vec & Parameters ()
 Modify the parameters (the b vector). More...

 
mlpack_deprecated void Predict (const arma::mat &points, arma::vec &predictions) const
 Calculate y_i for each data point in points. More...

 
void Predict (const arma::mat &points, arma::rowvec &predictions) const
 Calculate y_i for each data point in points. More...

 
template
<
typename
Archive
>
void Serialize (Archive &ar, const unsigned int)
 Serialize the model. More...

 
mlpack_deprecated void Train (const arma::mat &predictors, const arma::vec &responses, const bool intercept=true, const arma::vec &weights=arma::vec())
 Train the LinearRegression model on the given data. More...

 
void Train (const arma::mat &predictors, const arma::rowvec &responses, const bool intercept=true)
 Train the LinearRegression model on the given data. More...

 
void 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...

 

Detailed Description

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.

Definition at line 26 of file linear_regression.hpp.

Constructor & Destructor Documentation

◆ LinearRegression() [1/4]

mlpack_deprecated LinearRegression ( const arma::mat &  predictors,
const arma::vec &  responses,
const double  lambda = 0,
const bool  intercept = true,
const arma::vec &  weights = arma::vec() 
)

Creates the model.

Parameters
predictorsX, matrix of data points to create B with.
responsesy, the measured data for each point in X.
lambdaRegularization constant for ridge regression.
interceptWhether or not to include an intercept term.
weightsObservation weights (for boosting).

◆ LinearRegression() [2/4]

LinearRegression ( const arma::mat &  predictors,
const arma::rowvec &  responses,
const double  lambda = 0,
const bool  intercept = true 
)

Creates the model.

Parameters
predictorsX, matrix of data points.
responsesy, the measured data for each point in X.
lambdaRegularization constant for ridge regression.
interceptWhether or not to include an intercept term.

◆ LinearRegression() [3/4]

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.

Parameters
predictorsX, matrix of data points.
responsesy, the measured data for each point in X.
weightsObservation weights (for boosting).
lambdaRegularization constant for ridge regression.
interceptWhether or not to include an intercept term.

◆ LinearRegression() [4/4]

LinearRegression ( )
inline

Empty constructor.

This gives a non-working model, so make sure Train() is called (or make sure the model parameters are set) before calling Predict()!

Definition at line 77 of file linear_regression.hpp.

References LinearRegression::ComputeError(), mlpack_deprecated, LinearRegression::Predict(), and LinearRegression::Train().

Member Function Documentation

◆ ComputeError() [1/2]

mlpack_deprecated double ComputeError ( const arma::mat &  points,
const arma::vec &  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.

Parameters
pointsMatrix of predictors (X).
responsesVector of responses (y).

Referenced by LinearRegression::LinearRegression(), and RegressionDistribution::RegressionDistribution().

◆ ComputeError() [2/2]

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.

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.

Parameters
pointsMatrix of predictors (X).
responsesTransposed vector of responses (y^T).

◆ Intercept()

bool Intercept ( ) const
inline

Return whether or not an intercept term is used in the model.

Definition at line 196 of file linear_regression.hpp.

◆ Lambda() [1/2]

double Lambda ( ) const
inline

Return the Tikhonov regularization parameter for ridge regression.

Definition at line 191 of file linear_regression.hpp.

◆ Lambda() [2/2]

double& Lambda ( )
inline

Modify the Tikhonov regularization parameter for ridge regression.

Definition at line 193 of file linear_regression.hpp.

◆ Parameters() [1/2]

const arma::vec& Parameters ( ) const
inline

Return the parameters (the b vector).

Definition at line 186 of file linear_regression.hpp.

Referenced by RegressionDistribution::Dimensionality(), and RegressionDistribution::Parameters().

◆ Parameters() [2/2]

arma::vec& Parameters ( )
inline

Modify the parameters (the b vector).

Definition at line 188 of file linear_regression.hpp.

◆ Predict() [1/2]

mlpack_deprecated void Predict ( const arma::mat &  points,
arma::vec &  predictions 
) const

Calculate y_i for each data point in points.

Parameters
pointsthe data points to calculate with.
predictionsy, will contain calculated values on completion.

Referenced by LinearRegression::LinearRegression().

◆ Predict() [2/2]

void Predict ( const arma::mat &  points,
arma::rowvec &  predictions 
) const

Calculate y_i for each data point in points.

Parameters
pointsthe data points to calculate with.
predictionsy, will contain calculated values on completion.

◆ Serialize()

void Serialize ( Archive &  ar,
const unsigned  int 
)
inline

Serialize the model.

Definition at line 202 of file linear_regression.hpp.

References mlpack::data::CreateNVP().

◆ Train() [1/3]

mlpack_deprecated void Train ( const arma::mat &  predictors,
const arma::vec &  responses,
const bool  intercept = true,
const arma::vec &  weights = arma::vec() 
)

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.

Parameters
predictorsX, the matrix of data points to train the model on.
responsesy, the vector of responses to each data point.
interceptWhether or not to fit an intercept term.
weightsObservation weights (for boosting).

Referenced by LinearRegression::LinearRegression(), and RegressionDistribution::RegressionDistribution().

◆ Train() [2/3]

void 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.

Parameters
predictorsX, the matrix of data points to train the model on.
responsesy, the responses to the data points.
interceptWhether or not to fit an intercept term.

◆ Train() [3/3]

void 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.

Parameters
predictorsX, the matrix of data points to train the model on.
responsesy, the responses to the data points.
interceptWhether or not to fit an intercept term.
weightsObservation weights (for boosting).

The documentation for this class was generated from the following file: