nx_sketch_naive_bayes.nx
buildroot/runtime/nx_sketch_naive_bayes.nx
about
sketch_naive_bayes.nx -- streaming binary Naive Bayes classifier.
Online supervised-learning primitive. Maintains per-class feature
counts and class priors. Classify in log-space via:
score(class) = log P(class) + Σ_f log P(f | class)
predict = arg max class { score(class) }
where:
P(class) = class_count / total_obs
P(feature|class) = (feature_class_count + alpha) / (class_count + alpha * V)
alpha = 1 for Laplace smoothing (avoids -inf for unseen features)
V = vocabulary size (caller's responsibility to bound)
LOG-SPACE PROBABILITIES (integer):
log_2(p) approximated via bitlen(p) - 1 + linear fractional bits.
Sum-of-logs in PPM.
COMPOSES against sketch_hash_map for sparse feature storage.
USE CASES:
- spam vs ham email classification
- log-line severity (error/warning/info)
- sentiment (positive/negative)
- URL category (safe / suspicious)
API:
train(class, feature_id) // observe (class, feature) pair
predict(features, n_features) -> class label (0 or 1)
score(class, features, n) -> log-prob in PPM
LOSSLESS-LANGUAGE DISCIPLINE:
Prediction is a sealed binary value (0 or 1).
Posterior probability returned in PPM with NX_ENV_REL_STDDEV.
dependencies 3 imports · 0 importers
imports: nx_syscalls.nxnx_sketch_hash_map.nxnx_sketch_types.nx
imported by: nobody (leaf or entry point)
structs
| 49 | struct NaiveBayes |
consts
| 46 | const NX_NB_CLASS_NEG: i64 = 0 |
| 47 | const NX_NB_CLASS_POS: i64 = 1 |
functions
| 64 | func nx_nb_alloc(feature_capacity: i64, alpha: i64, vocab_size: i64) -> *NaiveBayes |
| 90 | func nx_nb_observe_class(nb: *NaiveBayes, cls: i64) -> i64 |
| 96 | func nx_nb_observe_feature(nb: *NaiveBayes, cls: i64, feature_id: i64) -> i64 |
| 113 | func nx_nb_log2_ppm(x: i64) -> i64 |
| 125 | func nx_nb_log_class_prior_ppm(nb: *NaiveBayes, cls: i64) -> i64 |
| 137 | func nx_nb_log_feature_likelihood_ppm(nb: *NaiveBayes, cls: i64, feature_id: i64) -> i64 |
| 155 | func nx_nb_score_ppm(nb: *NaiveBayes, cls: i64, |
| 166 | func nx_nb_predict(nb: *NaiveBayes, features: *i64, n_features: i64) -> i64 |
| 177 | func nx_nb_query(nb: *NaiveBayes, features: *i64, n_features: i64) -> *ApproxI64 |
| 186 | func nx_nb_n_total(nb: *NaiveBayes) -> i64 |
| 190 | func nx_nb_n_class(nb: *NaiveBayes, cls: i64) -> i64 |
| 196 | func nx_nb_memory_bytes(nb: *NaiveBayes) -> i64 calls 1: nx_hmap_memory_bytes |