fzalkow/cl-mlep

Run Bayes Crash

Closed this issue · 1 comments

Harag commented

The following code is crashing run byes

(setf labels '(2 1 1 1 1 1 1 1))

(setf test-set '((99 0 21 98 97 0 0 0 0 96 95 18 94 84 93 14 13 53 92 10 9 8 91 90 89 88 87)))

(setf data '((86 0 21 85 0 0 0 0 0 67 19 18 67 84 83 14 13 63 62 10 9 82 81 80 79 78 3)
(77 0 21 76 0 0 0 0 0 75 19 18 74 65 73 14 13 63 52 10 9 72 71 70 59 69 3)
(68 0 21 56 0 0 0 0 0 67 19 18 66 65 64 14 13 63 62 10 9 8 61 60 59 58 3)
(57 0 21 56 0 0 0 0 0 55 19 18 55 43 54 14 13 53 52 10 9 8 51 50 49 48 3)
(47 0 21 46 0 0 0 0 0 45 19 18 44 43 42 14 13 12 11 10 9 8 41 40 39 38 3)
(37 0 21 20 0 0 0 0 0 36 19 18 35 27 15 14 13 12 34 10 9 8 33 32 31 30 3)
(29 0 21 20 0 0 0 0 0 28 19 18 28 27 15 14 13 12 11 10 9 8 26 25 24 23 3)
(22 0 21 20 0 0 0 0 0 17 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3)))

(setf my-bayes (make-instance 'mlep:naive-bayes
:data-set data
:set-labels labels
:test-set test-set))

If I hack the code like this it removes the error but I dont know if the default value of eish should be 0.

(defmethod run ((instance naive-bayes) &key)
(declare (optimize (debug 3)))
(with-slots (data-set set-labels test-set all-labels prior-probabilities likelihoods) instance
(when (/= (length data-set) (length set-labels))
(error "Each item in data-set' has to have a label inset-labels'. (At the moment they are of unequal length.)"))
(let ((result))
(dolist (item test-set (nreverse result))
(let ((probabilities))
(dolist (label all-labels)
(let ((tmp-prob (rest (find label prior-probabilities :key #'first))))
(dotimes (i (length item))
(let ((eish (cddar (member-if #'(lambda (lik)
(and (eql (first lik)
label)
(equal (second lik)
(cons i (nth i item)))))
likelihoods))))
(setf tmp-prob (* tmp-prob (if eish eish 0)))))
(push tmp-prob probabilities)))
(push (nth (max-arg (nreverse probabilities)) all-labels) result))))))

Thanks alot for finding this bug and for posting your fix. The value of 0 for eish is correct! I also added a check: when all probs are 0, then it returned NIL instead of a traget number. So be prepared to receive NIL as an estimated target if there are no examples in the training data. 3fda0a3