knn_model
Builtin index · Statistics & ML
result = knn_model(X, y, k, [metric=], [kind=])
Input: X, y, k, metric, kind · Output: result
What it is
k-nearest-neighbours on an N-row, D-column X and a length-N target y, with integer neighbour count k, an optional metric string (distance function name), and kind ("regression" or "classification", never inferred). Fitting only stores X/y; all the work happens inside .predict, which returns a length-M vector for an M-row query matrix.
Overloads
X can be number or string; y can be number or string; k can be number or string; metric can be number or string; kind can be number or string.
2 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.
| Code | Input | Output | Description |
result = knn_model(X, y, k, [metric=], [kind=]) | X numberk-nearest-neighbours on an N-row, D-column X and a length-N target y, with integer neighbour count k, an optional metric string (distance function name), and kind ("regression" or "classification", never inferred).y numberk-nearest-neighbours on an N-row, D-column X and a length-N target y, with integer neighbour count k, an optional metric string (distance function name), and kind ("regression" or "classification", never inferred).k numberkind = "regression" predicts the mean of the k nearest neighbours' target values -- a number that can fall between any two training targets.metric number, optionalk-nearest-neighbours on an N-row, D-column X and a length-N target y, with integer neighbour count k, an optional metric string (distance function name), and kind ("regression" or "classification", never inferred).kind number, optionalkind = "regression" predicts the mean of the k nearest neighbours' target values -- a number that can fall between any two training targets. | result Model | — |
Examples
Written for this form and run by the build -- the output below is what the engine actually produced.
Xk = [0.0; 1.0; 2.0; 3.0]
yk = [10.0, 12.0, 30.0, 32.0]
knr = knn_model(Xk, yk, 2, kind = "regression")
q = [1.5] as matrix(1, 1)
print("knn_model regression predict(1.5): {knr.predict(q)}") # mean of the 2 nearest y's
knn_model regression predict(1.5): [21]
| Code | Input | Output | Description |
result = knn_model(X, y, k, [metric=], [kind=]) | X stringk-nearest-neighbours on an N-row, D-column X and a length-N target y, with integer neighbour count k, an optional metric string (distance function name), and kind ("regression" or "classification", never inferred).y stringk-nearest-neighbours on an N-row, D-column X and a length-N target y, with integer neighbour count k, an optional metric string (distance function name), and kind ("regression" or "classification", never inferred).k stringkind = "classification" predicts the majority label among the k nearest neighbours instead -- always one of the training labels, never an average.metric string, optionalk-nearest-neighbours on an N-row, D-column X and a length-N target y, with integer neighbour count k, an optional metric string (distance function name), and kind ("regression" or "classification", never inferred).kind string, optionalkind = "classification" predicts the majority label among the k nearest neighbours instead -- always one of the training labels, never an average. | result Model | — |
Examples
Written for this form and run by the build -- the output below is what the engine actually produced.
Xkc = [0.0; 1.0; 2.0; 3.0]
ykc = [0, 0, 1, 1]
knc = knn_model(Xkc, ykc, 3, kind = "classification")
q = [1.5] as matrix(1, 1)
print("knn_model classification predict(1.5): {knc.predict(q)}") # majority vote among 3 nearest labels
knn_model classification predict(1.5): [0]
How it works
knn_model(X, y, k, [metric=], [kind=]) — k-nearest-neighbors, a lazy/instance-based method: .fit doesn't compute anything at all, it just stores the training X/y verbatim as model fields; every real bit of work happens later, in .predict, which finds each new row's k nearest training rows and combines their ys. metric= picks how "nearest" is measured: "euclidean" (default, L2) or "manhattan" (L1) — the latter pairs naturally with kmedians_model below, which also builds on L1. kind= follows tree_model's own already- established convention (see that builtin's doc comment) for the genuinely ambiguous "is y categorical or continuous" question, rather than guessing from the values themselves: kind="classification" majority-votes the k neighbors' labels (ties broken in favor of whichever label is seen first scanning nearest-to-farthest, i.e. the closer label wins a tie), kind="regression" (the default, matching tree_model's own default) averages them.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.