-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
205 lines (128 loc) · 4.97 KB
/
model.py
File metadata and controls
205 lines (128 loc) · 4.97 KB
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
import numpy as np
import pandas as pd
import os
!pip install tweepy
import tweepy as tw
import re
import nltk
nltk.download("stopwords")
from nltk.corpus import stopwords
from nltk.stem.porter import *
from sklearn.model_selection import train_test_split
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Embedding, Conv1D, MaxPooling1D, Bidirectional, LSTM, Dense, Dropout
from keras.metrics import Precision, Recall
df = pd.read_csv('./Reddit_Data.csv')
df['category'] = df['category'].map({-1.0:'Negative', 0.0:'Neutral', 1.0:'Positive'})
def comment_to_words(comment):
text = comment.lower()
text = re.sub(r"[^a-zA-Z0-9]", " ", text)
words = text.split()
words = [w for w in words if w not in stopwords.words("english")]
words = [PorterStemmer().stem(w) for w in words]
return words
from sklearn.preprocessing import LabelEncoder
# Encode target labels
le = LabelEncoder()
Y = le.fit_transform(df['category'])
#Test and Train Split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=1)
from sklearn.feature_extraction.text import CountVectorizer
vocabulary_size = 5000
count_vector = CountVectorizer(max_features=vocabulary_size,preprocessor=lambda x: x,tokenizer=lambda x: x)
X_train = count_vector.fit_transform(X_train).toarray()
X_test = count_vector.transform(X_test).toarray()
#Tokenizing and Padding
max_words = 5000
max_len=50
def tokenize_pad_sequences(text):
# Text tokenization
tokenizer = Tokenizer(num_words=max_words, lower=True, split=' ')
tokenizer.fit_on_texts(text)
# Transforms text to a sequence of integers
X = tokenizer.texts_to_sequences(text)
# Pad sequences to the same length
X = pad_sequences(X, padding='post', maxlen=max_len)
# return sequences
return X, tokenizer
X, tokenizer = tokenize_pad_sequences(df['clean_comment'])
#Pickle Library
import pickle
# saving
with open('tokenizer.pickle', 'wb') as handle:
pickle.dump(tokenizer, handle, protocol=pickle.HIGHEST_PROTOCOL)
# loading
with open('tokenizer.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
y = pd.get_dummies(df['category'])
X_train, X_test,y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 42)
# Extracting validation set from the train set
valid_size=1000
X_valid, y_valid = X_train[-valid_size:], y_train[-valid_size:]
X_test, y_test = X_train[:-valid_size], y_train[:-valid_size]
vocab_size = 5000
embedding_size = 32
# Build model
model= Sequential()
model.add(Embedding(vocab_size, embedding_size, input_length=max_len))
model.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='relu'))
model.add(Bidirectional(LSTM(32)))
model.add(Dropout(0.4))
model.add(Dense(3, activation='softmax'))
tf.keras.utils.plot_model(model, show_shapes=True)
print(model.summary())
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy', Precision(), Recall()])
# Train model
num_epochs = 10
batch_size = 32
history = model.fit(X_train, y_train,
validation_data=(X_valid, y_valid),
batch_size=batch_size, epochs=num_epochs, verbose=1)
#Plots:
#Accuracy
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
plt.plot(history.epoch, acc, 'r', label='Training accuracy')
plt.plot(history.epoch, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.grid(True)
plt.figure()
#Loss
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.plot(history.epoch, loss, 'r', label='Training Loss')
plt.plot(history.epoch, val_loss, 'b', label='Validation Loss')
plt.title('Training and validation loss')
plt.legend()
plt.grid(True)
plt.show()
#Metrics Plotting
pd.DataFrame(history.history).plot(figsize=(8,5))
plt.grid(True)
plt.gca().set_ylim(0, 1)
#Confusion Matrix:
def plot_confusion_matrix(model, X_test, y_test):
'''Function to plot confusion matrix for the passed model and the data'''
sentiment_classes = ['Negative', 'Neutral', 'Positive']
# use model to do the prediction
y_pred = model.predict(X_test)
# compute confusion matrix
cm = confusion_matrix(np.argmax(np.array(y_test),axis=1), np.argmax(y_pred, axis=1))
# plot confusion matrix
plt.figure(figsize=(8,6))
sns.heatmap(cm, cmap=plt.cm.Blues, annot=True, fmt='d',
xticklabels=sentiment_classes,
yticklabels=sentiment_classes)
plt.title('Confusion matrix', fontsize=16)
plt.xlabel('Actual label', fontsize=12)
plt.ylabel('Predicted label', fontsize=12)
plot_confusion_matrix(model, X_test, y_test)
model.save('Reddit_comments_model_2.h5')