-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.py
154 lines (137 loc) · 4.53 KB
/
classifier.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
# -*- coding: utf-8 -*-
import json
import re
import numpy as np
from sklearn import grid_search
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import SVC
from sklearn.svm import LinearSVC
from sklearn import cross_validation
import sklearn.metrics
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.lda import LDA
from sklearn.metrics import confusion_matrix
from matplotlib import pyplot as plt
import matplotlib
import pickle
import os
import sys
def generateFeatures(headings, cities, sections):
special_chars = [';', '-', '?', '#', '*', '/', '_', '(', ')', '&', ':', '<', '>',
'{', '}', '.', '!', '@', '\\', '%', '~', '`', '^', '-', '+'
, '=', '[', ']', '\'', '"', ',']
X = []
for datapoint in zip(headings, cities, sections):
upperCharsCount = sum(1 for c in datapoint[0] if c.isupper())
specialCharsCount = sum(1 for c in datapoint[0] if c in special_chars)
text = datapoint[0]
text = re.sub(r'£|\$', ' denonimation', text)
for sc in special_chars:
text = text.replace(sc, '')
for sym in re.findall(r'\d+|\d+[.,]\d+', text):
text = text.replace(sym, ' numbr ')
text = text.replace(' ', ' ')
l = [text, str(upperCharsCount), str(specialCharsCount), datapoint[2]]
X.append(" ".join(ele for ele in l))
return X
def generateDefaultFeatures(headings):
X = []
for data in headings:
X.append(data)
return X
def readFile(file):
data = []
f = open(file)
for line in f:
data.append(json.loads(line))
return data
def getData(data):
headings = []
cities = []
sections = []
y = []
for x in data:
headings.append(x['heading'])
cities.append(x['city'])
sections.append(x['section'])
try:
y.append(x['category'])
except:
continue
return headings, cities, sections, y
def trainClassifier(classifier, X, y):
vectorizer = TfidfVectorizer(analyzer='char', use_idf=True, sublinear_tf=True, stop_words='english', ngram_range=(1,3), lowercase=True)
# vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(X).toarray()
if classifier == "SVC":
clf = LinearSVC()
# parameters = {'kernel':['linear', 'rbf'], 'C':[0.1, 1, 10]}
# clf = grid_search.GridSearchCV(clf, parameters)
clf.fit(X, y)
# print clf.best_params_
clf_vect = [clf, vectorizer]
f = open('svm.pkl', 'wb')
pickle.dump(clf_vect, f)
return clf, vectorizer
elif classifier == "RF":
clf = RandomForestClassifier()
clf.fit(X, y)
return clf, vectorizer
elif classifier == "MNB":
clf = MultinomialNB()
clf.fit(X, y)
return clf, vectorizer
elif classifier == "LDA":
clf = LDA()
clf.fit(X, y)
return clf, vectorizer
elif classifier == "KNN":
clf = KNeighborsClassifier()
clf.fit(X, y)
return clf, vectorizer
def confusionMatrix(clf, XTest, yTrue, cmap=plt.cm.Blues):
matplotlib.rc('xtick', labelsize=6)
matplotlib.rc('ytick', labelsize=6)
yPred = clf.predict(XTest.toarray()).tolist()
cm = confusion_matrix(yTrue, yPred)
plt.figure()
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title("Confusion Matrix")
plt.colorbar()
mylist = list(set(yTrue + yPred))
mylist.sort()
tick_marks = np.arange(len((mylist)))
plt.xticks(tick_marks, mylist, rotation=38)
plt.yticks(tick_marks, mylist)
plt.tight_layout()
plt.savefig('cm2.eps', format='eps', dpi=300)
def main():
data = readFile('training.json')
headings, cities, sections, y = getData(data)
# X = generateDefaultFeatures(headings)
X = generateFeatures(headings, cities, sections)
if not os.path.exists("svm.pkl"):
clf, vectorizer = trainClassifier("SVC", X, y)
else:
f = open("svm.pkl")
clf, vectorizer = pickle.load(f)
print "loading complete"
# print clf.predict(vectorizer.transform(["iPhone"]))
sys.exit()
test = readFile('sample-test.in.json')
headings_test, cities_test, sections_test, y_test = getData(test)
X_test = generateFeatures(headings_test, cities_test, sections_test)
y_test = []
fytest = open('sample-test.out.json')
for line in fytest:
y_test.append(line.strip('\n'))
X_test = vectorizer.transform(X_test)
# confusionMatrix(clf, X_test, y_test)
print "Accuracy: ", cross_validation.cross_val_score(clf, X_test.toarray(), y_test).mean()
print "F1: ", sklearn.metrics.f1_score(y_test, clf.predict(X_test.toarray()).tolist()).mean()
print "Precision: ", sklearn.metrics.precision_score(y_test, clf.predict(X_test.toarray()).tolist()).mean()
print "Recall: ", sklearn.metrics.recall_score(y_test, clf.predict(X_test.toarray()).tolist()).mean()
if __name__ == "__main__":
main()