-
Notifications
You must be signed in to change notification settings - Fork 0
/
Toxic-Comment-Classification.py
472 lines (275 loc) · 9.09 KB
/
Toxic-Comment-Classification.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
#importing required libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import re
from nltk.stem import PorterStemmer, WordNetLemmatizer
import seaborn as sns
import matplotlib.cm as cm
import itertools
get_ipython().run_line_magic('matplotlib', 'inline')
# In[2]:
df = pd.read_csv('train.csv')
# In[3]:
df.head(10)
# In[4]:
df.shape
# In[5]:
df.columns
# In[6]:
df.dtypes
# In[7]:
df.describe()
# In[8]:
df.info()
# In[9]:
# finding all the rows where the sum of labels is zero(the comment is a Clean comment)
rowsums=df.iloc[:,2:].sum(axis=1)
df['clean']=(rowsums==0)
df['clean'].sum()
# In[10]:
# Total no.of toxic comments
len(df[df['toxic']==1])
# In[11]:
df.head(10)
# In[12]:
# Using seaborn and matplotlib to visualize the count of different categories of toxicity of comments
colors_list = ["red", "purple","maroon","yellow", "brown","black", "blue"]
palette= sns.xkcd_palette(colors_list)
x=df.iloc[:,2:].sum()
plt.figure(figsize=(10,6))
# x.index has all the toxicity labels and x.values has their respective count
ax= sns.barplot(x.index, x.values,palette=palette)
plt.title("Class")
plt.xlabel('Toxicity Type', fontsize = 12)
plt.ylabel('Occurrences', fontsize=12)
rects = ax.patches
labels = x.values
for rect, label in zip(rects, labels):
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2, height + 10, label,
ha='center', va='bottom')
plt.show()
# In[13]:
#The graph shows us that the dataset is highly imbalanced as more than 1.4lac comments are categorized as clean.
#Taking an insight of the length of the comments in the dataset.
comment = df['comment_text']
for i in range(5):
print(i,"- " + comment[i] + "\n Length -" ,len(comment[i]))
# In[14]:
#The length of the comments looks to be quite large, so we'll visualize some more info about the comments.
## creating a numpy array of the length of each comment in the dataset.
x = np.array([len(comment[i]) for i in range(comment.shape[0])])
# In[15]:
print("""The maximum length of comment is:{}
\nThe minimum length of the comment is:{}
\nAnd the average length of a comment is: {}""".format(x.max(),x.min(),x.mean()))
# In[16]:
print('The average length of comment is : 394.073' )
bins = [1,200,400,600,800,1000,1200,1400]
plt.hist(x, bins=bins, color = 'Green')
plt.xlabel('Length of comments')
plt.ylabel('Number of comments')
plt.axis([0, 1400, 0, 90000])
plt.grid(True)
plt.show()
# In[17]:
#It is visible that length of most of the comments(Approx 80,000) lies in the range of 0-200 and around 40,000 lie in between 200-400
#Now we will try to find the count of different toxicity of comments in each bin
label = df[['toxic', 'severe_toxic' , 'obscene' , 'threat' , 'insult' , 'identity_hate']]
print(label.head(10))
label = label.values
# In[18]:
label.shape
# In[19]:
# Creating a zero matrix of shape (159571,6)
y = np.zeros(label.shape)
for i in range(label.shape[0]):
l = len(comment[i])
if label[i][0] :
y[i][0] = l
if label[i][1] :
y[i][1] = l
if label[i][2] :
y[i][2] = l
if label[i][3] :
y[i][3] = l
if label[i][4] :
y[i][4] = l
if label[i][5] :
y[i][5] = l
label_plot = ['toxic','severe_toxic','obscene','threat','insult','identity_hate']
color = ['red','purple','maroon','yellow','black','green']
plt.figure(figsize = (10,8))
plt.hist(y,bins = bins,label = label_plot,color = color)
plt.axis([0, 1400, 0, 12000])
plt.xlabel('Length of comments', fontsize = 14)
plt.ylabel('Number of comments', fontsize = 14)
plt.legend()
plt.grid(True)
plt.show()
# In[20]:
#Removing excessive length comments
# creating a list of comments with less than 400 length of words.
trim_comments = [comment[i] for i in range(comment.shape[0]) if len(comment[i])<=400 ]
# creating corresponding labels for those comments
my_labels = np.array([label[i] for i in range(comment.shape[0]) if len(comment[i])<=400 ])
# In[21]:
my_labels[:10, :]
# In[22]:
print(len(trim_comments))
print(len(my_labels))
print("Thus number of removed comments = {}".format(159571-115910))
# In[23]:
#So now we are left with 115910 comments whose length is less than 400
print(len(trim_comments))
print(my_labels.shape)
# In[24]:
#Now we start the preprocessing of the comments.
# Punctuation removal
import string
print(string.punctuation)
punctuation_edit = string.punctuation.replace('\'','') +"0123456789"
print (punctuation_edit)
outtab = " "
trantab = str.maketrans(punctuation_edit, outtab)
# In[25]:
# Stopwords
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
# Adding alphabets to the set
for i in range(ord('a'),ord('z')+1):
stop_words.add(chr(i))
print(stop_words)
# In[26]:
# Stemming and Lemmatizing
from nltk.stem import WordNetLemmatizer, PorterStemmer
lemmatizer = WordNetLemmatizer()
stemmer = PorterStemmer()
# In[27]:
# Looping through all the comments and processing them through the functions defined above.
for i in range(len(trim_comments)):
trim_comments[i] = trim_comments[i].lower().translate(trantab)
word_list = []
for word in trim_comments[i].split():
if not word in stop_words:
word_list.append(stemmer.stem(lemmatizer.lemmatize(word,pos="v")))
trim_comments[i] = " ".join(word_list)
# In[28]:
# Comments after stop words removal, stemming and lemmatizing.
for i in range(5):
print(trim_comments[i],"\n")
# In[29]:
# Applying count vectorizer
from sklearn.feature_extraction.text import CountVectorizer
#create object supplying our custom stop words
count_vector = CountVectorizer(stop_words=stop_words)
#fitting it to converts comments into bag of words format
tf = count_vector.fit_transform(trim_comments[:20000]).toarray()
# In[30]:
tf.shape
# In[31]:
"""Note: Due to hardware limitation the processing has stopped within 20,000 comments and
the results and accuracy are obtained accordingly"""
#Splitting into training and testing
def shuffle(matrix, target, test_proportion):
ratio = int(matrix.shape[0]/test_proportion)
X_train = matrix[ratio:,:]
X_test = matrix[:ratio,:]
Y_train = target[ratio:,:]
Y_test = target[:ratio,:]
return X_train, X_test, Y_train, Y_test
X_train, X_test, Y_train, Y_test = shuffle(tf, my_labels[:20000],3)
print(X_test.shape)
print(X_train.shape)
# In[32]:
from sklearn.metrics import hamming_loss
from sklearn.metrics import accuracy_score
from sklearn.metrics import log_loss
def evaluate_score(Y_test,predict):
loss = hamming_loss(Y_test,predict)
print("Hamming_loss : {}".format(loss*100))
accuracy = accuracy_score(Y_test,predict)
print("Accuracy : {}".format(accuracy*100))
try :
loss = log_loss(Y_test,predict)
except :
loss = log_loss(Y_test,predict.toarray())
print("Log_loss : {}".format(loss))
# In[33]:
from sklearn.naive_bayes import MultinomialNB
# In[34]:
#Binary Relevance (BR) Method with MultinomialNB classifiers
# clf will be the list of the classifiers for all the 6 labels
# each classifier is fit with the training data and corresponding classifier
clf = []
for i in range(6):
clf.append(MultinomialNB())
clf[i].fit(X_train,Y_train[:,i])
# In[35]:
# predict list contains the predictions, it is transposed later to get the proper shape
predict = []
for i in range(6):
predict.append(clf[i].predict(X_test))
predict = np.asarray(np.transpose(predict))
print(predict.shape)
# In[36]:
#calculate scores
evaluate_score(Y_test,predict)
# In[37]:
#BR Method with SVM classifier (from scikit-multilearn)
from skmultilearn.problem_transform import BinaryRelevance
from sklearn.svm import SVC
classifier = BinaryRelevance(classifier = SVC(), require_dense = [False, True])
classifier.fit(X_train, Y_train)
# In[38]:
#predictions
predictions = classifier.predict(X_test)
# In[39]:
#calculate scores
evaluate_score(Y_test,predictions)
# In[40]:
#BR Method with GaussianNB classifier.
from sklearn.naive_bayes import GaussianNB
#create and fit classifiers
clf = []
for i in range(6):
clf.append(GaussianNB())
clf[i].fit(X_train,Y_train[:,i])
# In[41]:
#predictions
predict = []
for ix in range(6):
predict.append(clf[ix].predict(X_test))
# In[42]:
#calculate scores
predict = np.asarray(np.transpose(predict))
evaluate_score(Y_test,predict)
# In[43]:
#Visualizing the result
x = ['BR-MultnomialNB','BR-SVC','BR-GaussianNB']
y = [3.65,4.36,20.74]
colors = itertools.cycle(['r', 'g', 'b'])
plt.figure(figsize= (8,6))
plt.ylabel('Hamming-Loss')
plt.xlabel('Model-details')
plt.xticks(rotation=90)
for i in range(len(y)):
plt.bar(x[i], y[i], color=next(colors))
plt.show()
# In[44]:
#The hamming loss is maximum for BR-GaussianNB and minimum for BR-MultinomialNB
x = ['BR-MultNB','BR-SVC','BR-GausNB']
y = [1.97,0.46,1.422]
colors = itertools.cycle(['r', 'g', 'b'])
plt.figure(figsize=(8,6))
plt.ylabel('Log-Loss')
plt.xlabel('Model-details')
plt.xticks(rotation=90)
for i in range(len(y)):
plt.bar(x[i], y[i], color=next(colors))
plt.show()
# In[ ]: