-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchisquared.py
More file actions
37 lines (26 loc) · 1.13 KB
/
chisquared.py
File metadata and controls
37 lines (26 loc) · 1.13 KB
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
"""
Chi Squared metric
"""
from metric import Metric
from math import log
class ChiSquared(Metric):
# returns [(attr, metric), (attr, metric), (attr, metric)]
def select(self):
for att in self._freq.keys():
for target in [0,1]:
self._conj[att][target][0] = self._target[target] - self._conj[att][target][1]
metrics = []
for att in self._freq.keys():
metric = 0.0
for target in [0, 1]:
for attVal in [0, 1]:
NEtEc = float(self._conj[att][target][attVal])/self._N
# N x P(t) * P(c)
Pt = self._conj[att][0][attVal] + self._conj[att][1][attVal]
if Pt == 0:
continue
EEtEc = float(Pt * self._target[target])/self._N
#print 'NEtEc: ' + str(NEtEc) + ' EEtEc: ' + str(EEtEc) + ' '+ str(target) + ' ' + str(attVal)
metric += float((NEtEc - EEtEc) * (NEtEc - EEtEc))/EEtEc
metrics.append((att, metric))
return metrics