Class PolynomialRegression

java.lang.Object
com.jml.core.Model<double[],​double[]>
com.jml.linear_models.PolynomialRegression
Direct Known Subclasses:
LinearRegression, PolynomialRegressionSGD

public class PolynomialRegression extends Model<double[],​double[]>
Model for least squares linear regression of polynomials.

PolynomialRegression fits a model y = b0 + b1x + b2x2 + ... + bnxn 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 solved explicitly.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a default polynomial regression model.
    PolynomialRegression​(int degree)
    Creates a polynomial regression model with specified degree.
  • Method Summary

    Modifier and Type
    Method
    Description
    fit​(double[] features, double[] targets)
    Fits or trains the model with the given features and targets.
    linalg.Matrix
    Gets the parameters of the trained model.
    Forms a string of the important aspects of the model.
    same as toString()
    double[]
    predict​(double[] features)
    Uses fitted/trained model to make prediction on single feature.
    linalg.Matrix
    predict​(linalg.Matrix X, linalg.Matrix w)
    Makes a prediction using a model by specifying the parameters of the model.
    void
    saveModel​(String filePath)
    Saves a trained model to the specified file path including the name of the file.
    Forms a string of the important aspects of the model.

    Methods inherited from class com.jml.core.Model

    load

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • PolynomialRegression

      public PolynomialRegression()
      Creates a default polynomial regression model. The default model is a degree one polynomial.
    • PolynomialRegression

      public PolynomialRegression(int degree)
      Creates a polynomial regression model with specified degree.
      Parameters:
      degree - Degree of polynomial to fit data.
  • Method Details

    • fit

      public PolynomialRegression fit(double[] features, double[] targets)
      Fits or trains the model with the given features and targets. For both the features and targets parameters, if they are 2D arrays, then the number of rows in each must match and will be the number of samples in the data. The number of columns in each will be the number of features and targets in a single sample.

      For instance, if the features array has shape n-by-m and the targets array has shape n-by-k. Then there are n samples in the dataset, each individual sample has m features, and each individual sample has k targets.
      Specified by:
      fit in class Model<double[],​double[]>
      Parameters:
      features - The features of the training set.
      targets - The targets of the training set.
      Returns:
      This. i.e. the trained model.
    • predict

      public double[] predict(double[] features)
      Uses fitted/trained model to make prediction on single feature.
      Specified by:
      predict in class Model<double[],​double[]>
      Parameters:
      features - The features to make predictions on.
      Returns:
      The models predicted labels.
      Throws:
      IllegalArgumentException - Thrown if the features are not correctly sized per the specification when the model was compiled.
    • predict

      public linalg.Matrix predict(linalg.Matrix X, linalg.Matrix w)
      Makes a prediction using a model by specifying the parameters of the model. Unlike the other predict method, no model needs to be trained to use this method since the parameters provided define a model.
      Specified by:
      predict in class Model<double[],​double[]>
      Parameters:
      X - Features to make prediction on.
      w - Parameters of the model.
      Returns:
      prediction on the features using the given model parameters.
    • getParams

      public linalg.Matrix getParams()
      Gets the parameters of the trained model.
      Specified by:
      getParams in class Model<double[],​double[]>
      Returns:
      A matrix containing the parameters of the trained model.
    • saveModel

      public void saveModel(String filePath)
      Saves a trained model to the specified file path including the name of the file. File path must include the extension .mdl.
      Specified by:
      saveModel in class Model<double[],​double[]>
      Parameters:
      filePath - File path, including extension, to save fitted / trained model to.
    • inspect

      public String inspect()
      Forms a string of the important aspects of the model.
      same as toString()
      Specified by:
      inspect in class Model<double[],​double[]>
      Returns:
      Details of model as string.
    • toString

      public String toString()
      Forms a string of the important aspects of the model.
      Specified by:
      toString in class Model<double[],​double[]>
      Returns:
      String representation of model.