-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetric.py
71 lines (55 loc) · 2.21 KB
/
metric.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
import thread
class Metric(object):
def combine(self, other):
# this should be isolated in dataSet
for key in set(self._freq.keys() + other._freq.keys()):
if key not in self._freq:
self._freq[key] = [0.0,0.0]
for value in [0, 1]:
self._freq[key][value] += other._freq.get(key, [0, 0])[value]
if key not in self._conj:
self._conj[key] = [[0.0, 0.0], [0.0, 0.0]]
for value in [0, 1]:
for target in [0, 1]:
#[att_name][target_value][att_value]
self._conj[key][target][value] += other._conj.get(key, [[0.0, 0.0], [0.0, 0.0]])[target][value]
self._N += other._N
for target in [0, 1]:
self._target[target] += other._target[target]
return self
def _readFile(self, fname, event, id, threadNum):
with open(fname, 'r') as f:
cnt = 0
while True:
line = f.readline()
if not line: break
if len(line) < 2: continue
if cnt % threadNum == id:
self.process(line)
cnt += 1
print 'Thread ' + str(id) + ' finished \n'
event.set()
def __init__(self): # pylint: disable=E1002
#dictionary like representation of lines
self._freq = {}
self._conj = {}
self._target = [0.0, 0.0]
self._N = 0.0
def process(self, lineStr):
#one more sample
self._N += 1
#count target class
splitted = lineStr.split()
target = int(splitted[0])
self._target[target] += 1
# this should be isolated in dataSet
for attVal in splitted[1:len(splitted)]:
value = int(attVal.split(':')[1])
name = attVal.split(':')[0]
if name not in self._freq:
self._freq[name] = [0.0, 0.0]
self._freq[name][value] += 1
if name not in self._conj:
self._conj[name] = [[0.0, 0.0], [0.0, 0.0]]
#[att_name][target_value][att_value]
self._conj[name][target][value] += 1