forked from phonedude/cs532-s17
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassify.py
115 lines (96 loc) · 3.06 KB
/
classify.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
import docclass
import feedfilter
import os
def findProb(data, cl):
'''
Use predefined fisher methods for these probabilities
'''
for item in data:
item['Cprob'] = cl.cprob(item['Title'], item["Actual"])
item['fisherprob'] = cl.fisherprob(item['Title'], item["Actual"])
print(item)
return data
def calcMeasurements(cats, data, trainCount):
'''
Method to calculate fmeasure, precision, recall
TP = True Positive
FP = False Positive
FN = False Negative
'''
catDict = {}
for cat in cats:
TP, FP, FN = 0.0, 0.0, 0.0
for i, item in enumerate(data):
if item['Actual'] != cat:
continue
if not item['Guess']:
FN += 1.0
elif(item['Actual'] == item['Guess']):
TP += 1.0
elif(item['Actual'] != item['Guess']):
FP += 1.0
print(item['Title'], item['Guess'], item['Actual'], "asdasd")
prec = TP / (TP + FP)
recall = 0.0
if (TP + FN) != 0.0:
recall = TP / (TP + FN)
f1 = 0.0
if (prec + recall) != 0.0:
f1 = 2 * (prec * recall) / (prec + recall)
print(cat)
print(prec)
print(recall)
print(f1)
catDict[cat] = {'prec': prec, 'recall': recall, 'f1': f1}
return catDict
def printTabular(cats):
outStr = "\hline\n"
print(cats)
for key, val in cats.items():
outStr += key + " & " + str(val['prec']) + " & " + \
str(val['recall']) + " & " + str(val['f1']) + " \\\\ \n"
return outStr
def tabularPredictions(data, trainCount):
outStr = "\hline\n"
for i, item in enumerate(data):
if(i == trainCount):
outStr += "% TEST SET STARTS HERE\n"
outStr += item["Title"] + " & " + item["Actual"] + \
" & " + item["Guess"] + " \\\\ \n"
return outStr
def resultPred(data):
'''
Get set of categories and list of predictions
'''
cats = set()
predicted = []
for i in data:
cats.add(i["Actual"])
predicted.append(i["Guess"])
return cats, predicted
def removeDbs():
'''
Clean databases for each run
'''
db1 = "data/first90-trained.db"
db2 = "data/first50-trained.db"
if(os.path.isfile(db1)):
os.remove(db1)
if(os.path.isfile(db2)):
os.remove(db2)
if __name__ == "__main__":
removeDbs()
trainCount = 90
cl = docclass.fisherclassifier(docclass.getwords)
cl.setdb("data/first" + str(trainCount) + "-trained.db")
data = feedfilter.read("data/feed.xml", cl, trainCount)
data = findProb(data, cl)
cats, predicted = resultPred(data)
catDict = calcMeasurements(cats, data, trainCount)
# print to files. should only be 1 time run
# with open("../docs/" + str(trainCount) + "measureTabular.tex", 'w') as f:
# outStr = printTabular(catDict)
# f.write(outStr)
# with open("../docs/" + str(trainCount) + "trained.tex", 'w') as f:
# outStr = tabularPredictions(data, trainCount)
# f.write(outStr)