-
Notifications
You must be signed in to change notification settings - Fork 11
/
sentimentmodel_wPickle.py
587 lines (441 loc) · 18.9 KB
/
sentimentmodel_wPickle.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import pandas.io.data as web
from sklearn.feature_extraction.text import TfidfVectorizer
import re
from sklearn.cross_validation import train_test_split, cross_val_score
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression, LinearRegression
import psycopg2
import pandas.io.sql as psql
import ipdb
import sys
from sklearn.cluster import KMeans
from sklearn.metrics import confusion_matrix, silhouette_score, roc_curve, auc
from sklearn.decomposition import NMF
import cPickle
import matplotlib.pyplot as plt
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews, stopwords
from pandas.tseries.offsets import *
from dateutil import parser
from nltk.tokenize import RegexpTokenizer
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.grid_search import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
from pprint import pprint
from time import time
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
sp_done = 0
def generate_tfidf(data):
'''
Description:
Generates a tfidf vectorizer
Parameters:
Returns: a parameterized vectorizer
'''
n_features = 10000
n_topics = 10
n_top_words = 20
# tfidf = TfidfVectorizer(max_df=0.95, min_df=min_df, max_features=n_features, stop_words='english')
tfidf = TfidfVectorizer(max_features=n_features, stop_words='english')
clfv = tfidf.fit_transform(data)
# with open ('kmeans_tfidf.pkl', 'wb') as fid:
# cPickle.dump(tfidf, fid)
return tfidf, clfv
def getConn(DBNAME, DBUSER, PASSWRD, tablename, bLabel=False):
'''
Description:
Generic database connection
Parameters:
Database name, user, password, tablename
Returns: dataframe with URL, title, content, date
'''
conn = psycopg2.connect(database=DBNAME, user=DBUSER, password=PASSWRD)
# flexible sql statement to return labeled and unlabeled data
if not bLabel:
sql = 'SELECT url, title, content, date, label from ' + tablename + ' where label is null'
else:
sql = 'SELECT url, title, content, date, label from ' + tablename + ' where label is NOT null'
df = psql.frame_query(sql, conn)
conn.close()
return df
def getScrapedContent(bLabel):
'''
Description:
Gets data from multiple datasources having URL, title, content, date and returns a merged dataframe
Parameters:
Returns a merged dataframe that hasn't been cleaned
'''
DBNAME = zip(['newscontent', 'financenews'],['stocknews_newscontent2', 'data2'])
DBUSER = 'ethancheung'
PASSWRD = open('password.txt').readline()
rDf = pd.DataFrame()
for eDB in DBNAME:
rDf = rDf.append(getConn(eDB[0], DBUSER, PASSWRD, eDB[1], bLabel))
return rDf
def combineHistVolColumn(contentDf, volDf):
'''
Description:
Scraped content dataframe from postgres
Volatility dataframe from google
RETURNS:
Merges the scraped web content with the historical volatility labels
Precondition: Scraped content dataframe, volatility dataframe
Returns: Content dataframe, label dataframe
'''
# reset the index to make the Date column available to be joined with other dataframe
volDf = volDf.reset_index()
volDf.columns = ['index', 'Volatility']
#make date columns same type
contentDf['Date_obj'] = pd.to_datetime(contentDf['date'])
# ENHANCEMENT: calculate the volatility for each document per given time period
volDf['Date_obj'] = pd.to_datetime(volDf['index'])
merged = pd.merge(contentDf, volDf, on='Date_obj', how='outer')
# handle NAs
merged = merged.dropna(subset=['content', 'url'])
X = merged[['content', 'url']]
y = merged['Volatility']
# fill in the weekends with 0 volatilty
y = y.fillna(0)
return X, y
def getHistoricalVolatility(time_period):
'''
Description:
Builds the S&P daily historical volatility dataframe since 2012-11-15 to end of 2014
Parameters:
Returns: Dataframe of S&P using 1 day lag
'''
global sp_done
if sp_done == 0:
data_start = '2012-11-15'
sp = web.DataReader('^GSPC', data_source='yahoo', start = data_start, end = '2014-12-31')
sp['IntraDay_Vol'] = sp['High']-sp['Low'] # intra_vol will be proxy for volatility instead of taking std of closing prices
sp.fillna(0, inplace=True)
sp_done = 1
sp.to_csv('hist_vol.csv')
else:
sp = pd.read_csv('hist_vol.csv', parse_dates=True, index_col='Date')
# calculate the volatility delta over a defined range before and after each date
vol_delta = pd.DataFrame()
temp_dict = {}
for eDate in sp.index.date:
temp_dict[eDate] = volatility_delta(eDate, time_period, sp)
vol_delta = vol_delta.from_dict(temp_dict, orient='index')
vol_delta = vol_delta.sort_index()
return vol_delta
def volatility_delta(doc_date, time_period, df):
'''
Description:
Without exact time stamps for documents and minute financial data, assumes that documents for a given day contributes to the volatility for that day
(i.e. documents are not separated by day)
Parameters:
Document date and the period of interest to calculate the volatility differences
Data dataframe containing historical volatility
'''
before = df.ix[price_data(doc_date, time_period, df, True)]['IntraDay_Vol']
before.fillna(0, inplace=True)
after = df.ix[price_data(doc_date, time_period, df, False)]['IntraDay_Vol']
after.fillna(0, inplace=True)
v_before = volatility(before)
v_after = volatility(after)
vol_delta = v_before - v_after
return vol_delta
def price_data(obj_doc_date, time_frame, data_df, bBefore):
'''
Description:
Helper function for volatility_delta
time_frame is measured in business days
doc_date is the median of the date range
Returns the volatility data for the period before and after. After period is inclusive of the document release date
Receives data_df and indexed by .['2014-1-30': XXX]
XXX is determined from the time_frame. For example, 2014-1-23 if time_frame is week and 2014-2-6 after
'''
# make the start date
if type(obj_doc_date) == 'str':
obj_doc_date = parser.parse(obj_doc_date)
# ensure that beginning period does not include the day the document is released
if bBefore:
data_window = pd.date_range(obj_doc_date - (time_frame + 1) * BDay(), obj_doc_date - 1 * Day())
else:
data_window = pd.date_range(obj_doc_date, obj_doc_date + time_frame * BDay())
# get volatility for the period before and after
return data_window #(data_df.ix[range_before]['intra_vol'], data_df.ix[range_after]['intra_vol'])
def volatility(price_data):
'''
Description:
Helper function for volatility_delta
Dataframe containing the price data of the S&P for a specified period
Parameters:
Receives data for a window period
Returns volatility of price range data
'''
return np.std(price_data)
def displayScore(clf, X_train, y_train, X_test, y_test, y_pred):
'''
Description:
Generalizable display score function
Parameters:
Receives various test, train dataframes
Returns: Nothing
'''
# 1 estimator score method
print "\nEstimator score method: ", str(clf.score(X_test, y_test)) + '\n'
# 2 scoring parameter
try:
scores = cross_val_score(clf, X_train, y_train, cv=2, scoring='accuracy')
print "Scoring parameter 'accuracy' from cross val: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() / 2)
# 3 scoring via metric functions
# print average_precision_score(y_test, y_pred)
print confusion_matrix(y_test, y_pred)
except:
# pass in the case of unevaluable sparse matrices
pass
def calculateSentiment(X):
'''
Description:
Calculates sentiment as a feature of predicting volatility
Parameters:
Receives the corpus of documents
Returns dataframe of one column containing sentiment
'''
nb_classifer = train_sentiment_classifier()
tokenizer = RegexpTokenizer(r'\w+')
sentDict = {}
for idx, eDoc in enumerate(X):
# classify the sentiment
newdict = {}
for i in tokenizer.tokenize(eDoc):
newdict[i] = True
sentDict[idx] = nb_classifer.classify(newdict)
sentDf = pd.DataFrame.from_dict(sentDict, orient='index')
sentDf = sentDf.sort_index()
return sentDf
def linear_reg():
# get content that is labeled using getScraped
# Case 1: for supervised learning
article_df = getScrapedContent(True)
df1_label = article_df['label']
df1_content = article_df[['content','date']]
sp_df = getHistoricalVolatility()
X, y_vol = combineHistVolColumn(df1_content, sp_df)
X_sentiment = calculateSentiment(X)
# generate tfidf
tfidf, clfv = generate_tfidf(X['content'])
X_train, X_test, y_train, y_test = train_test_split(clfv, df1_label, test_size=0.4, random_state=42)
clf = LinearRegression()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
displayScore(clf, X_train, y_train, X_test, y_test, y_pred)
def word_feats(words):
'''
Description:
Helper function for sentiment classifier
Parameters:
Receives words
Returns dict
'''
return dict([(word, True) for word in words])
def train_sentiment_classifier():
'''
Description:
Trains naive bayes classifier
Bootstrapped with basic movie word corpus
Parameters:
Returns: a NB trained classifier
'''
negids = movie_reviews.fileids('neg')
posids = movie_reviews.fileids('pos')
negfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'neg') for f in negids]
posfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'pos') for f in posids]
negcutoff = len(negfeats)*3/4
poscutoff = len(posfeats)*3/4
trainfeats = negfeats[:negcutoff] + posfeats[:poscutoff]
testfeats = negfeats[negcutoff:] + posfeats[poscutoff:]
classifier = NaiveBayesClassifier.train(trainfeats)
return classifier
def kmeans_logistic():
# Case 2: for unsupervised/semisupervised
article_df2 = getScrapedContent(False)
df2_content = article_df2[['content','date']]
df2_date = article_df2['date']
sp_df = getHistoricalVolatility()
X, y_vol = combineHistVolColumn(df2_content, sp_df)
# generate vectorized clfv
tfidf, clfv = generate_tfidf(X['content'])
clf = KMeans(n_clusters=10, init='k-means++', max_iter=100) #, n_init=1)
clf.fit_predict(clfv)
labels = clf.labels_
# with open ('kmeans_km_model.pkl', 'wb') as fid:
# cPickle.dump(clf, fid)
print '\nSilouette score :', str(silhouette_score(clfv, labels, metric='euclidean')) + '\n'
X_train, X_test, y_train, y_test = train_test_split(clfv, labels, test_size=0.4, random_state=42)
clf_lr = LogisticRegression()
clf_lr.fit(X_train, y_train)
y_pred = clf_lr.predict(X_test)
with open ('km_lr_tfi_model.pkl', 'wb') as fid:
cPickle.dump((clf_lr, tfidf), fid)
displayScore(clf_lr, X_train, y_train, X_test, y_test, y_pred)
def displayROC(X_test, y_test, clf, showAUC, i, volThres):
'''
Description:
If showAUC = True, displays the ROC curve
Parameters:
Receives X_test, y_test and a classifier
Returns a graph if showAUC = True else, returns the value of roc_auc
'''
probas_ = clf.predict_proba(X_test)
fpr, tpr,thresholds = roc_curve(y_test, probas_[:,1])
roc_auc = auc(fpr, tpr)
print("Area under the ROC curve : %f " % roc_auc, i)
if showAUC:
plt.figure()
plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC : ' + str(clf)[:10] + ' ' + str(i) + ',' + str(volThres))
plt.legend(loc="lower right")
plt.show()
return roc_auc
def newTestClassify(text):
'''
Description:
Used to classify new text
'''
textdata = ''.join([i if ord(i) < 128 else ' ' for i in text])
def add_Binary_Features(orig_Content, featureDf):
'''
Description:
Calculates sentiment, source info, binarizes and adds to feature DataFrame
Parameters:
Receives original content dataframe, feature DataFrame
Returns updated dataframe with binarized features
'''
sentDf = pd.DataFrame(calculateSentiment(orig_Content['content']))
# binarize the sentiment and add one column
dummies1 = pd.get_dummies(sentDf[0])
featureDf['Sentiment'] = dummies1.iloc[:,:-1] #[:,1:2] second column is pos
orig_Content['url'] = orig_Content['url'].apply(lambda x: x.split('.')[1])
dummies2 = pd.get_dummies(orig_Content['url'])
featureDf = pd.concat([featureDf, dummies2.iloc[:,:-1]], join='outer', axis=1)
featureDf['Date'] = featureDf['Date'].apply(lambda x: pd.to_datetime(x).dayofweek)
return featureDf
def importance_forest(data, label):
"""Compute feature importance using decision trees classifier.
INPUT: data -- numeric pandas dataframe with non-missing values
label -- boolean pandas series with which to predict on
OUTPUT: results sent to stdout
"""
clf = ExtraTreesClassifier()
clf.fit(data, label)
for imp, col in sorted( zip(clf.feature_importances_, data.columns), key=lambda (imp, col): imp, reverse=True ):
print "[{:.5f}] {}".format(imp, col)
def get_feature_matrix(df):
'''
Description:
Takes dataframe containing:
'content', 'data', 'url'
Uses pickled NMF to transform tfidf to categories
Adds binaries 'Sentiment', 'url', 'Date'
Date is represented as day of the week feature
Parameters:
Receives dataframe containing 'content', 'date', 'url'
Returns feature matrix
'''
with open ('/Users/ethancheung/Documents/zipfianacademy/FoxScraper/km_lrnmf_tfi_model.pkl', 'rb') as fid:
n_clf, logclf_loaded, tfclf_loaded, nb_classifer = cPickle.load(fid)
def show_confusion_mat(y_test, y_pred, i, volThres):
cm = confusion_matrix(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
print 'Precision :', precision, 'Recall :', recall
print cm
# Show confusion matrix
plt.matshow(cm)
plt.title('Confusion matrix ' + str(i) + ',' + str(volThres))
plt.colorbar()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
return precision, recall
def nmf_logistic():
# Case 2: for unsupervised/semisupervised
article_df2 = getScrapedContent(False)
df2_content = article_df2[['content','date', 'url']]
df2_date = article_df2['date']
iPer = 7
showAUC = False
aRocScore = []
volThres = 1
sp_df = getHistoricalVolatility(iPer)
X, y_vol = combineHistVolColumn(df2_content, sp_df)
# when using nmf, X_test is used bc I want to discover latent topics
n_topics = 10
n_top_words = 15
n_clf = NMF(n_components=n_topics, random_state=1)
tfidf, clfv = generate_tfidf(X['content'])
W = n_clf.fit_transform(clfv)
H = n_clf.components_
W_corr = pd.DataFrame(W)
W_corr['Date'] = df2_date.values
W_corr['Volatility'] = y_vol.values
# add binarize features
data = add_Binary_Features(X, W_corr)
# this label is continuous and won't work for logistic/random forest
label = data.pop('Volatility')
######## LOGISTIC #########
t0 = time()
lr_clf = LogisticRegression(C=1, penalty='l1', tol=0.01)
W_corr['HasVolatility'] = W_corr['Volatility'].apply(lambda x: 1 if x > volThres or x < -volThres else 0)
# binarize the label for logistic
label_ = W_corr.pop('HasVolatility')
X_train, X_test, y_train, y_test = train_test_split(data, label_, test_size=0.4, random_state=42)
lr_clf.fit(X_train, y_train)
aucscore = displayROC(X_test, y_test, lr_clf, showAUC, iPer, volThres)
y_pred = lr_clf.predict(X_test)
if showAUC:
precision, recall = show_confusion_mat(y_test, y_pred, iPer, volThres)
rocauc.append((precision, recall))
######## RANDOM FOREST #########
rf_clf = RandomForestClassifier(verbose=10, n_estimators=1, n_jobs=-1, max_features=None)
rf_clf.fit(X_train, y_train)
aucscore = displayROC(X_test, y_test, rf_clf, showAUC, iPer, volThres)
y_pred = rf_clf.predict(X_test)
if showAUC:
precision, recall = show_confusion_mat(y_test, y_pred, iPer, volThres)
################################ PREDICTION
######## LINEAR REG #########
print "------------------- performing LINEAR REG"
X_train, X_test, y_train, y_test = train_test_split(data, label, test_size=0.4, random_state=42)
lin_clf = LinearRegression()
lin_clf.fit(X_train, y_train)
y_pred = lin_clf.predict(X_test)
displayScore(lin_clf, X_train, y_train, X_test, y_test, y_pred)
print ("Residual sum of squares: %.2f" %
np.mean((lin_clf.predict(X_test) - y_test) ** 2))
# Explained variance score: 1 is perfect prediction
print ('Variance score: %.2f' % lin_clf.score(X_test, y_test))
nb_classifer = train_sentiment_classifier()
# pickle the clssifiers
with open ('lr_lin_n_clf.pkl', 'wb') as fid:
cPickle.dump((n_clf, lin_clf, lr_clf, tfidf, nb_classifer), fid)
# with open ('n_lin_lr_tf_nb.pkl', 'wb') as fid:
# cPickle.dump((n_clf, lin_clf, lr_clf, tfidf, nb_classifer), fid)
# with open ('rf.pkl', 'wb') as fid:
# cPickle.dump(rf_clf, fid)
fid.close()
print
options = {
'km' : kmeans_logistic,
'nmf' : nmf_logistic,
'lin' : linear_reg,
}
if __name__ == '__main__':
cmd_option = sys.argv[1:]
options[cmd_option[1]]()