<jupyter_start><jupyter_text>Fake News Classifier Using LSTMDataset: https://www.kaggle.com/c/fake-news/data<jupyter_code>import pandas as pd
df=pd.read_csv('Documents/FN_LSTM_train.csv')
df.head()
###Drop Nan Values
df=df.dropna()

## Get the Independent Features

X=df.drop('label',axis=1)
## Get the Dependent features
y=df['label']
X.shape
y.shape
import tensorflow as tf
from tensorflow.keras.layers import Embedding
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.text import one_hot
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dense
### Vocabulary size
voc_size=5000<jupyter_output><empty_output><jupyter_text>Onehot Representation<jupyter_code>messages=X.copy()
messages['title'][1]
messages.reset_index(inplace=True)
import nltk
import re
from nltk.corpus import stopwords
nltk.download('stopwords')
### Dataset Preprocessing
from nltk.stem.porter import PorterStemmer
ps = PorterStemmer()
corpus = []
for i in range(0, len(messages)):
    print(i)
    review = re.sub('[^a-zA-Z]', ' ', messages['title'][i])
    review = review.lower()
    review = review.split()
    
    review = [ps.stem(word) for word in review if not word in stopwords.words('english')]
    review = ' '.join(review)
    corpus.append(review)
corpus
onehot_repr=[one_hot(words,voc_size)for words in corpus] 
onehot_repr<jupyter_output><empty_output><jupyter_text>Embedding Representation<jupyter_code>sent_length=20
embedded_docs=pad_sequences(onehot_repr,padding='pre',maxlen=sent_length)
print(embedded_docs)
embedded_docs[0]
## Creating model
embedding_vector_features=40
model=Sequential()
model.add(Embedding(voc_size,embedding_vector_features,input_length=sent_length))
model.add(LSTM(100))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
print(model.summary())
len(embedded_docs),y.shape
import numpy as np
X_final=np.array(embedded_docs)
y_final=np.array(y)
X_final.shape,y_final.shape
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_final, y_final, test_size=0.33, random_state=42)<jupyter_output><empty_output><jupyter_text>Model Training<jupyter_code>### Finally Training
model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=10,batch_size=64)<jupyter_output>Epoch 1/10
192/192 [==============================] - 10s 38ms/step - loss: 0.4790 - accuracy: 0.7554 - val_loss: 0.1895 - val_accuracy: 0.9163
Epoch 2/10
192/192 [==============================] - 6s 30ms/step - loss: 0.1346 - accuracy: 0.9469 - val_loss: 0.1855 - val_accuracy: 0.9215
Epoch 3/10
192/192 [==============================] - 7s 39ms/step - loss: 0.0872 - accuracy: 0.9667 - val_loss: 0.2059 - val_accuracy: 0.9158
Epoch 4/10
192/192 [==============================] - 6s 33ms/step - loss: 0.0667 - accuracy: 0.9777 - val_loss: 0.2347 - val_accuracy: 0.9142
Epoch 5/10
192/192 [==============================] - 6s 30ms/step - loss: 0.0483 - accuracy: 0.9835 - val_loss: 0.2739 - val_accuracy: 0.9162
Epoch 6/10
192/192 [==============================] - 6s 31ms/step - loss: 0.0353 - accuracy: 0.9883 - val_loss: 0.3007 - val_accuracy: 0.9132
Epoch 7/10
192/192 [==============================] - 6s 30ms/step - loss: 0.0271 - accuracy: 0.9916 - val_loss: 0.3825 - val_accuracy: 0.914[...]<jupyter_text>Performance Metrics And Accuracy<jupyter_code>y_pred = model.predict_classes(X_test)
print(y_test)
print(y_pred)
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test,y_pred)
from sklearn.metrics import accuracy_score
accuracy_score(y_test,y_pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17727272727272728, 'fraction_numerical': 0.06657754010695187, 'mean_word_length': 6.4969939879759515, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '152228', 'license': 'MIT', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1341, 'n_words': 255, 'path': 'FND_LSTM_code.ipynb', 'repo_name': 'Puranshu/FND_project', 'size': '3740'}