Class LinearRegressionSGD


public class LinearRegressionSGD extends LinearRegression
Model for least squares linear regression of one variable by stochastic gradient descent.

LinearRegressionSGD fits a model y = b0 + b1x to the datasets by minimizing the residuals of the sum of squares between the values in the target dataset and the values predicted by the model. This is using stochastic gradient descent.
  • Constructor Details

    • LinearRegressionSGD

      public LinearRegressionSGD()
      Creates a LinearRegressionSGD model.
      This will use default settings for gradient descent:
          Learning Rate: 0.002
          Threshold: 0.5e-5
          Maximum Iterations: 1000
          Scheduler: None
       
    • LinearRegressionSGD

      public LinearRegressionSGD(double learningRate, int maxIterations, double threshold)
      Creates a LinearRegressionSGD model. When the fit method is called, Stochastic Gradient Descent will use the provided learning rate and will stop if it does not converge within the threshold by the specified number of max iterations.
      Parameters:
      learningRate - Learning rate to use during Stochastic Gradient Descent
      threshold - Threshold for early stopping during Stochastic Gradient Descent. If the loss is less than the specified threshold, gradient descent will stop early.
      maxIterations - Maximum number of iterations to run for during Stochastic Gradient Descent.
    • LinearRegressionSGD

      public LinearRegressionSGD(double learningRate, int maxIterations)
      Creates a LinearRegressionSGD model. When the fit method is called, Stochastic Gradient Descent will use the provided learning rate and will stop if it does not converge by the specified number of max iterations.
      Parameters:
      learningRate - Learning rate to use during Stochastic Gradient Descent.
      maxIterations - Maximum number of iterations to run for during Stochastic Gradient Descent.
    • LinearRegressionSGD

      public LinearRegressionSGD(double learningRate)
      Creates a LinearRegressionSGD model. When the fit method is called, Stochastic Gradient Descent will use the provided learning rate and will stop if it does not converge by the specified number of max iterations.
      Parameters:
      learningRate - Learning rate to use during Stochastic Gradient Descent.
    • LinearRegressionSGD

      public LinearRegressionSGD(int maxIterations)
      Creates a LinearRegressionSGD model. When the fit method is called, Stochastic Gradient Descent will use the provided learning rate and will stop if it does not converge by the specified number of max iterations.
      Parameters:
      maxIterations - Maximum number of iterations to run for during Stochastic Gradient Descent.
  • Method Details

    • fit

      public LinearRegressionSGD fit(double[] features, double[] targets)
      Fits or trains the model with the given features and targets.
      Overrides:
      fit in class LinearRegression
      Parameters:
      features - The features of the training set.
      targets - The targets of the training set.
      Returns:
      Returns details of the fitting / training process.
      Throws:
      IllegalArgumentException - Can be thrown for the following reasons
      - If key, value pairs in args are unspecified or invalid arguments.
      - If the features and targets are not correctly sized per the specification when the model was compiled.
    • getLossHist

      public double[] getLossHist()
      Gets the loss history from training.
      Returns:
      The loss of every iteration stored in a List.