0% found this document useful (0 votes)
6 views11 pages

Fake News Detection Using Machine Learning

.

Uploaded by

kefemi8514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Fake News Detection Using Machine Learning

.

Uploaded by

kefemi8514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Fake News Detector

Installing Necessary Libraries


import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import string
import re
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report

Loading the data


data_fake=pd.read_csv('Fake.csv')
data_true=pd.read_csv('True.csv')

Data Preview
data_fake.head()

title \
0 Donald Trump Sends Out Embarrassing New Year’...
1 Drunk Bragging Trump Staffer Started Russian ...
2 Sheriff David Clarke Becomes An Internet Joke...
3 Trump Is So Obsessed He Even Has Obama’s Name...
4 Pope Francis Just Called Out Donald Trump Dur...

text subject \
0 Donald Trump just couldn t wish all Americans ... News
1 House Intelligence Committee Chairman Devin Nu... News
2 On Friday, it was revealed that former Milwauk... News
3 On Christmas day, Donald Trump announced that ... News
4 Pope Francis used his annual Christmas Day mes... News

date
0 December 31, 2017
1 December 31, 2017
2 December 30, 2017
3 December 29, 2017
4 December 25, 2017

data_true.tail()
title \
21412 'Fully committed' NATO backs new U.S. approach...
21413 LexisNexis withdrew two products from Chinese ...
21414 Minsk cultural hub becomes haven from authorities
21415 Vatican upbeat on possibility of Pope Francis ...
21416 Indonesia to buy $1.14 billion worth of Russia...

text subject \
21412 BRUSSELS (Reuters) - NATO allies on Tuesday we... worldnews
21413 LONDON (Reuters) - LexisNexis, a provider of l... worldnews
21414 MINSK (Reuters) - In the shadow of disused Sov... worldnews
21415 MOSCOW (Reuters) - Vatican Secretary of State ... worldnews
21416 JAKARTA (Reuters) - Indonesia will buy 11 Sukh... worldnews

date
21412 August 22, 2017
21413 August 22, 2017
21414 August 22, 2017
21415 August 22, 2017
21416 August 22, 2017

data_fake["class"]=0
data_true['class']=1

data_fake.shape, data_true.shape

((23481, 5), (21417, 5))

data_fake_manual_testing = data_fake.tail(10)
for i in range(23480,23470,-1):
data_fake.drop([i],axis = 0, inplace = True)

data_true_manual_testing = data_true.tail(10)
for i in range(21416,21406,-1):
data_true.drop([i],axis = 0, inplace = True)

data_fake.shape, data_true.shape

((23471, 5), (21407, 5))

data_fake_manual_testing['class']=0
data_true_manual_testing['class']=1

C:\Users\Dell\AppData\Local\Temp\ipykernel_1640\1676563180.py:1:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation:
https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#
returning-a-view-versus-a-copy
data_fake_manual_testing['class']=0
C:\Users\Dell\AppData\Local\Temp\ipykernel_1640\1676563180.py:2:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation:


https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#
returning-a-view-versus-a-copy
data_true_manual_testing['class']=1

data_fake_manual_testing.head(10)

title \
23471 Seven Iranians freed in the prisoner swap have...
23472 #Hashtag Hell & The Fake Left
23473 Astroturfing: Journalist Reveals Brainwashing ...
23474 The New American Century: An Era of Fraud
23475 Hillary Clinton: ‘Israel First’ (and no peace ...
23476 McPain: John McCain Furious That Iran Treated ...
23477 JUSTICE? Yahoo Settles E-mail Privacy Class-ac...
23478 Sunnistan: US and Allied ‘Safe Zone’ Plan to T...
23479 How to Blow $700 Million: Al Jazeera America F...
23480 10 U.S. Navy Sailors Held by Iranian Military ...

text subject
\
23471 21st Century Wire says This week, the historic... Middle-east

23472 By Dady Chery and Gilbert MercierAll writers ... Middle-east

23473 Vic Bishop Waking TimesOur reality is carefull... Middle-east

23474 Paul Craig RobertsIn the last years of the 20t... Middle-east

23475 Robert Fantina CounterpunchAlthough the United... Middle-east

23476 21st Century Wire says As 21WIRE reported earl... Middle-east

23477 21st Century Wire says It s a familiar theme. ... Middle-east

23478 Patrick Henningsen 21st Century WireRemember ... Middle-east

23479 21st Century Wire says Al Jazeera America will... Middle-east

23480 21st Century Wire says As 21WIRE predicted in ... Middle-east


date class
23471 January 20, 2016 0
23472 January 19, 2016 0
23473 January 19, 2016 0
23474 January 19, 2016 0
23475 January 18, 2016 0
23476 January 16, 2016 0
23477 January 16, 2016 0
23478 January 15, 2016 0
23479 January 14, 2016 0
23480 January 12, 2016 0

data_true_manual_testing.head(10)

title \
21407 Mata Pires, owner of embattled Brazil builder ...
21408 U.S., North Korea clash at U.N. forum over nuc...
21409 U.S., North Korea clash at U.N. arms forum on ...
21410 Headless torso could belong to submarine journ...
21411 North Korea shipments to Syria chemical arms a...
21412 'Fully committed' NATO backs new U.S. approach...
21413 LexisNexis withdrew two products from Chinese ...
21414 Minsk cultural hub becomes haven from authorities
21415 Vatican upbeat on possibility of Pope Francis ...
21416 Indonesia to buy $1.14 billion worth of Russia...

text subject \
21407 SAO PAULO (Reuters) - Cesar Mata Pires, the ow... worldnews
21408 GENEVA (Reuters) - North Korea and the United ... worldnews
21409 GENEVA (Reuters) - North Korea and the United ... worldnews
21410 COPENHAGEN (Reuters) - Danish police said on T... worldnews
21411 UNITED NATIONS (Reuters) - Two North Korean sh... worldnews
21412 BRUSSELS (Reuters) - NATO allies on Tuesday we... worldnews
21413 LONDON (Reuters) - LexisNexis, a provider of l... worldnews
21414 MINSK (Reuters) - In the shadow of disused Sov... worldnews
21415 MOSCOW (Reuters) - Vatican Secretary of State ... worldnews
21416 JAKARTA (Reuters) - Indonesia will buy 11 Sukh... worldnews

date class
21407 August 22, 2017 1
21408 August 22, 2017 1
21409 August 22, 2017 1
21410 August 22, 2017 1
21411 August 21, 2017 1
21412 August 22, 2017 1
21413 August 22, 2017 1
21414 August 22, 2017 1
21415 August 22, 2017 1
21416 August 22, 2017 1
data_merge=pd.concat([data_fake, data_true], axis = 0)
data_merge.head(10)

title \
0 Donald Trump Sends Out Embarrassing New Year’...
1 Drunk Bragging Trump Staffer Started Russian ...
2 Sheriff David Clarke Becomes An Internet Joke...
3 Trump Is So Obsessed He Even Has Obama’s Name...
4 Pope Francis Just Called Out Donald Trump Dur...
5 Racist Alabama Cops Brutalize Black Boy While...
6 Fresh Off The Golf Course, Trump Lashes Out A...
7 Trump Said Some INSANELY Racist Stuff Inside ...
8 Former CIA Director Slams Trump Over UN Bully...
9 WATCH: Brand-New Pro-Trump Ad Features So Muc...

text subject \
0 Donald Trump just couldn t wish all Americans ... News
1 House Intelligence Committee Chairman Devin Nu... News
2 On Friday, it was revealed that former Milwauk... News
3 On Christmas day, Donald Trump announced that ... News
4 Pope Francis used his annual Christmas Day mes... News
5 The number of cases of cops brutalizing and ki... News
6 Donald Trump spent a good portion of his day a... News
7 In the wake of yet another court decision that... News
8 Many people have raised the alarm regarding th... News
9 Just when you might have thought we d get a br... News

date class
0 December 31, 2017 0
1 December 31, 2017 0
2 December 30, 2017 0
3 December 29, 2017 0
4 December 25, 2017 0
5 December 25, 2017 0
6 December 23, 2017 0
7 December 23, 2017 0
8 December 22, 2017 0
9 December 21, 2017 0

"title", "subject" and "date" columns is not required for detecting the fake news, so I
am going to drop the columns.
data_merge.columns

Index(['title', 'text', 'subject', 'date', 'class'], dtype='object')

data=data_merge.drop(['title','subject','date'], axis = 1)

#count of missing values


data.isnull().sum()
text 0
class 0
dtype: int64

Randomly shuffling the dataframe


data = data.sample(frac = 1)

data.head()

text class
1147 As the divisions in the nation bubble over, it... 0
7953 Six people are dead after a white, domestic te... 0
9026 WASHINGTON (Reuters) - Democrat Hillary Clinto... 1
16943 ATHENS (Reuters) - Greek police said they arre... 1
5809 WASHINGTON/LOS ANGELES (Reuters) - U.S. Presid... 1

data.reset_index(inplace = True)
data.drop(['index'], axis = 1, inplace = True)

data.columns

Index(['text', 'class'], dtype='object')

data.head()

text class
0 As the divisions in the nation bubble over, it... 0
1 Six people are dead after a white, domestic te... 0
2 WASHINGTON (Reuters) - Democrat Hillary Clinto... 1
3 ATHENS (Reuters) - Greek police said they arre... 1
4 WASHINGTON/LOS ANGELES (Reuters) - U.S. Presid... 1

Preprocessing Text
Creating a function to convert the text in lowercase, remove the extra space, special
chr., ulr and links.
def wordopt(text):
text = text.lower()
text = re.sub('\[.*?\]','',text)
text = re.sub("\\W"," ",text)
text = re.sub('https?://\S+|www\.\S+','',text)
text = re.sub('<.*?>+',b'',text)
text = re.sub('[%s]' % re.escape(string.punctuation),'',text)
text = re.sub('\w*\d\w*','',text)
return text

data['text'] = data['text'].apply(wordopt)
Defining dependent and independent variable as x and y
x = data['text']
y = data['class']

Training the model


Splitting the dataset into training set and testing set.
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size =
0.25)

Extracting Features from the Text


Convert text to vectors
from sklearn.feature_extraction.text import TfidfVectorizer

vectorization = TfidfVectorizer()
xv_train = vectorization.fit_transform(x_train)
xv_test = vectorization.transform(x_test)

Logistic Regression
from sklearn.linear_model import LogisticRegression

LR = LogisticRegression()
LR.fit(xv_train, y_train)

LogisticRegression()

pred_lr = LR.predict(xv_test)

LR.score(xv_test, y_test)

0.9877896613190731

print (classification_report(y_test, pred_lr))

precision recall f1-score support

0 0.99 0.99 0.99 5834


1 0.99 0.99 0.99 5386

accuracy 0.99 11220


macro avg 0.99 0.99 0.99 11220
weighted avg 0.99 0.99 0.99 11220
Decision Tree Classifier
from sklearn.tree import DecisionTreeClassifier

DT = DecisionTreeClassifier()
DT.fit(xv_train, y_train)

DecisionTreeClassifier()

pred_dt = DT.predict(xv_test)

DT.score(xv_test, y_test)

0.9952762923351158

print (classification_report(y_test, pred_lr))

precision recall f1-score support

0 0.99 0.99 0.99 5834


1 0.99 0.99 0.99 5386

accuracy 0.99 11220


macro avg 0.99 0.99 0.99 11220
weighted avg 0.99 0.99 0.99 11220

Gradient Boost Classifier


from sklearn.ensemble import GradientBoostingClassifier

GB = GradientBoostingClassifier(random_state = 0)
GB.fit(xv_train, y_train)

GradientBoostingClassifier(random_state=0)

pred_gb = GB.predict(xv_test)

GB.score(xv_test, y_test)

0.9944741532976827

print(classification_report(y_test, pred_gb))

precision recall f1-score support

0 1.00 0.99 0.99 5834


1 0.99 1.00 0.99 5386

accuracy 0.99 11220


macro avg 0.99 0.99 0.99 11220
weighted avg 0.99 0.99 0.99 11220
Random Forest Classifier
from sklearn.ensemble import RandomForestClassifier

RF = RandomForestClassifier(random_state = 0)
RF.fit(xv_train, y_train)

RandomForestClassifier(random_state=0)

pred_rf = RF.predict(xv_test)

RF.score(xv_test, y_test)

0.9869875222816399

print (classification_report(y_test, pred_rf))

precision recall f1-score support

0 0.99 0.99 0.99 5834


1 0.99 0.98 0.99 5386

accuracy 0.99 11220


macro avg 0.99 0.99 0.99 11220
weighted avg 0.99 0.99 0.99 11220

Testing the Model


def output_lable(n):
if n==0:
return "Fake News"
elif n==1:
return "Not A Fake News"

def manual_testing(news):
testing_news = {"text":[news]}
new_def_test = pd.DataFrame(testing_news)
new_def_test['text'] = new_def_test["text"].apply(wordopt)
new_x_test = new_def_test["text"]
new_xv_test = vectorization.transform(new_x_test)
pred_LR = LR.predict(new_xv_test)
pred_DT = DT.predict(new_xv_test)
pred_GB = GB.predict(new_xv_test)
pred_RF = RF.predict(new_xv_test)

return print("\n\nLR Predicition: {} \nDT Prediction: {} \nGBC


Prediction: {} \nRFC Prediction:{}".format(output_lable(pred_LR[0]),

output_lable(pred_DT[0]),
output_lable(pred_GB[0]),

output_lable(pred_RF[0])))

Model Testing With Manual Entry


news = str(input())
manual_testing(news)

Federal health officials told the AP they have not received any
reports of Ebola cases at the Nevada event. A screenshot of a supposed
post from the Centers for Disease Control and Prevention confirming
such cases was fabricated. And there is no record of a national
emergency being declared. The claims emerged after summer storm left
muddy roads flooded, stranding tens of thousands of partygoers; event
organizers let traffic flow out of the main road Monday afternoon. “So
it was announced earlier that Burning Man was declared a national
emergency because it was flooded, and so they sent in FEMA,” a woman
claims in a TikTok video shared on Instagram, suggesting the
development was suspicious. The AP found no record, including on
federal websites and in White House announcements, of a national
emergency declaration and FEMA confirmed that it was not involved in
the situation. “No FEMA personnel or assets have been deployed to the
Burning Man festival and there are no requests from local or state
authorities for our assistance,” FEMA spokesperson Jeremy Edwards said
in an email. The TikTok video, like other posts, goes on to relay
baseless rumors of reported cases of Ebola, whose occasional outbreaks
in humans primarily occur in Africa, at the festival. Some posts also
shared an image made to appear that the CDC confirmed the supposed
outbreak on X, the platform formerly known as Twitter. The purported X
post from the agency reads, “Ebola outbreak confirmed at Black Rock
City, NV. It is recommended that all Burning Man attendees remain in
their dwellings until further notice. Current State of Emergency in
progress.” But the CDC’s X account published no such post. “CDC has
not received any reports of Ebola at the Burning Man Festival and has
not issued any warnings or had any requests for assistance from the
state and local health departments either,” agency spokesperson Scott
Pauley said in an email. Reverse image searches further show that a
graphic about Ebola used in the fictitious CDC post was published by
the agency in 2016, but elements of it were changed. For example, the
original graphic asks, “Recently in West Africa?” But the version used
in the made-up X post asks, “Recently in Nevada?” Referencing more
online rumors, Pauley also noted the CDC had not received reports of
mpox, formerly known as monkeypox, or Marburg, a rare but severe
hemorrhagic fever, in relation to Burning Man. A representative for
the Burning Man Project organization also refuted the online claims.
“Quite simply, the online rumors of transmissible illnesses in Black
Rock City are unfounded and untrue,” Dominique Debucquoy-Dodley said
in an email. The festival had been closed to vehicles after more than
a half-inch (1.3 centimeters) of rain fell Sept. 1, causing flooding
and foot-deep mud, as the AP reported. The annual gathering, which
launched on a San Francisco beach in 1986, attracts nearly 80,000
artists, musicians and activists for a mix of wilderness camping and
avant-garde performances

LR Predicition: Fake News


DT Prediction: Fake News
GBC Prediction: Fake News
RFC Prediction:Fake News

news=str(input())
manual_testing(news)

MOSCOW (Reuters) - Russia’s former ambassador to Washington, Sergei


Kislyak, said on Saturday his conversations with former White House
national security adviser Michael Flynn had been transparent and
focused on matters of U.S.-Russia cooperation. Kislyak ended his
tenure in Washington in July but remains a key figure in ongoing U.S.
investigations into Moscow’s alleged meddling in the 2016
presidential election. Flynn was forced to resign in February after it
became known that he had failed to disclose the content of
conversations he had with Kislyak and misled U.S. Vice-President Mike
Pence about their meetings. “We only spoke about the most simple
things ... but the communication was completely correct, calm,
absolutely transparent. In any case, there were no secrets on our
side,” Kislyak said during a panel discussion on Russian television.
“There are a number of issues which are important for cooperation
between Russia and the United States - most of all, terrorism. And
that was one of the things we discussed.”

LR Predicition: Not A Fake News


DT Prediction: Not A Fake News
GBC Prediction: Not A Fake News
RFC Prediction:Not A Fake News

You might also like