-
Notifications
You must be signed in to change notification settings - Fork 1
/
exampleSNBC.py
41 lines (32 loc) · 1.11 KB
/
exampleSNBC.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
"""
This code belongs to the Probabilistic Graphical Models Python Library (PGM_PyLib)
PGM_PyLib: https://github.com/jona2510/PGM_PyLib
Check the "PGM_PyLib Manual vX.X.pdf" to see how the code works.
The PGM_PyLib is distributed under the GNU public license v3.0.
Code author: Jonathan Serrano-Pérez
"""
import numpy as np
import PGM_PyLib.semiNaive as sn
np.random.seed(0) # it is not necessary
# three classes
# 5 attributes
# 100 instances for training
data_train = np.random.randint(0,5,size=(100,5)).astype(str)
cl_train = np.random.randint(0,3,size=100)
# 50 instances for testing
data_test = np.random.randint(0,5,size=(50,5)).astype(str)
cl_test = np.random.randint(0,3,size=50)
# create the classifiers
c = sn.semiNaive(validation=0.8, epsilon=0.01, omega=0.01, smooth=0.1, nameAtts="auto", usePrior=True)
# train the classifier
c.fit(data_train, cl_train)
# predict
p = c.predict(data_test)
# evaluation
print(c.exactMatch(cl_test, p))
# ignore the Prior probabilities
c.NBC.usePrior = False
p = c.predict(data_test)
print(c.exactMatch(cl_test,p))
# show the operations that were applied
print(c.opeNameAtts)