Fake News Detection Using Machine Learning
Fake News Detection Using Machine Learning
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
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
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
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
23474 Paul Craig RobertsIn the last years of the 20t... Middle-east
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
data=data_merge.drop(['title','subject','date'], axis = 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
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']
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
DT = DecisionTreeClassifier()
DT.fit(xv_train, y_train)
DecisionTreeClassifier()
pred_dt = DT.predict(xv_test)
DT.score(xv_test, y_test)
0.9952762923351158
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))
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
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)
output_lable(pred_DT[0]),
output_lable(pred_GB[0]),
output_lable(pred_RF[0])))
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
news=str(input())
manual_testing(news)