# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/amankalra172/Using-NLP-to-detect-fake-news/blob/main/Fake_News_Detection.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="dtvIIWbSLA2w"
# The notebook is a helper for the blog : https://www.amankalra.com/machine-learning/using-nlp-to-detect-fake-news
#
# Download the data here: https://www.kaggle.com/clmentbisaillon/fake-and-real-news-dataset
#
# For any issues, contact at <EMAIL>
#
# Happy Learning!

# + [markdown] id="JWyhgatFKoYn"
# # Importing Libraries

# + id="APDYH-QRKGQk"
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
import re
import string
import os

# + colab={"base_uri": "https://localhost:8080/"} id="7DVBLzsKNfMK" outputId="4a4849bc-706f-4354-b52a-8cc955490e2f"
from google.colab import drive
drive.mount('/content/drive')

# + id="CSUSIRzZOnQf"
os.chdir('/content/drive/MyDrive/Portfolio/Project Notebooks/Fake News detection/')

# + [markdown] id="Bt44q7SrK82v"
# # Loading the Data

# + id="CpALTjp2NZ2Q"
df_fake = pd.read_csv('Fake.csv')
df_true = pd.read_csv('True.csv')

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="_Vi0xDzUjM-j" outputId="9bca4d18-4fb1-4e2c-d523-f6408cefd8c0"
df_fake.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="L2-XIaoujQ0p" outputId="834b8e7d-cefa-4f94-8797-c8173cac4892"
df_true.head()

# + id="2TuyqsnrkV5E"
df_fake['class'] = 0
df_true['class'] = 1

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="Uv0yR8t_kguJ" outputId="0eaf9015-8570-4aea-e01d-38ee8d720b9b"
df_fake.head()  

# + colab={"base_uri": "https://localhost:8080/"} id="3D6a2-h_kjZB" outputId="8622d390-0a6a-42f2-b298-fb258e7e799a"
df_fake.shape, df_true.shape

# + colab={"base_uri": "https://localhost:8080/", "height": 359} id="VcRSGQH3kmtJ" outputId="4cdd47ac-95ba-4187-bf8e-7aecee1f5f7f"
df_merge = pd.concat([df_fake, df_true], axis =0 )
df_merge.head(10)

# + [markdown] id="PoKJXnOILfAM"
# # Data Formatting 

# + colab={"base_uri": "https://localhost:8080/"} id="EO0JmXGclSP5" outputId="54f6b3ca-e572-4fb3-8945-a7bb21ff750e"
df_merge.columns

# + id="GOZi22J7lWXB"
df = df_merge.drop(["title", "subject","date"], axis = 1)

# + colab={"base_uri": "https://localhost:8080/"} id="2GS9UhzWld_Z" outputId="f7a8e6f6-7ec7-4178-cf3d-891fa9cee225"
df.isnull().sum()

# + id="fFbdGa4TlgC5"
df = df.sample(frac= 1)

# + colab={"base_uri": "https://localhost:8080/"} id="D9xDOxNkl5LA" outputId="4df46ab9-faab-473a-b14f-b72b030f375e"
df.shape

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="q57gxTKyl65Z" outputId="eabbc2db-16bd-4e51-c2f4-d3c9b691ecaa"
df.head()

# + id="tw7zeXJRmGsS"
df.reset_index(inplace = True)
df.drop(["index"], axis = 1, inplace = True)

# + colab={"base_uri": "https://localhost:8080/"} id="9WJTcp9xmSIZ" outputId="c9193fbf-fc67-4865-9946-65209eb7bc22"
df.columns


# + id="pkDadZoOmTmn"
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('<.*?>+', '', text)
    text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
    text = re.sub('\n', '', text)
    text = re.sub('\w*\d\w*', '', text)    
    return text


# + id="DOSl-esWmlpa"
df["text"] = df["text"].apply(wordopt)

# + id="GAb_ZHdOmvXw"
x = df['text']
y = df['class']

# + id="Hc0guwg8m35K"
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)

# + colab={"base_uri": "https://localhost:8080/"} id="xephtqsHm76a" outputId="b2c65ba5-b7e3-496e-a2a9-0e4910c96f1e"
x_train.head()

# + [markdown] id="Za76U1tzLpgS"
# # Tokenizing Data
#

# + id="p3iESEJnm-va"
#comverting text to vector
from sklearn.feature_extraction.text import TfidfVectorizer

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

# + [markdown] id="rTawQ_CgLwaI"
# # Model Building

# + id="6czCUfiNnE4C"
#modeling
#logestic regression

# + colab={"base_uri": "https://localhost:8080/"} id="M0Kew_K7nzbA" outputId="7c01eadb-f1db-493e-fb65-49268c0a43ce"
from sklearn.linear_model import LogisticRegression

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

# + id="QzSDjnLSoOyG"
pred_lr=LR.predict(xv_test)

# + colab={"base_uri": "https://localhost:8080/"} id="ILoFe6hFoA06" outputId="de12a380-647a-479c-f03a-79b54b1b0065"
LR.score(xv_test, y_test)

# + colab={"base_uri": "https://localhost:8080/"} id="uh8AoaSHoJla" outputId="456616c7-e0eb-4905-de24-d5129f139b85"
print(classification_report(y_test,pred_lr))

# + id="58Sa5EBpoZnC"
#decision tree

# + colab={"base_uri": "https://localhost:8080/"} id="S-qAInmFo3_9" outputId="040f9cc7-2c02-439e-bb69-1b507b5d55d8"
from sklearn.tree import DecisionTreeClassifier

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


# + id="stRYUd7Wo9Eb"
pred_dt = DT.predict(xv_test)

# + colab={"base_uri": "https://localhost:8080/"} id="RYoee1x1o-Vy" outputId="fc7da83e-e36f-4a46-d492-a41f7076d012"
DT.score(xv_test, y_test)

# + colab={"base_uri": "https://localhost:8080/"} id="e9ukVdtzpAKi" outputId="af4e83fe-91bb-4b55-d318-476e7664e7c5"
print(classification_report(y_test, pred_dt))

# + id="09r0LRkYpCOg"
#gradient boosting classifier

# + colab={"base_uri": "https://localhost:8080/"} id="CINKA87tpEpa" outputId="aad27fb1-6b43-4a26-aec5-ce4e107ef065"
from sklearn.ensemble import GradientBoostingClassifier

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

# + id="qDaex3BnpHui"
pred_gbc = GBC.predict(xv_test)

# + colab={"base_uri": "https://localhost:8080/"} id="OuupZANWpLTp" outputId="ef7906a2-2e38-4369-f55d-d588f2150d3f"
GBC.score(xv_test, y_test)

# + colab={"base_uri": "https://localhost:8080/"} id="c2PmNIJTpOLK" outputId="b448f627-93dd-481f-c72f-ad6721c355a8"
print(classification_report(y_test, pred_gbc))

# + id="PgTB5F_ppP95"
#random forest

# + colab={"base_uri": "https://localhost:8080/"} id="NHO9OFYMpQ_R" outputId="5fc05480-61af-4414-97db-9bf23192114f"
from sklearn.ensemble import RandomForestClassifier

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

# + id="wuBMOek3pSlh"
pred_rfc = RFC.predict(xv_test)

# + colab={"base_uri": "https://localhost:8080/"} id="fF0vv2FKpUEy" outputId="cb2ed0fa-2bb5-4851-ce2a-2d5169e84ef6"
RFC.score(xv_test, y_test)

# + colab={"base_uri": "https://localhost:8080/"} id="6hn8f6UtpWNz" outputId="d89d8d06-0f60-4ccd-b233-5deaf46fe33f"
print(classification_report(y_test, pred_rfc))

StarcoderdataJupyterScriptsDedupFiltered