Package com.jml.classifiers
Class KNearestNeighbors
java.lang.Object
com.jml.core.Model<double[][],int[]>
com.jml.classifiers.KNearestNeighbors
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
ConstructorDescriptionCreates a KNearestNeighbors model with a default k of 3.KNearestNeighbors(int k)
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 TypeMethodDescriptionModel<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.inspect()
Forms a string of the important aspects of the model.
same astoString()
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
Saves a trained model to the specified file path.toString()
Forms a string of the important aspects of the model.
-
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
Fits or trains the model with the given features and targets.- Specified by:
fit
in classModel<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 classModel<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. -
getParams
public linalg.Matrix getParams()Gets the parameters of the trained model. Note that for a KNearestNeighbors model, there are no parameters to return. -
saveModel
Saves a trained model to the specified file path. -
inspect
Forms a string of the important aspects of the model.
same astoString()
-
toString
Forms a string of the important aspects of the model.
-