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.
KDTree
K-d tree for fast exact nearest-neighbor queries over numeric rows. Built by median splits cycling through the dimensions; queries prune subtrees whose splitting plane lies farther than the current k-th best distance. Equivalent results to a brute-force scan (Euclidean).
Example
tree := KDTree->New(matrix);
nearest := tree->Nearest(3, [57.0, 170.0]); # row indexes, closest firstOperations
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();Nearest #
Finds the k nearest rows to the query point.
method : public : Nearest(k:Int, query:Float[]) ~ Int[]Parameters
| Name | Type | Description |
|---|---|---|
| k | Int | number of neighbors |
| query | Float | query point (length = features) |
Return
| Type | Description |
|---|---|
| Int | row indexes sorted closest-first (fewer than k when the tree is smaller), or Nil on invalid input |
New # constructor
Builds the tree from a data matrix.
New(data:Float[,])Parameters
| Name | Type | Description |
|---|---|---|
| data | Float | matrix (rows=samples, cols=features) |
New # constructor
Constructor.
New(matrix:Float[,], labels:String[])Parameters
| Name | Type | Description |
|---|---|---|
| matrix | Float | matrix input matrix |
| labels | String | categories labels |