-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathnbayes.cl
179 lines (157 loc) · 4.5 KB
/
nbayes.cl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
(defpackage :nbayes
(:use :cl
:hjs.learn.read-data)
(:export
:mbnb-learn
:make-mbnb-learner
:mnb-learn
:make-mnb-learner)
)
(in-package nbayes)
;;to prevent underflow
(defun exp% (x)
(if (< -700.0d0 x)
(exp x)
0.0d0))
;;Multivariate Bernoulli Naive Bayes
;;alpha is a smoothing parameter
;;we assume that final colum is the class label
(defun mbnb-learn (training-vector &key (alpha 1.0d0));;when alpha = 0.0, it occurs log 0.0 error
(let* ((l (1- (length (aref training-vector 0))))
(classes (loop
for v across training-vector
collect (aref v l) into cs
finally (return (sort (remove-duplicates cs :test #'equal) #'string<))))
(k (length classes))
(n-wc (make-array k :initial-contents
(loop
repeat k
collect (make-array l))))
(p-wc (make-array k :initial-contents
(loop
repeat k
collect (make-array l :element-type 'double-float :initial-element 0.0d0))))
(n-c (make-array k)))
(loop
for i below k
as category = (nth i classes)
do (setf (aref n-c i)
(loop
for v across training-vector
as class = (aref v l)
if (string= category class)
count v)))
(loop
for i below k
as category = (nth i classes)
do (loop
for j below l
do (setf (aref (aref n-wc i) j)
(loop
for v across training-vector
as class = (aref v l)
if (and (string= category class)
(/= 0 (aref v j)))
count v))))
(loop
for i below k
do (loop
for j below l
do (setf (aref (aref p-wc i) j)
(/ (+ (aref (aref n-wc i) j) alpha)
(+ (aref n-c i) (* 2 alpha))))))
(list p-wc classes)))
(defun make-mbnb-learner (p-wc classes)
#'(lambda (fv)
(let ((results
(loop
with n = (1- (length fv))
for i below (length classes)
collect (loop
for p across (aref p-wc i)
for j below n
as q = (if (= 0 (aref fv j))
0.0d0
1.0d0)
sum (log (+ (* p q)
(* (- 1.0d0 p) (- 1.0d0 q))))))))
(nth (position (loop for p in results maximize p) results) classes))))
;;Multinomial Naive Bayes
;;alpha is a smoothing parameter
;;we assume that final colum is the class label
(defun mnb-learn (training-vector &key (alpha 1.0d0));;when alpha = 0.0, it ocuurs log 0.0 error
(let* ((l (1- (length (aref training-vector 0))))
(classes (loop
for v across training-vector
collect (aref v l) into cs
finally (return (sort (remove-duplicates cs :test #'equal) #'string<))))
(k (length classes))
(n-wc (make-array k :initial-contents
(loop
repeat k
collect (make-array l))))
(q-wc (make-array k :initial-contents
(loop
repeat k
collect (make-array l :element-type 'double-float :initial-element 0.0d0)))))
(loop
for i below k
as category = (nth i classes)
as n-wci = (aref n-wc i)
do (loop
for j below l
do (setf (aref n-wci j)
(loop
for v across training-vector
as class = (aref v l)
if (string= category class)
sum (aref v j)))))
(loop
for i below k
as q-wci = (aref q-wc i)
as n-wci = (aref n-wc i)
do (loop
for j below l
do (setf (aref q-wci j)
(/ (+ (aref n-wci j) alpha)
(+ (loop
for n across n-wci
sum n)
(* l alpha))))))
(list q-wc classes)))
(defun make-mnb-learner (q-wc classes)
#'(lambda (fv)
(let ((results
(loop
for i below (length classes)
collect (loop
for q across (aref q-wc i)
for n-wd across fv
sum (* n-wd (log q))))))
(nth (position (loop for p in results maximize p) results) classes))))
;;return the alist of validation results like '(((predict-class. true-class) n)..)
(defun learner-validation (learner test-vector)
(let* ((n (length test-vector))
(l (1- (length (aref test-vector 0))))
(sum-up-list
(sum-up (loop
for i below n
collect (cons (funcall learner (aref test-vector i))
(aref (aref test-vector i) l))))))
(values sum-up-list (accuracy sum-up-list))))
(defun accuracy (sum-up-list)
(loop
for obj in sum-up-list
as category = (first obj)
sum (cdr obj) into m
if (equal (car category) (cdr category))
sum (cdr obj) into n
finally (return (* 100.0d0 (/ n m)))))
(defun sum-up (lst)
(loop with alist
for obj in lst
as sub-alist = (assoc obj alist :test #'equal)
do (if sub-alist
(incf (cdr sub-alist))
(push (cons obj 1) alist))
finally (return alist)))