mlpack.linear_regression

linear_regression(...)Simple Linear Regression and Prediction

>>> from mlpack import linear_regression

An implementation of simple linear regression and simple ridge regression using ordinary least squares. This solves the problem

y = X * b + e

where X (specified by 'training') and y (specified either as the last column of the input matrix 'training' or via the 'training_responses' parameter) are known and b is the desired variable. If the covariance matrix (X'X) is not invertible, or if the solution is overdetermined, then specify a Tikhonov regularization constant (with 'lambda_') greater than 0, which will regularize the covariance matrix to make it invertible. The calculated b may be saved with the 'output_predictions' output parameter.

Optionally, the calculated value of b is used to predict the responses for another matrix X' (specified by the 'test' parameter):

y' = X' * b

and the predicted responses y' may be saved with the 'output_predictions' output parameter. This type of regression is related to least-angle regression, which mlpack implements as the 'lars' program.

For example, to run a linear regression on the dataset 'X' with responses 'y', saving the trained model to 'lr_model', the following command could be used:

>>> output = linear_regression(training=X, training_responses=y)

>>> lr_model = output['output_model']

Then, to use 'lr_model' to predict responses for a test set 'X_test', saving the predictions to 'X_test_responses', the following command could be used:

>>> output = linear_regression(input_model=lr_model, test=X_test)

>>> X_test_responses = output['output_predictions']

## input options

- copy_all_inputs (bool): If specified, all input parameters will be deep copied before the method is run. This is useful for debugging problems where the input parameters are being modified by the algorithm, but can slow down the code.
- input_model (mlpack.LinearRegressionType): Existing LinearRegression model to use.
- lambda_ (float): Tikhonov regularization for ridge regression. If 0, the method reduces to linear regression. Default value 0.
- test (numpy matrix or arraylike, float dtype): Matrix containing X' (test regressors).
- training (numpy matrix or arraylike, float dtype): Matrix containing training set X (regressors).
- training_responses (numpy vector or array, float dtype): Optional vector containing y (responses). If not given, the responses are assumed to be the last row of the input file.
- verbose (bool): Display informational messages and the full list of parameters and timers at the end of execution.

## output options

The return value from the binding is a dict containing the following elements:

- output_model (mlpack.LinearRegressionType): Output LinearRegression model.
- output_predictions (numpy vector, float dtype): If --test_file is specified, this matrix is where the predicted responses will be saved.