code wiki / (root) / sketch_naive_bayes.nx

sketch_naive_bayes.nx

buildroot/runtime/sketch_naive_bayes.nx

7021 B192 linesdepth 5pulls 5 transitivereach 2 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 · 2 importers

syscalls.nx sketch_hash_map.nx sketch_types.nx sketch_naive_bayes.nx sketch_naive_bayes_test.nx sketch_naive_bayes_vs_majority_ben

imports: syscalls.nxsketch_hash_map.nxsketch_types.nx

imported by: sketch_naive_bayes_test.nxsketch_naive_bayes_vs_majority_bench.nx

structs

43struct NaiveBayes {

consts

40const NX_NB_CLASS_NEG: i64 = 0
41const NX_NB_CLASS_POS: i64 = 1

functions

58func nx_nb_alloc(feature_capacity: i64, alpha: i64, vocab_size: i64) -> *NaiveBayes {
called by 2: mainmain calls 1: nx_hmap_alloc
84func nx_nb_observe_class(nb: *NaiveBayes, cls: i64) -> i64 {
called by 2: mainmain
90func nx_nb_observe_feature(nb: *NaiveBayes, cls: i64, feature_id: i64) -> i64 {
called by 2: mainmain calls 2: nx_hmap_getnx_hmap_put
107func nx_nb_log2_ppm(x: i64) -> i64 {
119func nx_nb_log_class_prior_ppm(nb: *NaiveBayes, cls: i64) -> i64 {
called by 1: nx_nb_score_ppm calls 1: nx_nb_log2_ppm
131func nx_nb_log_feature_likelihood_ppm(nb: *NaiveBayes, cls: i64, feature_id: i64) -> i64 {
149func nx_nb_score_ppm(nb: *NaiveBayes, cls: i64,
160func nx_nb_predict(nb: *NaiveBayes, features: *i64, n_features: i64) -> i64 {
called by 3: nx_nb_querymainmain calls 1: nx_nb_score_ppm
171func nx_nb_query(nb: *NaiveBayes, features: *i64, n_features: i64) -> *ApproxI64 {
called by 1: main calls 2: nx_nb_predictnx_approx_new
180func nx_nb_n_total(nb: *NaiveBayes) -> i64 {
called by 1: main
184func nx_nb_n_class(nb: *NaiveBayes, cls: i64) -> i64 {
called by 1: main
190func nx_nb_memory_bytes(nb: *NaiveBayes) -> i64 {