Site icon Machine Learning Projects

Fake news Classifier using LSTM – with source code – fun project – 2023

Machine Learning Projects

So guys in this blog we will be implementing a Fake news Classifier using LSTM. So without any further due, Let’s do it…

Checkout the video here – https://youtu.be/XcHtSSKE6PI

Step 1 – Importing libraries required for Fake news Classifier.

import re
import nltk
import numpy as np
import pandas as pd
import tensorflow as tf
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from tensorflow.keras.models import Sequential
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.text import one_hot
from sklearn.metrics import confusion_matrix,accuracy_score
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Embedding,LSTM,Dense,Dropout

nltk.download('stopwords')

Step 2 – Reading input data.

df = pd.read_csv('train.csv')
df.dropna(inplace=True)
df.reset_index(inplace=True)
df.head(10)

Step 3 – Creating X and y data.

X = df['title']
y = df['label']

Step 4 – Cleaning input data.

ps = PorterStemmer()
corpus = []

for i in range(len(X)):
    text = X[i]
    text = re.sub('[^a-zA-Z]',' ',text)
    text = text.lower()
    text = text.split()
    text = [ps.stem(t) for t in text if t not in stopwords.words('english')]
    corpus.append(' '.join(text))

Step 5 – Encoding input data.

vocab_size = 5000
sent_len = 20

one_hot_encoded = [one_hot(x,vocab_size) for x in corpus]
one_hot_encoded = pad_sequences(one_hot_encoded,maxlen=sent_len)
one_hot_encoded[0]

Step 6 – Processing X and y data.

X = np.array(one_hot_encoded)
y = np.array(y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

Step 7 – Creating the model.

no_of_output_features = 40

model = Sequential()

model.add(Embedding(vocab_size,no_of_output_features,input_length=sent_len))
model.add(Dropout(0.5))
model.add(LSTM(100))
model.add(Dropout(0.5))
model.add(Dense(1))

model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])

model.summary()

Step 8 – Training the Fake news Classifier model.

model.fit(X_train,y_train,validation_data=(X_test,y_test),batch_size=64,epochs=40)

Step 9 – Checking metrics of the model.

pred = model.predict_classes(X_test)
confusion_matrix(y_test,pred)

Step 10 – Checking the accuracy of the Fake news Classifier model.

accuracy_score(y_test,pred)

Download the Source Code and Data

Download Source Code for Fake news Classifier…

NOTE – Download train.csv from the link below and place it in the project folder.

Download Fake news Classifier Data…

Do let me know if there’s any query regarding this topic by contacting me on email or LinkedIn. I have tried my best to explain this code.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Read my previous post: SINGULAR VALUE DECOMPOSITION

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net

Exit mobile version