-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier-example.py
More file actions
executable file
·60 lines (49 loc) · 1.64 KB
/
Copy pathclassifier-example.py
File metadata and controls
executable file
·60 lines (49 loc) · 1.64 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
#!/usr/bin/env python3
import nbayes
import random
import sys
if len(sys.argv) > 1:
random.seed( int(sys.argv[1]) )
nwords = ('this', 'that', 'the-other')
swords = ('viagra', 'stuff', 'blah') + nwords
hwords = ('douglas', 'jimmy', 'blah') + nwords
maxwlen = max([ len(x) for x in set(nwords+swords+hwords) ])
class Email(object):
src_words = nwords
def __init__(self):
self.words = []
for i in range(random.randint(5,15)):
self.words.append( random.choice(self.src_words) )
@property
def label(self):
return self.__class__.__name__.lower()
@property
def instance(self):
return nbayes.Instance(self.label, self.words)
def __str__(self):
return "{}<>".format(self.label)
class Spam(Email):
src_words = swords
class Ham(Email):
src_words = hwords
data = [ Spam().instance for i in range(random.randint(7,20)) ]
data += [ Ham().instance for i in range(random.randint(7,20)) ]
corpus = nbayes.Classifier(data)
print(corpus)
# for i in range(len(data)):
# p_spam = corpus.prob_label_not_label_given_attr('spam','ham', data[i].attr)
#
# ok = result = 'classified correctly'
# if p_spam >= 0.5 and data[i].label == 'ham':
# result = 'false positive'
# if p_spam < 0.5 and data[i].label == 'spam':
# result = 'false negative'
#
# moar = result
# if result != ok:
# moar += ' ' + str(data[i].attr)
# print("P(spam|data[{:2}]) = {:0.4f} {}".format(i, p_spam, moar))
# print("\nmethod 2")
for i in range(len(data)):
res = corpus.classify( data[i].attr )
print("R(data[{}]) = {} (actual: {})".format( i, res, data[i].label ))