forked from FavioVazquez/fake-news
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFakeNews.py
293 lines (168 loc) · 5.09 KB
/
FakeNews.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
#!/usr/bin/env python
# coding: utf-8
# # Fake News Detection
# In[1]:
import matplotlib.pyplot as plt
import pandas as pd
# ## Read Datasets
# In[2]:
fake_df = pd.read_csv("data/Fake.csv")
real_df = pd.read_csv("data/True.csv")
# In[3]:
print(fake_df.shape)
# In[4]:
print(real_df.shape)
# ## Data Preparation
# In[5]:
# Add flag to track fake and real
fake_df["target"] = "fake"
real_df["target"] = "real"
# In[6]:
# Concatenate fake and real dataframes
df = pd.concat([fake_df, real_df]).reset_index(drop=True)
print(df.shape)
# In[7]:
# Shuffle data
from sklearn.utils import shuffle
df = shuffle(df)
df = df.reset_index(drop=True)
print(df.head())
# In[8]:
# Remove title & date column
df.drop(["title", "date"], axis=1, inplace=True)
print(df.head())
# ## Data Cleaning
# In[9]:
df.dropna(subset=["text"], inplace=True)
df.reset_index(drop=True, inplace=True)
# In[10]:
# Convert to lowercase
df["text"] = df["text"].str.lower()
print(df.head())
# In[11]:
# Remove numbers, punctuations and extra spaces
df["text"] = df["text"].str.replace(r"[^a-zA-Z\s]", "", regex=True)
df["text"] = df["text"].str.replace(r"\s+", " ", regex=True)
df["text"] = df["text"].str.strip()
print(df.head())
# In[12]:
# Remove stopwords
import nltk
from nltk.corpus import stopwords
nltk.download("stopwords")
stopwords_ = stopwords.words("english")
df["text"] = df["text"].apply(lambda x: " ".join([word for word in x.split() if word not in stopwords_]))
print(df.head())
# ## Basic Data Exploration
# In[13]:
# How many articles per subject?
articles_subject_count = df.groupby(["subject"])["text"] .count() .sort_values(ascending=False)
print(articles_subject_count)
articles_subject_count.plot(kind="bar")
plt.show()
# In[14]:
# How many fake and real articles?
articles_types_count = df.groupby(["target"])["text"].count()
print(articles_types_count)
articles_types_count.plot(kind="bar")
plt.show()
# In[15]:
from wordcloud import WordCloud
def show_wordcloud(text):
wordcloud = WordCloud(
width=800,
height=500,
max_font_size=110,
collocations=False
).generate(text)
plt.figure(figsize=(12, 8))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
# In[16]:
# Most frequent words counter (Code adapted from https://www.kaggle.com/rodolfoluna/fake-news-detector)
import nltk
import seaborn as sns
from nltk.tokenize import WhitespaceTokenizer
def show_frequent_words(text, quantity):
tokens = WhitespaceTokenizer().tokenize(text)
frequency = nltk.FreqDist(tokens)
frequency_df = pd.DataFrame(
data=frequency.items(),
columns=["Word", "Frequency"]
)
frequency_df = frequency_df.nlargest(columns="Frequency", n=quantity)
plt.figure(figsize=(11, 8))
ax = sns.barplot(data=frequency_df, x="Word", y="Frequency", color="blue")
ax.set(ylabel="Count")
plt.xticks(rotation="vertical")
plt.show()
# In[17]:
# Word cloud and most frequent words for fake news
text = " ".join(df[df["target"] == "fake"]["text"].to_list())
show_wordcloud(text)
show_frequent_words(text, 20)
# In[18]:
# Word cloud and most frequent words for real news
text = " ".join(df[df["target"] == "real"]["text"].to_list())
show_wordcloud(text)
show_frequent_words(text, 20)
# ## Modeling
# In[19]:
# Vectorizing and applying TF-IDF
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.metrics import accuracy_score, ConfusionMatrixDisplay
from sklearn.pipeline import Pipeline
def generate_model(model, X_train, X_test, y_train, y_test):
pipeline = Pipeline([
("vectorizer", CountVectorizer()),
("tfidf", TfidfTransformer()),
("model", model)
])
# Fitting the model
model = pipeline.fit(X_train, y_train)
# Checking accuracy
y_pred = model.predict(X_test)
print("Accuracy: {}%".format(round(accuracy_score(y_test, y_pred) * 100, 2)))
# Plotting confusion matrix
disp = ConfusionMatrixDisplay.from_predictions(
y_test, y_pred,
normalize=None,
display_labels=["Fake", "Real"],
cmap=plt.cm.Blues
)
disp.ax_.set_title("Confusion matrix, without normalization")
# ### Peparing the data
# In[20]:
# Split the data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(df["text"], df.target, test_size=0.2, random_state=42)
# ### Logistic Regression
# In[21]:
from sklearn.linear_model import LogisticRegression
generate_model(
LogisticRegression(),
X_train, X_test, y_train, y_test
)
# ### Decision Tree Classifier
# In[22]:
from sklearn.tree import DecisionTreeClassifier
generate_model(
DecisionTreeClassifier(
criterion="entropy",
max_depth=20,
splitter="best",
random_state=42
),
X_train, X_test, y_train, y_test
)
# ### Random Forest Classifier
# In[23]:
from sklearn.ensemble import RandomForestClassifier
generate_model(
RandomForestClassifier(
n_estimators=50,
criterion="entropy"
),
X_train, X_test, y_train, y_test
)