<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf

from tensorflow.keras.preprocessing.text import Tokenizer
import pandas as pd 
import numpy as np
from tensorflow.keras.preprocessing.sequence import pad_sequences
df = pd.read_csv('../input/fake-news-classification/WELFake_Dataset.csv')
df.head()
df.info()
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>delete nan values<jupyter_code>df.dropna(inplace=True)
title = df['title'].tolist()
text = df["text"].tolist()
labels = df['label'].tolist()
title[:5]<jupyter_output><empty_output><jupyter_text>Make training and testing set with 80% of training<jupyter_code>training_size = int(len(title) * 0.8)

training_sentences = title[0:training_size]
testing_sentences = title[training_size:]

training_labels = labels[0:training_size]
testing_labels = labels[training_size:]

# Make labels into numpy arrays for use with the network later
training_labels_final = np.array(training_labels)
testing_labels_final = np.array(testing_labels)

len(training_sentences),len(testing_sentences)
vocab_size = 500
embedding_dim = 16
max_length = 50
trunc_type='post'
padding_type='post'
oov_tok = "<OOV>"

tokenizer = Tokenizer(num_words = vocab_size, oov_token=oov_tok)
tokenizer.fit_on_texts(training_sentences)
word_index = tokenizer.word_index

training_sequences = tokenizer.texts_to_sequences(training_sentences)
training_padded = pad_sequences(training_sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)

testing_sequences = tokenizer.texts_to_sequences(testing_sentences)
testing_padded = pad_sequences(testing_sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)
#word_index<jupyter_output><empty_output><jupyter_text>Word index give us the references of each word {'OOV': 1, 'to': 2, 'the': 3, 'trump': 4, 'in': 5, 'of': 6, 'for': 7, 'on': 8, 'new': 9, 'video': 10, 'a': 11, 'and': 12, 'york': 13, 'times': 14,<jupyter_code>reversed_index = [ (value,key) for (key,value) in word_index.items()]<jupyter_output><empty_output><jupyter_text>we got something like this after reversed the word index [(1, 'OOV'), (2, 'to'), (3, 'the'), (4, 'trump'), (5, 'in'), (6, 'of'), (7, 'for'), (8, 'on'), (9, 'new'), (10, 'video'), (11, 'a'), (12, 'and'), (13, 'york'), (14, 'times'), (15, 'u'), (16, 'is'), (17, 's'), (18, 'with'), (19, 'says'), (20, 'at'), (21, 'hillary'),<jupyter_code>training_sequences[:5]
text = ""
for i in range(len(training_sequences[0])):
    text += reversed_index[training_sequences[0][i]][1]
    text += " "
text<jupyter_output><empty_output><jupyter_text>We will make prediction with two different features firstly a prediction with only the title then a prediction with the text to see which preform the best<jupyter_code>model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(6, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.summary()
num_epochs = 30
history = model.fit(training_padded, training_labels_final, epochs=num_epochs, validation_data=(testing_padded, testing_labels_final))
import matplotlib.pyplot as plt


def plot_graphs(history, string):
    plt.plot(history.history[string])
    plt.plot(history.history['val_'+string])
    plt.xlabel("Epochs")
    plt.ylabel(string)
    plt.legend([string, 'val_'+string])
    plt.show()

plot_graphs(history, "accuracy")
plot_graphs(history, "loss")
preds = model.predict(testing_padded)
def roundArray(number):
    ret = 0
    if number > 0.5:
        ret = 1
    return ret 

preds = preds.flatten()
result = list(map(roundArray, preds))
result[:10]
title = df['title'].tolist()
labels = df['label'].tolist()

training_size = int(len(title) * 0.8)

testing_sentences = title[training_size:]
testing_labels = labels[training_size:]
testing_labels[:10]
error = 0

for i in range(len(result)):
    res = result[i]
    true = testing_labels[i]
    if res != true:
        error += 1
        
error,len(result),(len(result)-error)/len(result)*100<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15157994773105252, 'fraction_numerical': 0.021382751247327157, 'mean_word_length': 6.753222836095764, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 27, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '631764', 'license': 'Apache-2.0', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1478, 'n_words': 311, 'path': 'fake-news-classification-88-acc.ipynb', 'repo_name': 'theyazilimci/KaggleProject', 'size': '4209'}