-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake_news_classifier_lstm.py
160 lines (110 loc) · 3.4 KB
/
fake_news_classifier_lstm.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
# -*- coding: utf-8 -*-
"""fake_news_classifier_lstm.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1SxnGT0eYGAzgl-iKqWGNnXahp1ihwHAg
"""
from google.colab import files
files.upload()
# Let's make sure the kaggle.json file is present.
!ls -lha kaggle.json
!pip install -q kaggle
# The Kaggle API client expects this file to be in ~/.kaggle,
# so move it there.
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
# This permissions change avoids a warning on Kaggle tool startup.
!chmod 600 ~/.kaggle/kaggle.json
!pip install kaggle==1.5.6
!kaggle competitions download -c fake-news
!unzip fake-news.zip -d FakeNews
import pandas as pd
df = pd.read_csv('/content/FakeNews/train.csv')
df.head()
df.count()
df.isnull().sum()
df = df.dropna()
df.shape
df = df.drop('id', axis=1)
df.head()
news = df.copy()
news.head(10)
news.reset_index(inplace=True)
news.head(10)
## Get the Independent Features
Z=df.drop('label',axis=1)
Z.shape
X = df.iloc[ : , 0:3]
X.head()
X.shape
y = df.iloc[:, 3]
y
y.shape
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
import re
from nltk.stem.porter import PorterStemmer
corpus = []
ps = PorterStemmer()
for i in range(0, len(news)):
check = re.sub('[^a-zA-Z]', ' ', news['title'][i])
check = check.lower()
check = check.split()
check = [ps.stem(word) for word in check if not word in stopwords.words('english')]
check = ' '.join(check)
corpus.append(check)
corpus[1]
corpus
from tensorflow.keras.layers import Embedding
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.text import one_hot
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dense
voc_size = 10000
onehot_repr = [one_hot(words,voc_size)for words in corpus]
onehot_repr[0]
sent_length= 20
embedded_docs=pad_sequences(onehot_repr,padding='pre',maxlen=sent_length)
embedded_docs
embedded_docs[0]
embedding_features=40
model=Sequential()
model.add(Embedding(voc_size,embedding_features,input_length=sent_length))
model.add(LSTM(100))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
print(model.summary())
import numpy as np
X_final=np.array(embedded_docs)
y_final=np.array(y)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_final, y_final, test_size=0.33, random_state=42)
model.fit(X_train, y_train, validation_data= (X_test,y_test), epochs=10, batch_size=64)
from tensorflow.keras.layers import Dropout
embedding_vector_features= 100
model=Sequential()
model.add(Embedding(voc_size,embedding_vector_features,input_length=sent_length))
model.add(Dropout(0.3))
model.add(LSTM(100))
model.add(Dropout(0.3))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(X_train, y_train, validation_data= (X_test,y_test), epochs=10, batch_size=64)
y_test
pred = model.predict(X_test)
pred
y_test.shape
pred.shape
pred = pred.flatten()
pred.shape
pred
pred = (pred > 9.00e-01).astype(int)
pred
from sklearn.metrics import confusion_matrix, classification_report
cm = confusion_matrix(y_test, pred)
cm
print(classification_report(y_test, pred))
from sklearn.metrics import accuracy_score
accuracy_score(y_test, pred)