All Bundles

KNearestNeighbors

K-Nearest Neighbors algorithm

Operations

Code example:

matrix := [
  [51.0,  167.0]
  [58.0, 169.0]
  [62.0, 182.0]
  [69.0, 176.0]
  [64.0, 173.0]
  [65.0, 172.0]
  [56.0, 174.0]
  [57.0, 173.0]
  [55.0, 170.0]];

labels := [
  "underweight", 
  "normal", 
  "normal", 
  "normal", 
  "normal", 
  "underweight", 
  "normal", 
  "normal", 
  "normal"];

knn := KNearestNeighbors->New(matrix, labels);
nearest := knn->Query(3, [57.0, 170.0]);
each(neighbor := nearest) {
  neighbor->ToString()->PrintLine();
};

New

Constructor.

New(matrix:Float[,], labels:String[])
Parameters
NameTypeDescription
matrixFloat[,]matrix input matrix
labelsString[]categories labels

Query

Query the matrix for classification

method : public : Query(k:Int, query:Float[]) ~ KNeighbor[]
Parameters
NameTypeDescription
kIntnumber of nearest neighbors
queryFloat[]input query

Return
TypeDescription
KNeighbor[]k nearest neighbors