<jupyter_start><jupyter_text>DSFM text-as-data workshop 4. NLP Deep learning: sentiment analysisCreator: [Data Science for Managers - EPFL Program](https://www.dsfm.ch)Source: [https://github.com/dsfm-org/code-bank.git](https://github.com/dsfm-org/code-bank.git)License: [MIT License](https://opensource.org/licenses/MIT). See open source [license](LICENSE) in the Code Bank repository.  OverviewIn this notebook we will try to classify positive and negative reviews. We will starts with a simple approach (TF-IDF + Naive Bayes) to create a baseline and then we will improve the model using neural networks.  Part 1: TF-IDF and Naive Bayes Q1: Load the `yelp_review.csv` dataset and store it under `df`<jupyter_code>import pandas as pd

import numpy as np
# Fix random seed for reproducibility
np.random.seed(42)

df = pd.read_csv("./data/yelp_review.csv")
df.shape<jupyter_output><empty_output><jupyter_text>Q2: Let's start by simplifing a bit the problem by transforming the `stars` column into a boolean feature composed of positive and negative sentiments.By looking at the `median` and `mean` value of the `stars` columns, pick a good threshold and generate the `target` column. All reviews with a stars rating above such threshold will be targeted as `1` and the rest will be set as `0`.<jupyter_code>print("Stars median: ", # YOUR CODE HERE #)
print("Stars mean: ", # YOUR CODE HERE #)
df['target'] = # YOUR CODE HERE #
df.head(2)<jupyter_output><empty_output><jupyter_text>Q3: Look at the target distribution. Is that even distributed? What's the baseline we will need to beat?<jupyter_code># YOUR CODE HERE #<jupyter_output><empty_output><jupyter_text>Q4: Split the DataFrame into a `train_df` (80%) and `test_df` (20%). You can use scikit-learn `train_test_split`.<jupyter_code>from sklearn.model_selection import train_test_split
train_df, test_df = # YOUR CODE HERE #

print("train_df.shape: ", train_df.shape)
print("test_df.shape: ", test_df.shape)<jupyter_output><empty_output><jupyter_text>Q5: Fit a `TfidfVectorizer` from scikit-learn with `max_features=500` on `train_df['text']` and store it under `X_train`<jupyter_code>from sklearn.feature_extraction.text import TfidfVectorizer

tfidf_vectorizer = # YOUR CODE HERE #

X_train = # YOUR CODE HERE #<jupyter_output><empty_output><jupyter_text>Q6: Fit the `test_df` and store it under `X_test`<jupyter_code>X_test = # YOUR CODE HERE #<jupyter_output><empty_output><jupyter_text>Q7: Get `y_train` and `y_test` target as numpy array<jupyter_code>y_train = # YOUR CODE HERE #
y_test = # YOUR CODE HERE #<jupyter_output><empty_output><jupyter_text>Q8: Apply Naive Bayes and predict the `y_predicted` values using [GaussianNB](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html)<jupyter_code>from sklearn.naive_bayes import GaussianNB
import numpy as np

gnb = # YOUR CODE HERE #

y_predicted = # YOUR CODE HERE #<jupyter_output><empty_output><jupyter_text>Q9: Display the accuracy as well as the roc_auc_score<jupyter_code>from sklearn.metrics import accuracy_score

print(f"Accuracy score: # YOUR CODE HERE #")

from sklearn.metrics import roc_auc_score

print(f"Roc-auc score: # YOUR CODE HERE #")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10377942998760843, 'fraction_numerical': 0.007125154894671623, 'mean_word_length': 6.1280353200883, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 5, 'lorem ipsum': 0, 'www.': 1, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '315003', 'license': 'MIT', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1070, 'n_words': 328, 'path': 'tutorials/text-as-data/exercises/4-nlp-deep-learning-exercises.ipynb', 'repo_name': 'dsfm-org/code-bank', 'size': '3228'}