code wiki / (root) / nx_knn.nx

nx_knn.nx

buildroot/runtime/nx_knn.nx

12432 B346 linesdepth 4pulls 4 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_tier.nx nx_loop.nx nx_knn.nx

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

main sys_mmap nx_knn_classify sys_mmap ↻ _knn_find_k_nearest _knn_dist_sq _knn_majority_vote sys_mmap ↻ nx_knn_verdict_is_valid

structs

86struct NxKnnClassifier
98struct NxKnnResult

consts

65const NX_KNN_OK: nx_int = 0
66const NX_KNN_ERR_BAD_DIMS: nx_int = 1
67const NX_KNN_ERR_BAD_K: nx_int = 2
68const NX_KNN_ERR_AMBIGUOUS: nx_int = 3 // tied majority vote
69const NX_KNN_N_VERDICTS: nx_int = 4
94const NX_KNN_BYTES: nx_int = 40
105const NX_KNN_RESULT_BYTES: nx_int = 32

functions

71func nx_knn_verdict_is_valid(v: nx_int) -> nx_int
called by 1: main
109func _knn_dist_sq(features: *i64, n_features: nx_int,
called by 1: _knn_find_k_nearest
139func _knn_find_k_nearest(clf: *NxKnnClassifier, query: *i64, k: nx_int,
called by 1: nx_knn_classify calls 1: _knn_dist_sq
194func _knn_majority_vote(clf: *NxKnnClassifier, k_indices: *i64, k: nx_int,
called by 1: nx_knn_classify calls 1: sys_mmap
241func nx_knn_classify(clf: *NxKnnClassifier, query: *i64, k: nx_int) -> *NxKnnResult
284func main() -> i64