code wiki / (root) / sketch_naive_bayes_test.nx

sketch_naive_bayes_test.nx source

↩ module page · 85 lines · 3131 B

1// sketch_naive_bayes_test.nx -- binary Naive Bayes classifier verification. 2 3import "syscalls.nx" 4import "sketch_naive_bayes.nx" 5import "sketch_types.nx" 6 7func main() -> i64 { 8 // ---- alloc ---- 9 let nb: *NaiveBayes = nx_nb_alloc(64, 1, 100) 10 if nb == (0 as *NaiveBayes) { return __syscall(93, 5, 0, 0, 0, 0, 0) } 11 // Reject alpha <= 0. 12 if nx_nb_alloc(64, 0, 100) != (0 as *NaiveBayes) { 13 return __syscall(93, 6, 0, 0, 0, 0, 0) 14 } 15 // Reject vocab_size < 2. 16 if nx_nb_alloc(64, 1, 1) != (0 as *NaiveBayes) { 17 return __syscall(93, 7, 0, 0, 0, 0, 0) 18 } 19 20 // ---- train: spam vs ham classifier ---- 21 // 5 spam emails with features {1, 2, 3} (e.g., {free, viagra, click}) 22 var i: i64 = 0 23 while i < 5 { 24 nx_nb_observe_class(nb, NX_NB_CLASS_POS) // spam = positive 25 nx_nb_observe_feature(nb, NX_NB_CLASS_POS, 1) 26 nx_nb_observe_feature(nb, NX_NB_CLASS_POS, 2) 27 nx_nb_observe_feature(nb, NX_NB_CLASS_POS, 3) 28 i = i + 1 29 } 30 // 5 ham emails with features {4, 5, 6} (e.g., {meeting, regards, attached}) 31 i = 0 32 while i < 5 { 33 nx_nb_observe_class(nb, NX_NB_CLASS_NEG) // ham = negative 34 nx_nb_observe_feature(nb, NX_NB_CLASS_NEG, 4) 35 nx_nb_observe_feature(nb, NX_NB_CLASS_NEG, 5) 36 nx_nb_observe_feature(nb, NX_NB_CLASS_NEG, 6) 37 i = i + 1 38 } 39 if nx_nb_n_total(nb) != 10 { return __syscall(93, 10, 0, 0, 0, 0, 0) } 40 if nx_nb_n_class(nb, NX_NB_CLASS_POS) != 5 { return __syscall(93, 11, 0, 0, 0, 0, 0) } 41 if nx_nb_n_class(nb, NX_NB_CLASS_NEG) != 5 { return __syscall(93, 12, 0, 0, 0, 0, 0) } 42 43 // ---- predict: spam features -> spam ---- 44 let buf: *i64 = sys_mmap(8 * 8) as *i64 45 buf[0] = 1; buf[1] = 2; buf[2] = 3 46 if nx_nb_predict(nb, buf, 3) != NX_NB_CLASS_POS { 47 return __syscall(93, 20, 0, 0, 0, 0, 0) 48 } 49 50 // ---- predict: ham features -> ham ---- 51 buf[0] = 4; buf[1] = 5; buf[2] = 6 52 if nx_nb_predict(nb, buf, 3) != NX_NB_CLASS_NEG { 53 return __syscall(93, 30, 0, 0, 0, 0, 0) 54 } 55 56 // ---- mixed features: hopefully ham wins by 2-1 majority ---- 57 buf[0] = 1; buf[1] = 4; buf[2] = 5 58 // Features 4, 5 are ham; 1 is spam. Ham should win by likelihood vote. 59 if nx_nb_predict(nb, buf, 3) != NX_NB_CLASS_NEG { 60 return __syscall(93, 40, 0, 0, 0, 0, 0) 61 } 62 63 // ---- typed envelope ---- 64 buf[0] = 1; buf[1] = 2 65 let q: *ApproxI64 = nx_nb_query(nb, buf, 2) 66 if q.envelope_kind != NX_ENV_ABS { 67 return __syscall(93, 50, 0, 0, 0, 0, 0) 68 } 69 if q.value != NX_NB_CLASS_POS { 70 return __syscall(93, 51, 0, 0, 0, 0, 0) 71 } 72 if q.maturity != NX_MATURITY_REFERENCE_IMPL { 73 return __syscall(93, 52, 0, 0, 0, 0, 0) 74 } 75 76 // ---- input validation ---- 77 if nx_nb_observe_feature(nb, NX_NB_CLASS_POS, 0) != -1 { 78 return __syscall(93, 60, 0, 0, 0, 0, 0) 79 } 80 if nx_nb_observe_feature(nb, NX_NB_CLASS_POS, -5) != -1 { 81 return __syscall(93, 61, 0, 0, 0, 0, 0) 82 } 83 84 return 0 85}