<jupyter_start><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
### 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)

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)
## 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)
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)

### Finally Training
model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=5,batch_size=64)<jupyter_output>Train on 12250 samples, validate on 6035 samples
Epoch 1/5
12250/12250 [==============================] - 12s 993us/sample - loss: 0.3397 - acc: 0.8390 - val_loss: 0.1999 - val_acc: 0.9133
Epoch 2/5
12250/12250 [==============================] - 12s 944us/sample - loss: 0.1445 - acc: 0.9411 - val_loss: 0.2012 - val_acc: 0.9122
Epoch 3/5
12250/12250 [==============================] - 10s 785us/sample - loss: 0.1063 - acc: 0.9593 - val_loss: 0.2106 - val_acc: 0.9147
Epoch 4/5
12250/12250 [==============================] - 13s 1ms/sample - loss: 0.0806 - acc: 0.9691 - val_loss: 0.2607 - val_acc: 0.9145
Epoch 5/5
12250/12250 [==============================] - 12s 997us/sample - loss: 0.0630 - acc: 0.9776 - val_loss: 0.3031 - val_acc: 0.9032<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'])
# Performance Metrics And Accuracy
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.17712177121771217, 'fraction_numerical': 0.07380073800738007, 'mean_word_length': 7.059459459459459, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '375394', 'license': 'MIT', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1082, 'n_words': 188, 'path': 'LSTM with Fake News Classifier/Faker News Classifier Using LSTM.ipynb', 'repo_name': 'Hamza-Jamil121/Deep-Learning-With-Python-Programming', 'size': '2981'}