<jupyter_start><jupyter_text>Onehot Representation<jupyter_code>messages=X.copy()
messages['title'][0]
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)
len(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>model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=10,batch_size=64)<jupyter_output>Epoch 1/10
192/192 [==============================] - 3s 18ms/step - loss: 0.3349 - accuracy: 0.8447 - val_loss: 0.1972 - val_accuracy: 0.9180
Epoch 2/10
192/192 [==============================] - 3s 16ms/step - loss: 0.1415 - accuracy: 0.9457 - val_loss: 0.2030 - val_accuracy: 0.9140
Epoch 3/10
192/192 [==============================] - 4s 19ms/step - loss: 0.0979 - accuracy: 0.9639 - val_loss: 0.2434 - val_accuracy: 0.9220
Epoch 4/10
192/192 [==============================] - 4s 20ms/step - loss: 0.0671 - accuracy: 0.9767 - val_loss: 0.2424 - val_accuracy: 0.9162
Epoch 5/10
192/192 [==============================] - 3s 18ms/step - loss: 0.0509 - accuracy: 0.9821 - val_loss: 0.2724 - val_accuracy: 0.9107
Epoch 6/10
192/192 [==============================] - 3s 18ms/step - loss: 0.0306 - accuracy: 0.9904 - val_loss: 0.4509 - val_accuracy: 0.9072
Epoch 7/10
192/192 [==============================] - 3s 18ms/step - loss: 0.0197 - accuracy: 0.9938 - val_loss: 0.4744 - val_accuracy: 0.9074[...]<jupyter_text>Adding Dropout<jupyter_code>from tensorflow.keras.layers import Dropout
##creating model
embedding_vector_features=40
model=Sequential()
model.add(Embedding(voc_size,embedding_vector_features,input_length=sent_length))
model.add(Dropout(0.3))
model.add(LSTM(100))
model.add(Dropout(0.3))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
print(model.summary())<jupyter_output><empty_output><jupyter_text>Performances Metrics And Accuracy<jupyter_code>y_pred=model.predict_classes(X_test)
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.19055944055944055, 'fraction_numerical': 0.07400932400932402, 'mean_word_length': 7.6040100250626566, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '448294', 'license': 'MIT', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1259, 'n_words': 195, 'path': 'FakeNewsClassifierUsingLSTM.ipynb', 'repo_name': 'pallabi-sahoo/Fake_News_Classifier', 'size': '3432'}