(defun newNaiveBayes (n tbl &key (verbose t)) (labels ((trainer (tbl cautions egs) (cross-index tbl)) (tester (tbl cautions xindex one) (cons (isa one tbl) (bayes-classify2 one xindex)))) (newN-way n tbl :verbose verbose :trainer #'trainer :tester #'tester))) (defun bayes-classify2 (one x &optional (m 2) (k 1)) (let* ((classes (xindex-classes-all x)) (nclasses (xindex-classes-n x)) (n (xindex-ns x)) (classi (xindex-classi x)) (like most-negative-fixnum) (classification (first classes))) (dolist (class classes) (let* ((prior (/ (+ (f x class) k) (+ n (* k nclasses)))) (tmp (log prior))) (doitems (feature i one) (unless (= classi i) (unless (unknownp feature) (let ((delta (/ (+ (f x class i feature) (* m prior)) (+ (f x class) m)))) (incf tmp (log delta)))))) (when (> tmp like) (setf like tmp classification class)))) classification))