<jupyter_start><jupyter_text>Results<jupyter_code>import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt  
import seaborn as sns
import matplotlib.pyplot as plt

import tabletext
f1_lr = 0.96
auc_lr = 0.99
accuracy_lr = 0.96
f1_nb = 0.88
auc_nb = 0.95
accuracy_nb = 0.88
f1_xgb = 0.96
auc_xgb = 0.99
accuracy_xgb = 0.96
f1_cnn = 0.97
auc_cnn = 0.99
accuracy_cnn = 0.97
accuracies = {'Classifier': ['Logistic Regression', 'Naive Bayes', 'XGBoost', 'CNN',
                     ],
        'Accuracy': [accuracy_lr, accuracy_nb, accuracy_xgb, accuracy_cnn,
               ]
        }
f1s = {'Classifier': ['Logistic Regression', 'Naive Bayes', 'XGBoost', 'CNN',
                     ],
        'F1': [f1_lr, f1_nb, f1_xgb, f1_cnn,
               ]
        }
aucs = {'Classifier': ['Logistic Regression', 'Naive Bayes', 'XGBoost', 'CNN',
                     ],
        'AUC': [auc_lr, auc_nb, auc_xgb, auc_cnn,
               ]
        }
df_accuracy = pd.DataFrame(accuracies, columns = ['Classifier', 'Accuracy'])
df_f1 = pd.DataFrame(f1s, columns = ['Classifier', 'F1'])
df_auc = pd.DataFrame(aucs, columns = ['Classifier', 'AUC'])<jupyter_output><empty_output><jupyter_text>*****Results*****<jupyter_code>sns.set(style="whitegrid")

# Initialize the matplotlib figure
f, ax = plt.subplots(figsize=(9, 9))
ax = sns.barplot(x = "Accuracy", y = "Classifier", data = df_accuracy, label = "Classifier", palette = "Blues_d", orient='h')
# Annotate every bar           
for p in ax.patches:
    width = p.get_width()
    plt.text(0.05 + p.get_width(), p.get_y() + 0.55 * p.get_height(), '{:1.2f}'.format(width), ha = 'center', va = 'center')
sns.set(style="whitegrid")

# Initialize the matplotlib figure
f, ax = plt.subplots(figsize=(9, 9))
ax = sns.barplot(x = "F1", y = "Classifier", data = df_f1, label = "Classifier", palette = "Blues_d", orient='h')
# Annotate every bar           
for p in ax.patches:
    width = p.get_width()
    plt.text(0.05 + p.get_width(), p.get_y() + 0.55 * p.get_height(), '{:1.2f}'.format(width), ha = 'center', va = 'center')
sns.set(style="whitegrid")

# Initialize the matplotlib figure
f, ax = plt.subplots(figsize=(9, 9))
ax = sns.barplot(x = "AUC", y = "Classifier", data = df_auc, label = "Classifier", palette = "Blues_d", orient='h')
# Annotate every bar          
for p in ax.patches:
    width = p.get_width()
    plt.text(0.05 + p.get_width(), p.get_y() + 0.55 * p.get_height(), '{:1.2f}'.format(width), ha = 'center', va = 'center')<jupyter_output><empty_output><jupyter_text>*****Discussion***** It is very difficult to say which model performs better based on these results, as they are very similar. The CNN slightly outperforms the Logistic Regression in terms of accuracy and F1-score. However, the training time is higher for convolutional neural networks, and although I did little optimization here, in general this step also require more know-how and effort. What little may be gained in terms of predictive accuracy using convolutional neural networks is definitely lost in terms of simplicity and interpretability when compared to the logistic regression. *****Predictions*****<jupyter_code>df = pd.read_csv('data/test.csv')
df.head()
selection = df.at[1,'text']
print(selection)
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer(tokenizer = lambda x: x,
                        preprocessor = lambda x: x, 
                        min_df = 5, max_df = 0.75, max_features = 25000, ngram_range = (1,3)) 
train_features = np.load('train_dataset_features.npy', allow_pickle=True)
tfidf_vectorizer.fit_transform(train_features) 
import nltk
wpt = nltk.WordPunctTokenizer()
tokenized_selection = [wpt.tokenize(text) for text in selection]
x_test_tfidf = tfidf_vectorizer.transform(tokenized_selection)<jupyter_output><empty_output><jupyter_text>**Logistic Regression**<jupyter_code>%%timeit
import joblib
# load, no need to initialize the loaded model
loaded_logreg = joblib.load("./logreg.joblib")
%%timeit
pred = loaded_logreg.predict(x_test_tfidf)
print(pred)<jupyter_output>[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ...[...]<jupyter_text>**CNN**<jupyter_code>loaded_cnn = tensorflow.keras.models.load_model('cnn')
x_test_tfidf = x_test_tfidf.item()
x_test_tfidf = x_test_tfidf.todense()
x_test_tfidf = np.expand_dims(x_test_tfidf, axis = 2)
%%timeit
pred = loaded_cnn.predict(x_test_tfidf)
print(pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16568265682656827, 'fraction_numerical': 0.07896678966789668, 'mean_word_length': 2.8804581245526126, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '629200', 'license': 'MIT', 'n_tokens_mistral': 2505, 'n_tokens_neox': 2149, 'n_words': 717, 'path': 'Results.ipynb', 'repo_name': 'lauravoicu/fake-news', 'size': '5420'}