-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gaussian classiers.py
299 lines (186 loc) · 7.86 KB
/
Gaussian classiers.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# -*- coding: utf-8 -*-
"""
Created on Mon May 16 19:26:12 2022
@author: gabrj
"""
#Gaussian models
#In the first part of this laboratory we will solve the IRIS
#classication task using Gaussian classiers
import sklearn.datasets
import numpy
import scipy.special
import matplotlib.pyplot as plt
def load_iris():
D, L = sklearn.datasets.load_iris()['data'].T, sklearn.datasets.load_iris()['target']
return D, L
#Splitting dataset in training e evaluation part
def split_db_2to1(D, L, seed=0):
nTrain = int(D.shape[1]*2.0/3.0)
numpy.random.seed(seed)
idx = numpy.random.permutation(D.shape[1])
idxTrain = idx[0:nTrain]
idxTest = idx[nTrain:]
#training
DTR = D[:, idxTrain]
LTR = L[idxTrain]
#evaluation test
LTE = L[idxTest]
DTE = D[:, idxTest]
print(DTR.shape)
print(LTR.shape)
print(DTE.shape)
print(LTE.shape)
return (DTR, LTR), (DTE, LTE)
def vcol(v):
return v.reshape((v.size, 1))
def vrow(v):
return v.reshape((1,v.size))
def covariance_and_mean(D):
mu=vcol(D.mean(1))
C=numpy.dot(D-mu,(D-mu).T)/float(D.shape[1])
return [C,mu]
def logpdf_GAU_ND(X, mu, C):
P=numpy.linalg.inv(C)
return -0.5*X.shape[0]*numpy.log(numpy.pi*2)+\
0.5*numpy.linalg.slogdet(P)[1] - 0.5 *\
(numpy.dot(P,(X-mu))* (X-mu)).sum(0)
def Multivariate_Gaussian_Classifer(h,DTrain, LTrain, DTest, LTest, stamp):
#Calculate class posterior probability in 3 step.
#1-Calculate loglikelihood (Classic no log) for test sample
#2-Store in a matrix S[i,j] che è la class condition probability
#per il campione j data la classe i
SJoint=numpy.zeros((3,DTest.shape[1]))
logSJoint=numpy.zeros((3,DTest.shape[1]))
classPriors=[1.0/3.0, 1.0/3.0, 1.0/3.0]
for label in [0,1,2]:
mu,C = h[label]
SJoint[label,:]=numpy.exp(logpdf_GAU_ND(DTest, mu, C).ravel()) * classPriors[label]
logSJoint[label,:]=logpdf_GAU_ND(DTest, mu, C).ravel() + numpy.log(classPriors[label])
SMarginal=SJoint.sum(axis=0)
logSMarginal=scipy.special.logsumexp(logSJoint,axis=0)
Post1=SJoint / vrow(SMarginal)
logPost=logSJoint-vrow(logSMarginal)
Post2=numpy.exp(logPost)
#Trovo la probabilità a posteriori maggiore per i campioni
LPred1=Post1.argmax(axis=0)
LPred2=Post2.argmax(axis=0) #for logarithmic
res=(LPred1==LTest)
relerror=((numpy.abs(Post2-Post1))/Post1).max()
accuracy=(LPred1==LTest).sum()*100/LTest.size
if(stamp==1):
print("\n\n***********************************\n* Multivariate Gaussian Classifer *\n***********************************\n\n")
print("Result of assumptions => \n\n",res)
print("\nRelError between log and classic =",relerror)
print("Accuracy=",accuracy,"%")
return accuracy
def Naive_Bayes_Gaussian_Classifer(h,DTrain, LTrain, DTest, LTest, stamp):
#Very similar to SVG but i take only the diagonal of covariance matrix
SJoint=numpy.zeros((3,DTest.shape[1]))
classPriors=[1.0/3.0, 1.0/3.0, 1.0/3.0]
for label in [0,1,2]:
mu,C = h[label]
C=C*numpy.identity(C.shape[0])
SJoint[label,:]=numpy.exp(logpdf_GAU_ND(DTest, mu, C).ravel()) * classPriors[label]
SMarginal=SJoint.sum(axis=0)
Post=SJoint / vrow(SMarginal)
#Trovo la probabilità a posteriori maggiore per i campioni
LPred=Post.argmax(axis=0)
res=(LPred==LTest)
accuracy=(LPred==LTest).sum()*100/LTest.size
if(stamp==1):
print("\n\n***********************************\n*Naive Bayes Gaussian Classifiers *\n***********************************\n\n")
print("Result of assumptions => \n\n",res)
print("Accuracy=",accuracy,"%")
return accuracy
def Tied_Covariance_Gaussian_Classifer(h, DTrain, LTrain, DTest, LTest, stamp):
Tied=0;
for label in [0,1,2]:
mu,C = h[label]
Di=DTrain[:,LTrain==label]
Tied+=Di.shape[1]*C;
Tied=Tied/DTrain.shape[1]
Tied=Tied*numpy.identity(Tied.shape[0])
SJoint=numpy.zeros((3,DTest.shape[1]))
classPriors=[1.0/3.0, 1.0/3.0, 1.0/3.0]
for label in [0,1,2]:
mu,C = h[label]
SJoint[label,:]=numpy.exp(logpdf_GAU_ND(DTest, mu, Tied).ravel()) * classPriors[label]
SMarginal=SJoint.sum(axis=0)
Post=SJoint / vrow(SMarginal)
LPred=Post.argmax(axis=0)
res=(LPred==LTest)
accuracy=(LPred==LTest).sum()*100/LTest.size
if(stamp==1):
print("\n\n***************************************\n*Tied Covariance Gaussian Classifiers *\n***************************************\n\n")
print("Result of assumptions => \n\n",res)
print("Accuracy=",accuracy,"%")
return accuracy
def Tied_Naive_Bayes(h, DTrain, LTrain, DTest, LTest, stamp):
Tied=0;
for label in [0,1,2]:
mu,C = h[label]
Di=DTrain[:,LTrain==label]
Tied+=Di.shape[1]*C;
Tied=Tied/DTrain.shape[1]
SJoint=numpy.zeros((3,DTest.shape[1]))
classPriors=[1.0/3.0, 1.0/3.0, 1.0/3.0]
for label in [0,1,2]:
mu,C = h[label]
SJoint[label,:]=numpy.exp(logpdf_GAU_ND(DTest, mu, Tied).ravel()) * classPriors[label]
SMarginal=SJoint.sum(axis=0)
Post=SJoint / vrow(SMarginal)
LPred=Post.argmax(axis=0)
res=(LPred==LTest)
accuracy=(LPred==LTest).sum()*100/LTest.size
if(stamp==1):
print("\n\n*******************\n*Tied Naive Bayes *\n*******************\n\n")
print("Result of assumptions => \n\n",res)
print("Accuracy=",accuracy,"%")
return accuracy
def Leave_one_out(D, L, Model):
print("\n\n\n\n****************\n*Leave One Out *\n****************\n\n")
print(Model.__name__)
h={}
GlobalAcc=0;
for i in range(D.shape[1]):
#training
DTR = numpy.hstack((D[:,0:i],D[:,i+1:]))
LTR=numpy.hstack((L[0:i], L[i+1:]))
#evaluation test
LTE = L[i:i+1]
DTE = D[:,i:i+1]
for label in [0,1,2]:
C,mu =covariance_and_mean(DTR[:,LTR==label])
h[label]=(mu,C)
GlobalAcc+=Model(h, DTR, LTR, DTE, LTE, 0)
GlobalAcc=GlobalAcc/D.shape[1]
print("Global Accuracy= ",GlobalAcc, "%")
return GlobalAcc
if __name__ == '__main__':
D,L=load_iris()
(DTrain, LTrain), (DTest, LTest) = split_db_2to1(D, L)
#Calcolo di media empirica e covarianza per ogni classe
h={}
for label in [0,1,2]:
C,mu =covariance_and_mean(DTrain[:,LTrain==label])
h[label]=(mu,C)
#***********************************
#* Multivariate Gaussian Classifer *
#***********************************
Multivariate_Gaussian_Classifer(h,DTrain, LTrain, DTest, LTest,1)
#**********************************
#* Naive Bayes Gaussian Classifer *
#**********************************
Naive_Bayes_Gaussian_Classifer(h,DTrain, LTrain, DTest, LTest,1)
#**************************************
#* Tied Covariance Gaussian Classifer *
#**************************************
Tied_Covariance_Gaussian_Classifer(h,DTrain, LTrain, DTest, LTest,1)
#********************
#* Tied Naive Bayes *
#********************
Tied_Naive_Bayes(h,DTrain, LTrain, DTest, LTest,1)
Leave_one_out(D,L,Multivariate_Gaussian_Classifer)
Leave_one_out(D,L,Naive_Bayes_Gaussian_Classifer)
Leave_one_out(D,L,Tied_Covariance_Gaussian_Classifer)
Leave_one_out(D,L,Tied_Naive_Bayes)