Class KNearestNeighbors

java.lang.Object
com.jml.core.Model<double[][],​int[]>
com.jml.classifiers.KNearestNeighbors

public class KNearestNeighbors extends Model<double[][],​int[]>
K Nearest Neighbors (KNN) model. This model classifies data samples based on the K nearest training samples. The distance metric to determine the nearest samples is by default Euclidean distance. However, other Minkowski distance metrics can be used instead.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a KNearestNeighbors model with a default k of 3.
    Creates a KNearestNeighbors model with a specified k.
    KNearestNeighbors​(int k, int p)
    Creates a KNearestNeighbors model with a specified k and power parameter for distance metric.
  • Method Summary

    Modifier and Type
    Method
    Description
    Model<double[][],​int[]>
    fit​(double[][] features, int[] 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()
    int[]
    predict​(double[][] features)
    Uses fitted/trained model to make prediction on single feature.
    linalg.Matrix
    predict​(linalg.Matrix X, linalg.Matrix F)
    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.
    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

    • KNearestNeighbors

      public KNearestNeighbors()
      Creates a KNearestNeighbors model with a default k of 3. That is, the model will consider the three closest neighbors when making class prediction. The default distance metric is euclidean distance.
    • KNearestNeighbors

      public KNearestNeighbors(int k)
      Creates a KNearestNeighbors model with a specified k. The default distance metric is euclidean distance.
      Parameters:
      k - Number of neighbors to consider when making a class prediction.
    • KNearestNeighbors

      public KNearestNeighbors(int k, int p)
      Creates a KNearestNeighbors model with a specified k and power parameter for distance metric.
      Parameters:
      k - Number of neighbors to consider when making a class prediction.
      p - Power parameter of the Minkowski distance i.e. sum( | xi - yi | p ) 1/p. If p=2, this is equivalent to the euclidean distance. If p=1 this is equivalent to the manhattan distance.
  • Method Details

    • fit

      public Model<double[][],​int[]> fit(double[][] features, int[] targets)
      Fits or trains the model with the given features and targets.
      Specified by:
      fit in class Model<double[][],​int[]>
      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 int[] predict(double[][] features)
      Uses fitted/trained model to make prediction on single feature.
      Specified by:
      predict in class Model<double[][],​int[]>
      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 fit.
    • predict

      public linalg.Matrix predict(linalg.Matrix X, linalg.Matrix F)
      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[][],​int[]>
      Parameters:
      X - Features to make prediction on.
      F - The parameters of the Model. In the case of KNN, there are no parameters and so this will be ignored.
      Returns:
      prediction on the features using the given model parameters.
    • getParams

      public linalg.Matrix getParams()
      Gets the parameters of the trained model. Note that for a KNearestNeighbors model, there are no parameters to return.
      Specified by:
      getParams in class Model<double[][],​int[]>
      Returns:
      Features of model since there are no parameters in a KNearestNeighbors model. If model has not been fit, returns null.
    • saveModel

      public void saveModel(String filePath)
      Saves a trained model to the specified file path.
      Specified by:
      saveModel in class Model<double[][],​int[]>
      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[][],​int[]>
      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[][],​int[]>
      Returns:
      String representation of model.