-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCODE_second.py
260 lines (238 loc) · 8.61 KB
/
CODE_second.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from pandas.plotting import scatter_matrix
%matplotlib inline
# Loading the dataset
df = pd.read_csv('Stock Headlines.csv', encoding = 'ISO-8859-1')
df.columns
df.shape
df.head(3)
# Visualizing the count of 'Label' column from the dataset
plt.figure(figsize=(4,4))
sns.countplot(x='Label', data=df)
plt.xlabel('Stock Sentiments (0-Down/Same, 1-Up)')
plt.ylabel('Count')
plt.show()
# Adding a scatter plot
#plt.subplot(1, 2, 2)
plt.subplot(1, 2, 2)
sns.scatterplot(x=df.columns[0], y=df.columns[1], data=df)
plt.xlabel('X Axis Label')
plt.ylabel('Y Axis Label')
plt.tight_layout()
plt.show()
print(df.shape)
# Finding any NaN values
df.isna().any()
# Dropping NaN values
df.dropna(inplace=True)
print(df.shape)
df_copy = df.copy()
df_copy.reset_index(inplace=True)
# Splitting the dataset into train an test set
train = df_copy[df_copy['Date'] < '20150101']
test = df_copy[df_copy['Date'] > '20141231']
print('Train size: {}, Test size: {}'.format(train.shape, test.shape))
train.columns
# Splitting the dataset
y_train = train['Label']
train = train.iloc[:, 3:28]
y_test = test['Label']
test = test.iloc[:, 3:28]
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
# Removing punctuation and special character from the text
train.replace(to_replace='[^a-zA-Z]', value=' ', regex=True, inplace=True)
test.replace(to_replace='[^a-zA-Z]', value=' ', regex=True, inplace=True)
# Renaming columns
new_columns = [str(i) for i in range(0,25)]
train.columns = new_columns
test.columns = new_columns
# Converting the entire text to lower case
for i in new_columns:
train[i] = train[i].str.lower()
test[i] = test[i].str.lower()
train_headlines = []
test_headlines = []
for row in range(0, train.shape[0]):
train_headlines.append(' '.join(str(x) for x in train.iloc[row, 0:25]))
for row in range(0, test.shape[0]):
test_headlines.append(' '.join(str(x) for x in test.iloc[row, 0:25]))
train_headlines[0]
test_headlines[0]
# Creating corpus of train dataset
ps = PorterStemmer()
train_corpus = []
for i in range(0, len(train_headlines)):
# Tokenizing the news-title by words
words = train_headlines[i].split()
# Removing the stopwords
words = [word for word in words if word not in set(stopwords.words('english'))]
# Stemming the words
words = [ps.stem(word) for word in words]
# Joining the stemmed words
headline = ' '.join(words)
# Building a corpus of news-title
train_corpus.append(headline)
# Creating corpus of test dataset
test_corpus = []
for i in range(0, len(test_headlines)):
# Tokenizing the news-title by words
words = test_headlines[i].split()
# Removing the stopwords
words = [word for word in words if word not in set(stopwords.words('english'))]
# Stemming the words
words = [ps.stem(word) for word in words]
# Joining the stemmed words
headline = ' '.join(words)
# Building a corpus of news-title
test_corpus.append(headline)
train_corpus[0:5]
test_corpus[0:5]
down_words = []
for i in list(y_train[y_train==0].index):
down_words.append(train_corpus[i])
up_words = []
for i in list(y_train[y_train==1].index):
up_words.append(train_corpus[i])
# Creating wordcloud for down_words
from wordcloud import WordCloud
wordcloud1 = WordCloud(background_color='white', width=3000, height=2500).generate(down_words[1])
plt.figure(figsize=(4,4))
plt.imshow(wordcloud1)
plt.axis('off')
plt.title("Words which indicate a fall in DJIA ")
plt.show()
# Creating wordcloud for up_words
wordcloud2 = WordCloud(background_color='white', width=3000, height=2500).generate(up_words[5])
plt.figure(figsize=(4,4))
plt.imshow(wordcloud2)
plt.axis('off')
plt.title("Words which indicate a rise in DJIA ")
plt.show()
# Creating the Bag of Words model
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(max_features=10000, ngram_range=(2,2))
X_train = cv.fit_transform(train_corpus).toarray()
X_test = cv.transform(test_corpus).toarray()
from sklearn.linear_model import LogisticRegression
lr_classifier = LogisticRegression()
lr_classifier.fit(X_train, y_train)
lr_y_pred = lr_classifier.predict(X_test)
# Accuracy, Precision and Recall
from sklearn.metrics import accuracy_score, precision_score, recall_score
score1 = accuracy_score(y_test, lr_y_pred)
score2 = precision_score(y_test, lr_y_pred)
score3 = recall_score(y_test, lr_y_pred)
print("---- Scores ----")
print("Accuracy score is: {}%".format(round(score1*100,2)))
print("Precision score is: {}".format(round(score2,2)))
print("Recall score is: {}".format(round(score3,2)))
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
lr_cm = confusion_matrix(y_test, lr_y_pred)
lr_cm
# Plotting the confusion matrix
plt.figure(figsize=(6,4))
sns.heatmap(data=lr_cm, annot=True, cmap="Blues", xticklabels=['Down', 'Up'], yticklabels=['Down', 'Up'])
plt.xlabel('Predicted values')
plt.ylabel('Actual values')
plt.title('Confusion Matrix for SVM Algorithm')
plt.show()
from sklearn.ensemble import RandomForestClassifier
rf_classifier = RandomForestClassifier(n_estimators=100, criterion='entropy')
rf_classifier.fit(X_train, y_train)
rf_y_pred = rf_classifier.predict(X_test)
# Accuracy, Precision and Recall
score1 = accuracy_score(y_test, rf_y_pred)
score2 = precision_score(y_test, rf_y_pred)
score3 = recall_score(y_test, rf_y_pred)
print("---- Scores ----")
print("Accuracy score is: {}%".format(round(score1*100,2)))
print("Precision score is: {}".format(round(score2,2)))
print("Recall score is: {}".format(round(score3,2)))
# Making the Confusion Matrix
rf_cm = confusion_matrix(y_test, rf_y_pred)
rf_cm
# Plotting the confusion matrix
plt.figure(figsize=(6,4))
sns.heatmap(data=rf_cm, annot=True, cmap="Blues", xticklabels=['Down', 'Up'], yticklabels=['Down', 'Up'])
plt.xlabel('Predicted values')
plt.ylabel('Actual values')
plt.title('Confusion Matrix for Random Forest Algorithm')
plt.show()
# Predicting the Test set results
from sklearn.naive_bayes import GaussianNB
nb_classifier = GaussianNB()
nb_classifier.fit(X_train, y_train)
nb_y_pred = nb_classifier.predict(X_test)
# Accuracy, Precision and Recall
score1 = accuracy_score(y_test, nb_y_pred)
score2 = precision_score(y_test, nb_y_pred)
score3 = recall_score(y_test, nb_y_pred)
print("---- Scores ----")
print("Accuracy score is: {}%".format(round(score1*100,2)))
print("Precision score is: {}".format(round(score2,2)))
print("Recall score is: {}".format(round(score3,2)))
# Making the Confusion Matrix
nb_cm = confusion_matrix(y_test, nb_y_pred)
nb_cm
# Plotting the confusion matrix
plt.figure(figsize=(6,4))
sns.heatmap(data=nb_cm, annot=True, cmap="Blues", xticklabels=['Down', 'Up'], yticklabels=['Down', 'Up'])
plt.xlabel('Predicted values')
plt.ylabel('Actual values')
plt.title('Confusion Matrix for Multinomial Naive Bayes Algorithm')
plt.show()
import re
def stock_prediction(sample_news):
sample_news = re.sub(pattern='[^a-zA-Z]',repl=' ', string=sample_news)
sample_news = sample_news.lower()
sample_news_words = sample_news.split()
sample_news_words = [word for word in sample_news_words if not word in set(stopwords.words('english'))]
ps = PorterStemmer()
final_news = [ps.stem(word) for word in sample_news_words]
final_news = ' '.join(final_news)
temp = cv.transform([final_news]).toarray()
return lr_classifier.predict(temp)
# For generating random integer
from random import randint
sample_test = df_copy[df_copy['Date'] > '20141231']
sample_test.reset_index(inplace=True)
sample_test = sample_test['Top1']
# Predicting values
row = randint(0,sample_test.shape[0]-1)
sample_news = sample_test[row]
print('News: {}'.format(sample_news))
if stock_prediction(sample_news):
print('Prediction: The stock price will remain the same or will go down.')
else:
print('Prediction: The stock price will go up!')
# Predicting values
row = randint(0,sample_test.shape[0]-1)
sample_news = sample_test[row]
print('News: {}'.format(sample_news))
if stock_prediction(sample_news):
print('Prediction: The stock price will remain the same or will go down.')
else:
print('Prediction: The stock price will go up!')
# Predicting values
row = randint(0,sample_test.shape[0]-1)
sample_news = sample_test[row]
print('News: {}'.format(sample_news))
if stock_prediction(sample_news):
print('Prediction: The stock price will remain the same or will go down.')
else:
print('Prediction: The stock price will go up!')
# Predicting values
row = randint(0,sample_test.shape[0]-1)
sample_news = sample_test[row]
print('News: {}'.format(sample_news))
if stock_prediction(sample_news):
print('Prediction: The stock price will remain the same or will go down.')
else:
print('Prediction: The stock price will go up!')