API Reference - Models - KNearestNeighbours (KNN)
Stored Model Parameters
Contains a table of matrices.
-
ModelParameters[1]: Feature Matrix
-
ModelParameters[2]: Label Vector / Label Matrix
Constructors
new()
Create new model object. If any of the arguments are nil, default argument values for that argument will be used.
KNearestNeighbours.new(kValue: integer, distanceFunction: string): ModelObject
Parameters:
-
kValue: The number of closest data points taken into consideration for majority voting to determine the class of a given data point.
-
distanceFunction: The distance function to be used to measure the similarity between two data points. Available options are:
-
Euclidean
-
Manhattan
-
Cosine
-
Returns:
- ModelObject: The generated model object.
Functions
setParameters()
Set model’s parameters. When any of the arguments are nil, previous argument values for that argument will be used.
KNearestNeighbours:setParameters(kValue: integer, distanceFunction: string)
Parameters:
-
kValue: The number of closest data points taken into consideration for majority voting to determine the class of a given data point.
-
distanceFunction: The distance function to be used to measure the similarity between two data points. Available options are:
-
Euclidean
-
Manhattan
-
Cosine
-
train()
Train the model.
KNearestNeighbours:train(featureMatrix: Matrix, labelVector: Matrix): number[]
Parameters:
-
featureMatrix: Matrix containing all data.
-
labelVector: A (n x 1) matrix containing values related to featureMatrix.
Returns:
- costArray: An array containing cost values.
predict()
Predict the values for given data.
KNearestNeighbours:predict(featureMatrix: Matrix, returnOriginalOutput: boolean): Matrix, Matrix -OR- Matrix
Parameters
-
featureMatrix: Matrix containing all data.
-
returnOriginalOutput: Set whether or not to return predicted matrix instead of value with highest probability.
Returns:
-
predictedlabelVector: A vector tcontaining predicted labels generated from the model.
-
valueVector: A vector that contains the values of predicted labels.
-OR-
- predictedMatrix: A matrix containing all distances between stored and given data points.