Package com.jml.core

Class Model<X,​Y>

java.lang.Object
com.jml.core.Model<X,​Y>
Type Parameters:
X - The type of the features dataset.
Y - The type of the targets dataset.
Direct Known Subclasses:
KNearestNeighbors, LogisticRegression, MultipleLinearRegression, NeuralNetwork, Perceptron, PolynomialRegression

public abstract class Model<X,​Y> extends Object
This interface specifies the requirements for a machine learning model.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract Model<X,​Y>
    fit​(X features, Y targets)
    Fits or trains the model with the given features and targets.
    abstract linalg.Matrix
    Gets the parameters of the trained model.
    abstract String
    Forms a string of the important aspects of the model.
    same as toString()
    static Model
    load​(String filePath)
    Loads a model from specified file path including extension.
    Models must be saved as an MDL file (i.e.
    abstract linalg.Matrix
    predict​(linalg.Matrix X, linalg.Matrix w)
    Makes a prediction using a model by specifying the parameters of the model.
    abstract Y
    predict​(X features)
    Uses fitted/trained model to make prediction on single feature.
    abstract void
    saveModel​(String filePath)
    Saves a trained model to the specified file path.
    abstract String
    Forms a string of the important aspects of the model.

    Methods inherited from class java.lang.Object

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

    • Model

      public Model()
  • Method Details

    • fit

      public abstract Model<X,​Y> fit(X features, Y 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.
      Parameters:
      features - The features of the training set.
      targets - The targets of the training set.
      Returns:
      This. i.e. the trained model.
      Throws:
      IllegalArgumentException - Thrown if the features and targets are not correctly sized per the specification when the model was compiled.
    • predict

      public abstract Y predict(X features)
      Uses fitted/trained model to make prediction on single feature.
      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.
      IllegalStateException - Thrown if the model has not been compiled and fit.
    • predict

      public abstract 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.
      Parameters:
      X - Features to make prediction on.
      w - Parameters of the model.
      Returns:
      prediction on the features using the given model parameters.
    • getParams

      public abstract linalg.Matrix getParams()
      Gets the parameters of the trained model.
      Returns:
      A matrix containing the parameters of the trained model.
    • saveModel

      public abstract void saveModel(String filePath)
      Saves a trained model to the specified file path.
      Parameters:
      filePath - File path, including extension, to save fitted / trained model to.
    • inspect

      public abstract String inspect()
      Forms a string of the important aspects of the model.
      same as toString()
      Returns:
      Details of model as string.
    • toString

      public abstract String toString()
      Forms a string of the important aspects of the model.
      Overrides:
      toString in class Object
      Returns:
      String representation of model.
    • load

      public static Model load(String filePath)
      Loads a model from specified file path including extension.
      Models must be saved as an MDL file (i.e. *.mdl).
      Parameters:
      filePath - File path, including file extension, of model to load.
      Returns:
      Returns a saved trained model from the file path.