-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocclass.py
More file actions
209 lines (169 loc) · 6.2 KB
/
Copy pathdocclass.py
File metadata and controls
209 lines (169 loc) · 6.2 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import re
import math
def getwords(doc):
splitter = re.compile('\\W*')
# Split the words by non-alpha characters
words = [s.lower() for s in splitter.split(doc)
if len(s)>2 and len(s)<20]
# Return the unique set of words only
return dict([(w,1) for w in words])
def sampletrain(cl):
cl.train('Nobody owns the water.','good')
cl.train('the quick rabbit jumps fences','good')
cl.train('buy pharmaceuticals now','bad')
cl.train('make quick money at the online casino','bad')
cl.train('the quick brown fox jumps','good')
class classifier:
def __init__(self,getfeatures,filename=None):
# Counts of feature/category combinations
self.fc = {}
# Counts of documents in each category
self.cc = {}
self.getfeatures=getfeatures
# Increase the count of a feature/category pair
def incf(self,f,cat):
self.fc.setdefault(f,{})
self.fc[f].setdefault(cat,0)
self.fc[f][cat] += 1
# Increase the count of a category
def incc(self,cat):
self.cc.setdefault(cat,0)
self.cc[cat]+=1
# The number of times a feature has appeared in a category
def fcount(self,f,cat):
if f in self.fc and cat in self.fc[f]:
return float(self.fc[f][cat])
return 0.0
# The number of items in a category
def catcount(self,cat):
if cat in self.cc:
return float(self.cc[cat])
return 0
# The total number of items
def totalcount(self):
return sum(self.cc.values())
# The list of all categories
def categories(self):
return self.cc.keys()
def train(self,item,cat):
# type: (object, object) -> object
features=self.getfeatures(item)
# Increment the count for every feature with this category
for f in features:
self.incf(f,cat)
# Increment the count for this category
self.incc(cat)
def fprob(self,f,cat, default_prob=0.01):
if self.catcount(cat) == 0:
return 0
if self.fcount(f, cat) == 0:
return default_prob
# The total number of times this feature appeared in this
# category divided by the total number of items in this category
return self.fcount(f,cat)/self.catcount(cat)
def weightedprob(self,f,cat,prf,weight=1.0,ap=0.5):
# Calculate current probability
basicprob = prf(f,cat)
# Count the number of times this feature has appeared in
# all categories
totals = sum([self.fcount(f,c) for c in self.categories()])
# Calculate the weighted average
bp = ((weight * ap) + (totals * basicprob)) / (weight + totals)
return bp
class naivebayes(classifier):
def __init__(self,getfeatures):
classifier.__init__(self,getfeatures)
self.thresholds = {}
def setthreshold(self,cat,t):
self.thresholds[cat] = t
def getthreshold(self,cat):
if cat not in self.thresholds:
return 1.0
return self.thresholds[cat]
def classify(self, item, default=None):
# Find the category with the highest probability
max = 0
for cat in self.categories():
cat_prob = self.prob(item, cat)
# print cat, probs[cat]
if cat_prob >= max:
max = cat_prob
best = cat
return best
def classify_with_thresholds(self,item,default=None):
probs={}
# Find the category with the highest probability
max=0.0
for cat in self.categories():
probs[cat]=self.prob(item,cat)
#print cat, probs[cat]
if probs[cat]>max:
max = probs[cat]
best = cat
# Make sure the probability exceeds threshold*next best
for cat in probs:
if cat == best:
continue
if probs[cat]*self.getthreshold(best)>probs[best]:
return default
return best
def docprob(self,item,cat):
features = self.getfeatures(item)
# Multiply the probabilities of all the features together
p = 1
for f in features:
p *= self.fprob(f, cat)
return p
def prob(self,item,cat):
catprob = self.catcount(cat) / self.totalcount()
docprob = self.docprob(item, cat)
return docprob * catprob
class fisherclassifier(classifier):
def cprob(self,f,cat):
# The frequency of this feature in this category
clf = self.fprob(f,cat)
if clf == 0:
return 0
# The frequency of this feature in all the categories
freqsum = sum([self.fprob(f,c) for c in self.categories()])
# The probability is the frequency in this category divided by
# the overall frequency
p = clf/(freqsum)
return p
def __init__(self,getfeatures):
classifier.__init__(self,getfeatures)
self.minimums={}
def setminimum(self,cat,min):
self.minimums[cat]=min
def getminimum(self,cat):
if cat not in self.minimums:
return 0
return self.minimums[cat]
def fisherprob(self,item,cat):
# Multiply all the probabilities together
p = 1
features = self.getfeatures(item)
for f in features:
p *= (self.weightedprob(f,cat,self.cprob))
# Take the natural log and multiply by -2
fscore = -2*math.log(p)
# Use the inverse chi2 function to get a probability
return self.invchi2(fscore,len(features)*2)
def invchi2(self,chi,df):
m = chi / 2.0
sum = term = math.exp(-m)
for i in range(1, df//2):
term *= m / i
sum += term
return min(sum, 1.0)
def classify(self,item,default=None):
# Loop through looking for the best result
best = default
max = 0.0
for c in self.categories():
p = self.fisherprob(item,c)
# Make sure it exceeds its minimum
if p>self.getminimum(c) and p>max:
best=c
max=p
return best