linearfit

Linear Regression. It uses Gradient Descent to estimate the coefficients.

Attention: Available only with Activate commercial edition.

Syntax

parameters = linearfit(X,y)

parameters = linearfit(X,y,options)

Inputs

X
Training data.
Type: double
Dimension: vector | matrix
y
Target values.
Type: double
Dimension: vector | matrix
options
Type: struct
alpha
Learning rate of the Gradient Descent algorithm (default: 0.01).
Type: double | integer
Dimension: scalar
to_normalize
If true, input data X will be normalized before performing regression (default: false). It is done by subtracting mean and dividing by the standard deviation.
Type: Boolean
Dimension: logical
l2_penalty
Regularization Strength (default: 0). If value greater than 0, it becomes Ridge Regression.
Type: double | integer
Dimension: scalar
iters
Maximum number of iterations to apply Gradient Descent (default: 500).
Type: integer
Dimension: scalar
method
Optimization method to be used to estimate coefficients. Only supported method is Gradient Descent. Default: 'gd'
Type: char
Dimension: string

Outputs

parameters
Contains all the values passed to linearfit method as options. Additionally it has below key-value pairs.
Type: struct
scorer
Function handle pointing to r2 function (R2 Coefficient of Determination).
Type: function handle
intercept
Estimated intercept.
Type: integer
Dimension: scalar
coef
Estimated coefficients
Type: double
Dimension: vector
params
Contains both intercept and coef as a vector. It is used by predict method.
Type: double
Dimension: vector
costHistory
Cost of the model during training from 0 to specified number of iterations. It can be plotted.
Type: double
Dimension: vector
n_samples
Number of rows in the training data.
Type: integer
Dimension: scalar
n_features
Number of columns in the training data.
Type: integer
Dimension: scalar

Example

Usage of linearfit with options

X = [1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15; 16 17 18; 19 20 21];
y = [1, 2, 3, 4, 5, 6, 7];
options = struct;
options.to_normalize = true;
options.alpha = 0.3;
parameters = linearfit(X, y, options)
parameters = struct [
  alpha: 0.3
  coef: [Matrix] 3 x 1
  0.66667
  0.66667
  0.66667
  costHistory: [Matrix] 1 x 500 Row[1] …
  intercept: 4
  iters: 500
  l2_penalty: 0
  method: gd
  n_features: 3
  n_samples: 7
  params: [Matrix] 4 x 1
  4.00000
  0.66667
  0.66667
  0.66667
  scorer: @rmse
  to_normalize: 1
] 

Comments

It estimates the coefficient using Gradient Descent method. Once the coefficients are computed, outputs are predicted using linearpredict method. Output 'parameters' can be passed to linearpredict function