sketch_naive_bayes.nx
buildroot/runtime/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 · 2 importers
imports: syscalls.nxsketch_hash_map.nxsketch_types.nx
imported by: sketch_naive_bayes_test.nxsketch_naive_bayes_vs_majority_bench.nx
structs
| 43 | struct NaiveBayes { |
consts
| 40 | const NX_NB_CLASS_NEG: i64 = 0 |
| 41 | const NX_NB_CLASS_POS: i64 = 1 |
functions
| 58 | func nx_nb_alloc(feature_capacity: i64, alpha: i64, vocab_size: i64) -> *NaiveBayes { |
| 84 | func nx_nb_observe_class(nb: *NaiveBayes, cls: i64) -> i64 { |
| 90 | func nx_nb_observe_feature(nb: *NaiveBayes, cls: i64, feature_id: i64) -> i64 { |
| 107 | func nx_nb_log2_ppm(x: i64) -> i64 { |
| 119 | func nx_nb_log_class_prior_ppm(nb: *NaiveBayes, cls: i64) -> i64 { |
| 131 | func nx_nb_log_feature_likelihood_ppm(nb: *NaiveBayes, cls: i64, feature_id: i64) -> i64 { |
| 149 | func nx_nb_score_ppm(nb: *NaiveBayes, cls: i64, |
| 160 | func nx_nb_predict(nb: *NaiveBayes, features: *i64, n_features: i64) -> i64 { |
| 171 | func nx_nb_query(nb: *NaiveBayes, features: *i64, n_features: i64) -> *ApproxI64 { |
| 180 | func nx_nb_n_total(nb: *NaiveBayes) -> i64 {
called by 1: main |
| 184 | func nx_nb_n_class(nb: *NaiveBayes, cls: i64) -> i64 {
called by 1: main |
| 190 | func nx_nb_memory_bytes(nb: *NaiveBayes) -> i64 {
calls 1: nx_hmap_memory_bytes |