mlpack: mlpack::regression::LinearRegression Class Reference
mlpack  2.2.0
mlpack::regression::LinearRegression Class Reference

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

Public Member Functions

 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 LinearRegression &linearRegression)
 Copy constructor. More...
 
 LinearRegression ()
 Empty constructor. More...
 
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...
 
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...
 
void Predict (const arma::mat &points, arma::vec &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...
 
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...
 

Private Attributes

bool intercept
 Indicates whether first parameter is intercept. More...
 
double lambda
 The Tikhonov regularization parameter for ridge regression (0 for linear regression). More...
 
arma::vec parameters
 The calculated B. 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/3]

mlpack::regression::LinearRegression::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/3]

mlpack::regression::LinearRegression::LinearRegression ( const LinearRegression linearRegression)

Copy constructor.

Parameters
linearRegressionthe other instance to copy parameters from.

◆ LinearRegression() [3/3]

mlpack::regression::LinearRegression::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 56 of file linear_regression.hpp.

References ComputeError(), intercept, Predict(), and Train().

Member Function Documentation

◆ ComputeError()

double mlpack::regression::LinearRegression::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(), and mlpack::distribution::RegressionDistribution::RegressionDistribution().

◆ Intercept()

bool mlpack::regression::LinearRegression::Intercept ( ) const
inline

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

Definition at line 114 of file linear_regression.hpp.

References intercept.

◆ Lambda() [1/2]

double mlpack::regression::LinearRegression::Lambda ( ) const
inline

Return the Tikhonov regularization parameter for ridge regression.

Definition at line 109 of file linear_regression.hpp.

References lambda.

◆ Lambda() [2/2]

double& mlpack::regression::LinearRegression::Lambda ( )
inline

Modify the Tikhonov regularization parameter for ridge regression.

Definition at line 111 of file linear_regression.hpp.

References lambda.

◆ Parameters() [1/2]

const arma::vec& mlpack::regression::LinearRegression::Parameters ( ) const
inline

Return the parameters (the b vector).

Definition at line 104 of file linear_regression.hpp.

References parameters.

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

◆ Parameters() [2/2]

arma::vec& mlpack::regression::LinearRegression::Parameters ( )
inline

Modify the parameters (the b vector).

Definition at line 106 of file linear_regression.hpp.

References parameters.

◆ Predict()

void mlpack::regression::LinearRegression::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().

◆ Serialize()

template<typename Archive >
void mlpack::regression::LinearRegression::Serialize ( Archive &  ar,
const unsigned  int 
)
inline

Serialize the model.

Definition at line 120 of file linear_regression.hpp.

References mlpack::data::CreateNVP(), intercept, lambda, and parameters.

◆ Train()

void mlpack::regression::LinearRegression::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().

Member Data Documentation

◆ intercept

bool mlpack::regression::LinearRegression::intercept
private

Indicates whether first parameter is intercept.

Definition at line 141 of file linear_regression.hpp.

Referenced by Intercept(), LinearRegression(), and Serialize().

◆ lambda

double mlpack::regression::LinearRegression::lambda
private

The Tikhonov regularization parameter for ridge regression (0 for linear regression).

Definition at line 138 of file linear_regression.hpp.

Referenced by Lambda(), and Serialize().

◆ parameters

arma::vec mlpack::regression::LinearRegression::parameters
private

The calculated B.

Initialized and filled by constructor to hold the least squares solution.

Definition at line 132 of file linear_regression.hpp.

Referenced by Parameters(), and Serialize().


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