code wiki / (root) / nx_sketch_naive_bayes.nx

nx_sketch_naive_bayes.nx

buildroot/runtime/nx_sketch_naive_bayes.nx

7089 B198 linesdepth 4pulls 4 transitivereach 0 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_sketch_hash_map.nx nx_sketch_types.nx nx_sketch_naive_bayes.nx

imports: nx_syscalls.nxnx_sketch_hash_map.nxnx_sketch_types.nx

imported by: nobody (leaf or entry point)

structs

49struct NaiveBayes

consts

46const NX_NB_CLASS_NEG: i64 = 0
47const NX_NB_CLASS_POS: i64 = 1

functions

64func nx_nb_alloc(feature_capacity: i64, alpha: i64, vocab_size: i64) -> *NaiveBayes
90func nx_nb_observe_class(nb: *NaiveBayes, cls: i64) -> i64
96func nx_nb_observe_feature(nb: *NaiveBayes, cls: i64, feature_id: i64) -> i64
113func nx_nb_log2_ppm(x: i64) -> i64
125func nx_nb_log_class_prior_ppm(nb: *NaiveBayes, cls: i64) -> i64
called by 1: nx_nb_score_ppm calls 1: nx_nb_log2_ppm
137func nx_nb_log_feature_likelihood_ppm(nb: *NaiveBayes, cls: i64, feature_id: i64) -> i64
155func nx_nb_score_ppm(nb: *NaiveBayes, cls: i64,
166func nx_nb_predict(nb: *NaiveBayes, features: *i64, n_features: i64) -> i64
called by 1: nx_nb_query calls 1: nx_nb_score_ppm
177func nx_nb_query(nb: *NaiveBayes, features: *i64, n_features: i64) -> *ApproxI64
186func nx_nb_n_total(nb: *NaiveBayes) -> i64
190func nx_nb_n_class(nb: *NaiveBayes, cls: i64) -> i64
196func nx_nb_memory_bytes(nb: *NaiveBayes) -> i64