Bundle Core machine learning types: the seedable Random generator, Matrix2D linear algebra and activations, matrix reference holders, the NeuralNetwork and CSV-backed MatrixReader. Compile with -lib ml.
KNearestNeighbors
k-nearest-neighbors classifier over a matrix of numeric feature rows. Each row of the matrix carries the label at the same index in the labels array. Query returns the k closest rows, and Classify reduces them to the majority label.
Example
knn := KNearestNeighbors->New(matrix, labels);
label := knn->Classify(3, query);Operations
Classify #
Classifies a query point by majority vote of its k nearest neighbors. Ties are broken in favor of the label that first reaches the top count.
method : public : Classify(k:Int, query:Float[]) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| k | Int | number of neighbors to poll |
| query | Float | input feature vector |
Return
| Type | Description |
|---|---|
| String | predicted category label (empty string if there are no neighbors) |
Example
knn := KNearestNeighbors->New(matrix, labels);
knn->Classify(3, [57.0, 170.0])->PrintLine();