-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclassification_nltk_v1.py
44 lines (34 loc) · 1.16 KB
/
classification_nltk_v1.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
import nltk
nltk.usage(nltk.classify.ClassifierI)
train = [
(dict(a=1,b=1,c=1), 'y'),
(dict(a=1,b=1,c=1), 'x'),
(dict(a=1,b=1,c=0), 'y'),
(dict(a=0,b=1,c=1), 'x'),
(dict(a=0,b=1,c=1), 'y'),
(dict(a=0,b=0,c=1), 'y'),
(dict(a=0,b=1,c=0), 'x'),
(dict(a=0,b=0,c=0), 'x'),
(dict(a=0,b=1,c=1), 'y'),
]
test = [
(dict(a=1,b=0,c=1)), # unseen
(dict(a=1,b=0,c=0)), # unseen
(dict(a=0,b=1,c=1)), # seen 3 times, labels=y,y,x
(dict(a=0,b=1,c=0)), # seen 1 time, label=x
]
classifier = nltk.classify.NaiveBayesClassifier.train(train)
sorted(classifier.labels())
classifier.classify_many(test)
for pdist in classifier.prob_classify_many(test):
print('%.4f %.4f' % (pdist.prob('x'), pdist.prob('y')))
classifier.show_most_informative_features()
classifier = nltk.classify.DecisionTreeClassifier.train(
train, entropy_cutoff=0, support_cutoff=0)
sorted(classifier.labels())
print(classifier)
classifier.classify_many(test)
print(classifier)
'''
for pdist in classifier.prob_classify_many(test):
print('%.4f %.4f' % (pdist.prob('x'), pdist.prob('y')))'''