-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_featmultinomial.py
170 lines (139 loc) · 5.56 KB
/
test_featmultinomial.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
"""
Tests for the modified MultinonialNB that checks if it is correctly trained
using labeled features.
"""
import unittest
import numpy as np
from featmultinomial import FeatMultinomalNB
from copy import deepcopy
from math import log
from sklearn import tree
# I should use random numbers here!
X = np.array([
[2, 0, 10, 3],
[5, 0, 1, 0],
[0, 8, 3, 7]
])
Y = np.array([0, 0, 1])
features = np.array([
[1, 1, 1.5, 1],
[1, 1.5, 1, 1]
])
"""
I = [[1,0,1,1],
[1,0,1,0],
[0,1,1,1]]
P(I0=1, c0) = #instances with feat 0 and class 0 / # instances = 2/3
P(I1=1, c0) = 0/3 = 0
P(I2=1, c0) = 2/3
P(I3=1, c0) = 1/3
P(I0=1, c1) = #instances with feat 0 and class 1 / # instances = 0/3 = 0
P(I1=1, c1) = 1/3
P(I2=1, c1) = 1/3
P(I3=1, c1) = 1/3
P(I0=0, c0) = #instances without feat 0 and class 0 / # instances = 0/3 = 0
P(I1=0, c0) = 2/3
P(I2=0, c0) = 0/3
P(I3=0, c0) = 1/3
P(I0=0, c1) = #instances with feat 0 and class 1 / # instances = 1/3
P(I1=0, c1) = 0/3
P(I2=0, c1) = 0/3
P(I3=0, c1) = 0/3
P(I0=1) = 2/3 P(I0=0) = 1/3
P(I1=1) = 1/3 P(I1=0) = 2/3
P(I2=1) = 1 P(I2=0) = 0
P(I3=1) = 2/3 P(I3=0) = 1/3
P(c0) = 2/3
P(c1) = 1/3
IG(f0) = (P(I0=1, c0) * log(P(I0=1, c0) / (P(I0=1) * P(c0)) ) ) +
(P(I0=1, c1) * log(P(I0=1, c1) / (P(I0=1) * P(c1)) ) ) +
(P(I0=0, c0) * log(P(I0=0, c0) / (P(I0=0) * P(c0)) ) ) +
(P(I0=0, c1) * log(P(I0=0, c1) / (P(I0=0) * P(c1)) ) )
= (2.0/3.0 * log(2.0/3.0 / (2.0/3.0 * 2.0/3.0) ) ) +
(0 * log(0 / (2.0/3.0 * 1/3.0) ) ) +
(0 * log(0 / (1/3.0 * 2.0/3.0) ) ) +
(1/3.0 * log(1/3.0 / (1/3.0 * 1/3.0) ) )
= 0.27031 + 0 + 0 + 0.3662 = 0.63651
IG(f1) = (0 * log(0 / (1/3.0 * 2/3.0) ) ) +
(1.0/3.0 * log(1.0/3.0 / (1.0/3.0 * 1.0/3.0) ) ) +
(2/3.0 * log(2/3.0 / (2.0/3.0 * 2/3.0) ) ) +
(0 * log(1.0/3.0 / (2.0/3.0 * 1.0/3.0) ) )
= 0 + 0.3662 + 0.27031 + 0.13515 = 0.7716
IG(f2) = (2/3.0 * log(2/3.0 / (1 * 2/3.0) ) ) +
(1/3.0 * log(1/3.0 / (1 * 1/3.0) ) ) +
(0 * log(0 / (0 * 2/3.0) ) ) +
(0 * log(0 / (0 * 1/3.0) ) )
= 0 + 0 + 0 + 0 = 0
IG(f3) = (1/3.0 * log(1/3.0 / (2/3.0 * 2/3.0) ) ) +
(1/3.0 * log(1/3.0 / (2/3.0 * 1/3.0) ) ) +
(1/3.0 * log(1/3.0 / (1/3.0 * 2/3.0) ) ) +
(0 * log(0 / (1/3.0 * 1/3.0) ) )
= -0.09589402415059363 + 0.135115 + 0.135115 + 0 = 0.17433
"""
ig_correct_anwers = [0.636514, 0.636514, 0.0, 0.17441]
class TestFeatMultinomialNB(unittest.TestCase):
def setUp(self):
self.fmnb = FeatMultinomalNB()
self.fmnb.fit(X, Y)
def test_fit(self):
no_feat_prior = deepcopy(self.fmnb.feature_log_prob_)
self.fmnb.fit(X, Y, features=features)
feat_prior = self.fmnb.feature_log_prob_
self.assertNotEqual(no_feat_prior[0][2], feat_prior[0][2])
self.assertTrue(np.all(self.fmnb.alpha == features))
def test_information_gain(self):
ig = self.fmnb.feat_information_gain
self.assertEqual(ig.shape[0], X.shape[1])
for i, answer in enumerate(ig):
self.assertAlmostEqual(answer, ig_correct_anwers[i], places=4)
self.assertTrue(np.all(ig.argsort() == [2, 3, 0, 1]))
def test_instance_proba(self):
"""
P(f0) = 0.4*0.25 + 0.6*0.75 = 0.55
P(f1) = 0.2*0.25 + 0.8*0.75 = 0.65
P(f2) = 0.5*0.25 + 0.5*0.75 = 0.5
P(I0) = 0.55**0 * 0.65**1 * 0.5**3 = 0.08125
P(I1) = 0.55**2 * 0.65**0 * 0.5**5 = 0.0094
P(I2) = 0.55**3 * 0.65**0 * 0.5**1 = 0.083
P(I3) = 0.55**0 * 0.65**4 * 0.5**0 = 0.1785
"""
self.fmnb.feature_log_prob_ = np.log(np.array([[0.4, 0.2, 0.5],
[0.6, 0.8, 0.5]]))
self.fmnb.class_log_prior_ = np.log(np.array([0.25, 0.75]))
instances = np.array([[0, 1, 3],
[2, 0, 5],
[3, 0, 1],
[0, 4, 0]])
result = self.fmnb.instance_proba(instances)
expected = np.array([0.08125, 0.0094, 0.083, 0.1785])
self.assertEqual(result.shape, (4,))
np.testing.assert_array_almost_equal(result, expected, decimal=3)
class TestIGwithDecisionTree(unittest.TestCase):
def setUp(self):
self.fmnb = FeatMultinomalNB()
self.dtree = tree.DecisionTreeClassifier(criterion='entropy',
min_samples_split=1,
min_samples_leaf=1)
def tearDown(self):
self.assertTrue(np.all(self.fmnb.feat_information_gain.argsort() ==
self.dtree.feature_importances_.argsort()))
def test_ig_with_iris(self):
from sklearn.datasets import load_iris
iris = load_iris()
self.fmnb.fit((iris.data > 3), iris.target)
self.dtree.fit((iris.data > 3), iris.target)
def test_ig_with_bag_of_words(self):
from sklearn.feature_extraction.text import CountVectorizer
corpus = ['This is a corpus and the main',
'objective is to have senteces to simulate a sparse',
'matrix of features, on the opposite of the iris corpus',
'that has few features and all the features are present in all',
'the instances.',
'By the way, this all are documents.']
target = [1, 2, 3, 2, 1, 2]
vectorizer = CountVectorizer(min_df=1)
X = vectorizer.fit_transform(corpus)
self.fmnb.fit(X, target)
self.dtree.fit(X.todense(), target)
if __name__ == '__main__':
unittest.main()