-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo.py
executable file
·279 lines (185 loc) · 6.74 KB
/
algo.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# coding: utf-8
# In[200]:
#get_ipython().magic(u'matplotlib inline')
import matplotlib.pyplot as plt
import csv, time, sys
from textblob import TextBlob
import pandas
import sklearn
import random
import cPickle
import pylab as pl
import numpy as np
from sklearn.utils import shuffle
import textblob
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC, LinearSVC
from sklearn.metrics import classification_report, f1_score, accuracy_score, confusion_matrix, roc_curve, auc
from sklearn.pipeline import Pipeline
from sklearn.grid_search import GridSearchCV
from sklearn.cross_validation import StratifiedKFold, cross_val_score, train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.learning_curve import learning_curve
# In[289]:
def clean(txt):
not_list = ['...', 'you', 'your', 'his', 'her', 'will', "didn't", "i'll"]
replace_list = [".", "'", '"', "!", "?", "#", "\r\n", "-", "@", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "+", "$"]
cleaned = []
for token in str(txt).split(' '):
flag = False
for y in not_list:
if y in token.lower():
flag = True
break
if flag: continue
for y in replace_list:
token = token.replace(y, "")
if len(token) > 3:
token = token.strip().lower();
try:
#word = textblob.TextBlob(token).words[0]
cleaned.append(token)
except:
continue
return ' '.join(cleaned).decode("ascii", "ignore")
# In[290]:
def clean_pos(txt):
pos = TextBlob(txt).tags
cleaned = [x[0] for x in pos if x[1] != 'IN' and x[1] != 'PRP' and x[1] != 'WP' and x[1] != 'FW']
return ' '.join(cleaned)
# In[291]:
safe_stuff = []
no_safe_stuff = []
# In[418]:
f = open("trace.log", 'wb')
f.write("start\n")
print "sdf"
safedf = pandas.DataFrame()
no_safedf = pandas.DataFrame()
messages = pandas.read_csv('./safe_recipe.csv', sep=',', quoting=csv.QUOTE_NONE,
names=["date", "rating", "review"])
safedf['review'] = messages[messages.columns[2:4]]
safedf['label'] = "safe"
safedf = shuffle(safedf).reset_index(drop=True)
messages = pandas.read_csv('./no_safe_recipe.csv', sep=',', quoting=csv.QUOTE_NONE,
names=["date", "rating", "review"])
no_safedf['review'] = messages[messages.columns[2:4]]
no_safedf['label'] = "not_safe"
dataset = safedf.loc[0:10592,:]
dataset = dataset.append(no_safedf)
##print len(dataset)
dataset['review'] = dataset['review'].map(clean)
##print dataset
# In[397]:
dataset.replace('', np.nan, inplace=True)
dataset.dropna(subset=['review'], inplace=True)
dataset['review'] = dataset['review'].map(clean_pos)
##print dataset.head()
f.write("checkpoint1\n");
print "This is 1"
# In[398]:
#dataset.groupby('label').describe()
# In[399]:
dataset['length'] = dataset['review'].map(lambda text: len(text))
##print dataset.head()
# In[400]:
#dataset.length.plot(bins=20, kind='hist', figsize=(16, 8))
# In[401]:
#dataset.length.describe()
# In[402]:
#dataset.hist(column='length', by='label', bins=50, figsize=(16, 8))
# In[403]:
def split_into_lemmas(message):
words = TextBlob(message).words
# for each word, take its "base form" = lemma
return [word.lemma for word in words]
# In[404]:
# not appropriate data, need training and test set
dataset = shuffle(dataset)
# In[405]:
msg_train, msg_test, label_train, label_test = train_test_split(dataset['review'], dataset['label'], test_size=0.2)
##print len(msg_train), len(msg_test), len(msg_train) + len(msg_test)
# In[406]:
f.write("checkpoint2\n")
print "This is 2"
pipeline = Pipeline([
('bow', CountVectorizer(analyzer=split_into_lemmas)), # strings to token integer counts
('tfidf', TfidfTransformer()), # integer counts to weighted TF-IDF scores
('classifier', MultinomialNB()), # train on TF-IDF vectors w/ Naive Bayes classifier
])
# In[407]:
# Cross-validation is a technique for evaluating ML models by training several
# ML models on subsets of the available input data and
# evaluating them on the complementary subset of the data
scores = cross_val_score(pipeline, # steps to convert raw messages into models
msg_train, # training data
label_train, # training labels
cv=10, # split data randomly into 10 parts: 9 for training, 1 for scoring
scoring='accuracy', # which scoring metric?
n_jobs=1, # -1 = use all cores = faster
)
##print scores
# In[408]:
##print scores.mean(), scores.std()
print "after cv"
# In[409]:
params = {
'tfidf__use_idf': (True, False),
'bow__analyzer': (split_into_lemmas,),
}
grid = GridSearchCV(
pipeline, # pipeline from above
params, # parameters to tune via cross validation
refit=True, # fit using all available data at the end, on the best found param combination
n_jobs=1, # number of cores to use for parallelization; -1 for "all cores"
scoring='accuracy', # what score are we optimizing?
cv=StratifiedKFold(label_train, n_folds=5), # what type of cross validation to use
)
nb_detector = grid.fit(msg_train, label_train)
f.write("checkpoint3\n")
print 'This is 3'
# In[410]:
#def ##print_top10(vectorizer, clf):
# """##prints features with the highest coefficient values, per class"""
# feature_names = vectorizer.get_feature_names()
#for i, class_label in enumerate(class_labels):
##print np.argsort(clf.coef_[0])
# top10 = np.argsort(clf.coef_[0])[-10:]
# bot10 = np.argsort(clf.coef_[0])[0:10]
##print("%s" % (" ".join(feature_names[j] for j in top10)))
##print("%s" % (" ".join(feature_names[j] for j in bot10)))
###print nb_detector.grid_scores_
###print nb_detector.best_estimator_
##print_top10(best.named_steps['bow'], best.named_steps['classifier'])
# In[411]:
#predictions = nb_detector.predict(msg_test)
#prob = nb_detector.predict_proba(msg_test)
# In[430]:
##print predictions
def get_prediction(txt):
blob = clean(txt)
blob = clean_pos(blob)
x = []
x.append(blob)
return nb_detector.predict(x)[0]
def get_prediction2(txt):
blob = clean(txt)
blob = clean_pos(blob)
predi = nb_detector.predict_proba(txt)
f.write(str(predi))
predi = predi[:,1]
f.write(str(predi))
score = (np.sum(predi) / predi.size)
f.write(str(score))
rscore = round(score)
f.write(str(rscore))
if rscore == 1:
return "safe"
else:
return "not_safe"
# In[432]:
f.write("checkpoint4\n")
print 'This is 4'
##print classification_report(label_test, predictions)
#print 'This is 5'