-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcateg.py
214 lines (163 loc) · 6.83 KB
/
categ.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
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
208
209
210
211
212
213
214
#!/usr/bin/python
# -*-coding:utf-8-*
import nltk
from nltk.corpus import reuters
from nltk.classify.scikitlearn import SklearnClassifier
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import MultinomialNB, BernoulliNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import LinearSVC
from sklearn.neighbors import KNeighborsClassifier, NearestCentroid
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.linear_model import Perceptron
from sklearn import metrics
import matplotlib.pyplot as plt
################################################################################
# Usage
################################################################################
# python categ.py
################################################################################
################################################################################
# Functions
################################################################################
def features(words):
features = {}
for word in words:
features[word] = True
return features
def buildSet(t_set,docs):
for doc in docs:
categories = reuters.categories(doc)
for category in categories:
t_set.append((features(reuters.words(doc)), category))
def classify(features_selection,classification_method):
pipeline = []
if features_selection == "tf-idf":
pipeline.append((features_selection, TfidfTransformer()))
elif features_selection == "chi2":
pipeline.append((features_selection, SelectKBest(chi2, k=1000)))
if classification_method == "M-NB":
pipeline.append((classification_method, MultinomialNB()))
if classification_method == "B-NB":
pipeline.append((classification_method, BernoulliNB()))
elif classification_method == "SVM":
pipeline.append((classification_method, LinearSVC()))
#elif classification_method == "Decision Tree":
# pipeline.append((classification_method, DecisionTreeClassifier()))
# classifier = SklearnClassifier(Pipeline(pipeline),sparse=False)
elif classification_method == "KNN":
pipeline.append((classification_method, KNeighborsClassifier()))
elif classification_method == "Rocchio":
pipeline.append((classification_method, NearestCentroid()))
elif classification_method == "Perceptron":
pipeline.append((classification_method, Perceptron()))
return SklearnClassifier(Pipeline(pipeline))
def getResults(classifier,test_set,precisions,recalls):
test = []
truth = []
for (feat,cat) in test_set:
test.append(feat)
truth.append(cat)
categories = classifier.batch_classify(test)
precision = metrics.precision_score(truth,categories)
recall = metrics.recall_score(truth,categories)
fmeasure = 2.0 * (precision*recall) / (precision+recall)
precisions.append(precision)
recalls.append(recall)
print '\tPrecision =', precision
print '\tRecall =', recall
print '\tF-measure =', fmeasure, '\n'
################################################################################
################################################################################
# Main
################################################################################
print 'Loading docs...'
train_docs = []
test_docs = []
for fileid in reuters.fileids():
if fileid.startswith('training'):
train_docs.append(fileid)
if fileid.startswith('test'):
test_docs.append(fileid)
print 'Building training set...'
training_set = []
buildSet(training_set,train_docs)
print 'Building test set...'
test_set = []
buildSet(test_set,test_docs)
precisions_tfidf=[]
recalls_tfidf=[]
precisions_chi2=[]
recalls_chi2=[]
print '\n################################################################################'
print '# TF-IDF'
print '################################################################################\n'
print 'Training the Multinomial Naive Bayes classifier...'
classifier = classify("tf-idf","M-NB")
classifier.train(training_set)
getResults(classifier,test_set,precisions_tfidf,recalls_tfidf)
print 'Training the Bernoulli Naive Bayes classifier...'
classifier = classify("tf-idf","B-NB")
classifier.train(training_set)
getResults(classifier,test_set,precisions_tfidf,recalls_tfidf)
print 'Training the SVM classifier...'
classifier = classify("tf-idf","SVM")
classifier.train(training_set)
getResults(classifier,test_set,precisions_tfidf,recalls_tfidf)
print 'Training the K-nearest Neighbors classifier...'
classifier = classify("tf-idf","KNN")
classifier.train(training_set)
getResults(classifier,test_set,precisions_tfidf,recalls_tfidf)
print 'Training the Rocchio\'s classifier...'
classifier = classify("tf-idf","Rocchio")
classifier.train(training_set)
getResults(classifier,test_set,precisions_tfidf,recalls_tfidf)
print 'Training the Perceptron classifier...'
classifier = classify("tf-idf","Perceptron")
classifier.train(training_set)
getResults(classifier,test_set,precisions_tfidf,recalls_tfidf)
# print 'Training the Decision Tree classifier...'
# classifier = classify("tf-idf","Decision Tree")
# classifier.train(training_set)
# getResults(classifier,test_set)
print '\n################################################################################'
print '# Chi squared'
print '################################################################################\n'
print 'Training the Multinomial Naive Bayes classifier...'
classifier = classify("chi2","M-NB")
classifier.train(training_set)
getResults(classifier,test_set,precisions_chi2,recalls_chi2)
print 'Training the Bernoulli Naive Bayes classifier...'
classifier = classify("chi2","B-NB")
classifier.train(training_set)
getResults(classifier,test_set,precisions_chi2,recalls_chi2)
print 'Training the SVM classifier...'
classifier = classify("chi2","SVM")
classifier.train(training_set)
getResults(classifier,test_set,precisions_chi2,recalls_chi2)
print 'Training the K-nearest Neighbors classifier...'
classifier = classify("chi2","KNN")
classifier.train(training_set)
getResults(classifier,test_set,precisions_chi2,recalls_chi2)
print 'Training the Rocchio\'s classifier...'
classifier = classify("chi2","Rocchio")
classifier.train(training_set)
getResults(classifier,test_set,precisions_chi2,recalls_chi2)
print 'Training the Perceptron classifier...'
classifier = classify("chi2","Perceptron")
classifier.train(training_set)
getResults(classifier,test_set,precisions_chi2,recalls_chi2)
print 'Displaying plot...'
plt.plot(recalls_tfidf,precisions_tfidf,'ro',label='Tf-idf')
plt.plot(recalls_chi2,precisions_chi2,'b^',label='Chi2')
plt.ylabel('Precision')
plt.xlabel('Recall')
methods=["M-NB","B-NB","SVM","KNN","Rocchio","Perceptron"]
for i in range(len(precisions_tfidf)):
plt.text(recalls_tfidf[i],precisions_tfidf[i],methods[i])
for i in range(len(precisions_chi2)):
plt.text(recalls_chi2[i],precisions_chi2[i],methods[i])
plt.legend(loc='upper left')
plt.show()
################################################################################