<jupyter_start><jupyter_text><jupyter_code>%%capture
!pip install torch_snippets
## Setup and imports
import os
from pathlib import Path
import urllib.request
import pandas as pd
from sklearn.model_selection import train_test_split
from torch_snippets import unzip_file
if not os.path.exists('SMSSpamCollection'):
    urllib.request.urlretrieve ("https://archive.ics.uci.edu/ml/machine-learning-databases/00228/smsspamcollection.zip", "smsspamcollection.zip")
    # Extracting the dataset
    unzip_file('smsspamcollection.zip', './')
df = pd.read_csv('SMSSpamCollection', sep='\t', header=None)
df.columns = ['class','content']
trn_df, val_df = train_test_split(df, random_state=10)
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
import numpy as np

vectorizer = CountVectorizer()
vectorizer.fit(trn_df['content'])
trn = vectorizer.transform(trn_df['content']).todense()
val = vectorizer.transform(val_df['content']).todense()
words = vectorizer.get_feature_names()

nb = MultinomialNB()
nb.fit(trn, trn_df['class'])
from sklearn.metrics import confusion_matrix, classification_report
classes = ['ham','spam']

_y = nb.predict(val)
y = val_df['class']

print(confusion_matrix(y, _y))
print(classification_report(y, _y))

feature_probs = np.array(nb.feature_log_prob_)

k = 15
for i in range(len(feature_probs)):
    print(f'Top {k} features for {classes[i]}')
    _f = np.argsort(feature_probs[i])[-k:]
    print([words[j] for j in _f])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13237221494102228, 'fraction_numerical': 0.005897771952817824, 'mean_word_length': 6.8307692307692305, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '368462', 'license': 'MIT', 'n_tokens_mistral': 555, 'n_tokens_neox': 536, 'n_words': 107, 'path': 'spam-ham/step_1_first_model.ipynb', 'repo_name': 'sizhky/naive-bayes-demo', 'size': '1526'}