nx_knn.nx
buildroot/runtime/nx_knn.nx
about
nx_knn.nx -- k-Nearest-Neighbors classifier.
The cardinal feedback-loras-and-negatives-are-patches-not-systems
names "classifiers" as a load-bearing piece: inputs from
classifiers lead to known outputs with statistical measurement.
This file ships the most bits-up classifier in the literature.
kNN (Cover & Hart 1967):
- No training phase.
- No model weights to load.
- Just a labeled point cloud + a distance metric.
- Given query: find k closest labeled points, return majority
class + confidence.
Why kNN as the FIRST substrate classifier:
1. Bits-up minimum. No FPU dep, no matrix factorisation, no
training optimiser, no hyperparameter tuning beyond k.
2. Theoretical guarantee. Cover & Hart proved kNN error is
bounded by twice the Bayes error rate. No deep-learning
magic.
3. Composes naturally with the closed-loop + Monte Carlo
primitives. Each kNN prediction is a measurable verdict;
confidence becomes the variance input to MC.
4. Pluggable into ANY downstream domain. Age, pose, gender,
style -- all become kNN problems if the caller provides
labeled feature vectors.
Distance metric: Euclidean squared (sum of (a-b)^2 across
features). We avoid sqrt because ranking-by-distance is the
same with or without it; the substrate caller can sqrt if they
want a true distance value via nx_isqrt_q10.
genealogy_id: cover_hart_1967_kNN + fix_hodges_1951_discriminatory_analysis +
duda_hart_2001_pattern_classification
lineage_id: substrate_knn_v1
nx_safety_envelope:
intended_use: "k-Nearest Neighbors classifier (Cover & Hart
1967) -- substrate baseline classifier; also
dependencies 3 imports · 0 importers
imports: nx_syscalls.nxnx_tier.nxnx_loop.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| 86 | struct NxKnnClassifier |
| 98 | struct NxKnnResult |
consts
| 65 | const NX_KNN_OK: nx_int = 0 |
| 66 | const NX_KNN_ERR_BAD_DIMS: nx_int = 1 |
| 67 | const NX_KNN_ERR_BAD_K: nx_int = 2 |
| 68 | const NX_KNN_ERR_AMBIGUOUS: nx_int = 3 // tied majority vote |
| 69 | const NX_KNN_N_VERDICTS: nx_int = 4 |
| 94 | const NX_KNN_BYTES: nx_int = 40 |
| 105 | const NX_KNN_RESULT_BYTES: nx_int = 32 |
functions
| 71 | func nx_knn_verdict_is_valid(v: nx_int) -> nx_int called by 1: main |
| 109 | func _knn_dist_sq(features: *i64, n_features: nx_int, called by 1: _knn_find_k_nearest |
| 139 | func _knn_find_k_nearest(clf: *NxKnnClassifier, query: *i64, k: nx_int, |
| 194 | func _knn_majority_vote(clf: *NxKnnClassifier, k_indices: *i64, k: nx_int, |
| 241 | func nx_knn_classify(clf: *NxKnnClassifier, query: *i64, k: nx_int) -> *NxKnnResult |
| 284 | func main() -> i64 |