{ "cells": [ { "cell_type": "markdown", "id": "1bd3a4e9-5e71-4d5f-8582-c2141e186370", "metadata": { "id": "1bd3a4e9-5e71-4d5f-8582-c2141e186370" }, "source": [ "# Multinomial Naïve Bayes" ] }, { "cell_type": "markdown", "id": "08fcb7b3-fc94-4ce9-b058-0b26a3b93f7a", "metadata": { "id": "08fcb7b3-fc94-4ce9-b058-0b26a3b93f7a" }, "source": [ "This Notebook will have you working and experimenting with the Multinomial Naïve Bayes classifier. Initially, you will transform the given data in csv file to count matrix, then calculate the priors. Use those priors to compute likelyhoods according to Multinomial Naive Bayes and then classify the test data. Please note that use of `sklearn` implementations is only for the final question of the assignment, for other doubts regarding libraries you can reach out to the TAs.\n", "\n", "The dataset is about `Spam SMS`. There is 1 attribute that is the `message`, and the class label which could be `spam` or `ham`. The data is present in `spam.csv`. It contains about 5-6000 samples.\n", "For your convinience the data is already pre-processed and loaded, but I suggest you to just take a look at the code for your own knowledge, and parts vectorization is left up to you which could be easily done." ] }, { "cell_type": "code", "execution_count": 1, "id": "aa8a96d4-58f3-4360-b6ae-a02405ffdddb", "metadata": { "id": "aa8a96d4-58f3-4360-b6ae-a02405ffdddb" }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "id": "630170e6-57f8-4dae-b644-ab275a3f53a2", "metadata": { "id": "630170e6-57f8-4dae-b644-ab275a3f53a2", "tags": [] }, "source": [ "## Reading text-based data using pandas" ] }, { "cell_type": "code", "execution_count": 2, "id": "2430d4f2-e3ed-4e09-9e50-a446889d58cf", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "id": "2430d4f2-e3ed-4e09-9e50-a446889d58cf", "outputId": "4332c5cc-32fe-4efd-da0a-d8894b0df133" }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
labelmessage
0hamGo until jurong point, crazy.. Available only ...
1hamOk lar... Joking wif u oni...
2spamFree entry in 2 a wkly comp to win FA Cup fina...
3hamU dun say so early hor... U c already then say...
4hamNah I don't think he goes to usf, he lives aro...
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ], "text/plain": [ " label message\n", "0 ham Go until jurong point, crazy.. Available only ...\n", "1 ham Ok lar... Joking wif u oni...\n", "2 spam Free entry in 2 a wkly comp to win FA Cup fina...\n", "3 ham U dun say so early hor... U c already then say...\n", "4 ham Nah I don't think he goes to usf, he lives aro..." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# read file into pandas using a relative path\n", "\n", "df = pd.read_csv(\"./spam.csv\", encoding='latin-1')\n", "df.dropna(how=\"any\", inplace=True, axis=1)\n", "df.columns = ['label', 'message']\n", "\n", "df.head()" ] }, { "cell_type": "markdown", "id": "c5fbeec1-2b88-4af3-b8c6-906936938951", "metadata": { "id": "c5fbeec1-2b88-4af3-b8c6-906936938951" }, "source": [ "## Pre-processing\n", "\n", "- Our main issue with our data is that it is all in text format (strings). The classification algorithms that we usally use need some sort of numerical feature vector in order to perform the classification task. There are actually many methods to convert a corpus to a vector format. The simplest is the bag-of-words approach, where each unique word in a text will be represented by one number.\n", "\n", "- As a first step, let's write a function that will split a message into its individual words and return a list. We'll also remove very common words, ('the', 'a', etc..). To do this we will take advantage of the NLTK library. It's pretty much the standard library in Python for processing text and has a lot of useful features. We'll only use some of the basic ones here." ] }, { "cell_type": "code", "execution_count": 3, "id": "d736a35c-e849-4029-bee7-42a31f7d0b11", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "d736a35c-e849-4029-bee7-42a31f7d0b11", "outputId": "5326f654-9e60-4951-8477-2b54dfb0b7c8" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[nltk_data] Downloading package stopwords to /root/nltk_data...\n", "[nltk_data] Unzipping corpora/stopwords.zip.\n" ] } ], "source": [ "import string\n", "import nltk\n", "nltk.download('stopwords')\n", "\n", "from nltk.corpus import stopwords\n", "\n", "def text_process(mess):\n", " \"\"\"\n", " Takes in a string of text, then performs the following:\n", " 1. Remove all punctuation\n", " 2. Remove all stopwords\n", " 3. Returns a list of the cleaned text\n", " \"\"\"\n", " STOPWORDS = stopwords.words('english') + ['u', 'ü', 'ur', '4', '2', 'im', 'dont', 'doin', 'ure']\n", " # Check characters to see if they are in punctuation\n", " nopunc = [char for char in mess if char not in string.punctuation]\n", "\n", " # Join the characters again to form the string.\n", " nopunc = ''.join(nopunc)\n", " \n", " # Now just remove any stopwords\n", " return ' '.join([word for word in nopunc.split() if word.lower() not in STOPWORDS])" ] }, { "cell_type": "code", "execution_count": 4, "id": "ed807b40-7762-4968-b546-2f72817beed3", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "id": "ed807b40-7762-4968-b546-2f72817beed3", "outputId": "02ee09cc-8762-4677-ad87-50465fe8202a" }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
labelmessage
0hamGo jurong point crazy Available bugis n great ...
1hamOk lar Joking wif oni
2spamFree entry wkly comp win FA Cup final tkts 21s...
3hamdun say early hor c already say
4hamNah think goes usf lives around though
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ], "text/plain": [ " label message\n", "0 ham Go jurong point crazy Available bugis n great ...\n", "1 ham Ok lar Joking wif oni\n", "2 spam Free entry wkly comp win FA Cup final tkts 21s...\n", "3 ham dun say early hor c already say\n", "4 ham Nah think goes usf lives around though" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['message'] = df.message.apply(text_process)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 5, "id": "ad88d89f-1452-4e7a-9f9c-cb1094f890d1", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "id": "ad88d89f-1452-4e7a-9f9c-cb1094f890d1", "outputId": "31462e8c-82e6-46ab-e7fe-1b3b469f56d6" }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
labelmessage
00Go jurong point crazy Available bugis n great ...
10Ok lar Joking wif oni
21Free entry wkly comp win FA Cup final tkts 21s...
30dun say early hor c already say
40Nah think goes usf lives around though
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ], "text/plain": [ " label message\n", "0 0 Go jurong point crazy Available bugis n great ...\n", "1 0 Ok lar Joking wif oni\n", "2 1 Free entry wkly comp win FA Cup final tkts 21s...\n", "3 0 dun say early hor c already say\n", "4 0 Nah think goes usf lives around though" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['label'] = df.label.map({'ham':0, 'spam':1})\n", "df.head()" ] }, { "cell_type": "markdown", "id": "f22eddba-546a-4e97-b013-2906e90946b7", "metadata": { "id": "f22eddba-546a-4e97-b013-2906e90946b7" }, "source": [ "## Splitting the data" ] }, { "cell_type": "code", "execution_count": 6, "id": "f859993a-2b29-4baf-a168-117151ed240b", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "f859993a-2b29-4baf-a168-117151ed240b", "outputId": "1c43a691-0556-4326-fe09-172c05576f96" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X: (5572,)\n", "y: (5572,)\n", "\n", "X_train: (4179,)\n", "y_train: (4179,)\n", "\n", "X_test: (1393,)\n", "y_test: (1393,)\n", "\n" ] } ], "source": [ "# split X and y into training and testing sets \n", "from sklearn.model_selection import train_test_split\n", "\n", "X = df.message\n", "y = df.label\n", "\n", "print(f'X: {X.shape}')\n", "print(f'y: {y.shape}')\n", "print()\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)\n", "\n", "print(f'X_train: {X_train.shape}')\n", "print(f'y_train: {y_train.shape}')\n", "print()\n", "\n", "print(f'X_test: {X_test.shape}')\n", "print(f'y_test: {y_test.shape}')\n", "print()" ] }, { "cell_type": "markdown", "id": "ba6deffc-0424-4cb6-85d2-eb74f97e8cdd", "metadata": { "id": "ba6deffc-0424-4cb6-85d2-eb74f97e8cdd", "tags": [] }, "source": [ "## Helper code / Example code for Representing text as Numerical data using Sci-kit learn\n", "\n", "📌 From the scikit-learn documentation:\n", "- Text Analysis is a major application field for machine learning algorithms. However the raw data, a sequence of symbols cannot be fed directly to the algorithms themselves as most of them expect numerical feature vectors with a fixed size rather than the raw text documents with variable length.\n", "- We will use CountVectorizer to \"convert text into a matrix of token counts\":" ] }, { "cell_type": "code", "execution_count": 7, "id": "1309604c-b9c1-4910-ab60-432f7e12824b", "metadata": { "id": "1309604c-b9c1-4910-ab60-432f7e12824b" }, "outputs": [], "source": [ "# example text for model training (SMS messages)\n", "simple_train = ['call you tonight', 'Call me a cab', 'Please call me... PLEASE!']" ] }, { "cell_type": "code", "execution_count": 8, "id": "0fde24ff-b303-4526-899d-fb902eabc1d8", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0fde24ff-b303-4526-899d-fb902eabc1d8", "outputId": "a9ec619e-c16e-4688-f1ed-d85dd17acc4e" }, "outputs": [ { "data": { "text/plain": [ "array(['cab', 'call', 'me', 'please', 'tonight', 'you'], dtype=object)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# import and instantiate CountVectorizer (with the default parameters)\n", "from sklearn.feature_extraction.text import CountVectorizer\n", "\n", "vect = CountVectorizer()\n", "simple_train = vect.fit_transform(simple_train)\n", "\n", "vect.get_feature_names_out()" ] }, { "cell_type": "code", "execution_count": 9, "id": "576f6e05-6e94-45a1-96cc-3e694cc971d6", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "576f6e05-6e94-45a1-96cc-3e694cc971d6", "outputId": "d566e0ad-2994-4004-a515-41edb5de0ae2" }, "outputs": [ { "data": { "text/plain": [ "array(['cab', 'call', 'me', 'please', 'tonight', 'you'], dtype=object)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vect.get_feature_names_out()" ] }, { "cell_type": "code", "execution_count": 10, "id": "0aef831c-50f0-4723-a04a-27af774f0542", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0aef831c-50f0-4723-a04a-27af774f0542", "outputId": "a6e5da20-9914-4ee4-aa64-04bd3ea6dc36" }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 0, 0, 1, 1],\n", " [1, 1, 1, 0, 0, 0],\n", " [0, 1, 1, 2, 0, 0]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# convert sparse matrix to a dense matrix\n", "simple_train.toarray()" ] }, { "cell_type": "markdown", "id": "aa523614-ff0d-4f22-b8f2-4505e33a2460", "metadata": { "id": "aa523614-ff0d-4f22-b8f2-4505e33a2460" }, "source": [ "In this scheme, features and samples are defined as follows:\n", "\n", "- Each individual token occurrence frequency (normalized or not) is treated as a feature.\n", "- The vector of all the token frequencies for a given document is considered a multivariate sample.\n", "\n", "A corpus of documents can thus be represented by a matrix with one row per document and one column per token (e.g. word) occurring in the corpus." ] }, { "cell_type": "code", "execution_count": 11, "id": "5d0a1561-6e5c-495e-b056-fc9fe376121d", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "5d0a1561-6e5c-495e-b056-fc9fe376121d", "outputId": "2321fa9f-01c8-4d33-e5c6-658fac8f2a9a" }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
cabcallmepleasetonightyou
0010011
1111000
2011200
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ], "text/plain": [ " cab call me please tonight you\n", "0 0 1 0 0 1 1\n", "1 1 1 1 0 0 0\n", "2 0 1 1 2 0 0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# examine the vocabulary and document-term matrix together\n", "pd.DataFrame(simple_train.toarray(), columns=vect.get_feature_names_out())" ] }, { "cell_type": "markdown", "id": "02042133-9bad-4aa3-b0be-a53158e7102e", "metadata": { "id": "02042133-9bad-4aa3-b0be-a53158e7102e" }, "source": [ "### Transform Testing data into a document-term matrix (using existing / training vocabulary)\n", "\n", "- You are supposed to use the training vocabolary to make the count matrix for test data" ] }, { "cell_type": "code", "execution_count": 12, "id": "1a50c164-91ac-4861-993d-e14a2be7c6aa", "metadata": { "id": "1a50c164-91ac-4861-993d-e14a2be7c6aa" }, "outputs": [], "source": [ "simple_test = [\"please don't call me\"]" ] }, { "cell_type": "code", "execution_count": 13, "id": "30e0b5eb-d7ab-468a-86e2-89dcd0fa0184", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "30e0b5eb-d7ab-468a-86e2-89dcd0fa0184", "outputId": "0efbd037-8881-4783-fb53-b99441c6444c" }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 1, 1, 0, 0]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simple_test_dtm = vect.transform(simple_test)\n", "simple_test_dtm.toarray()" ] }, { "cell_type": "code", "execution_count": 14, "id": "9df0574c-6088-4418-92b4-d6d69c9d9d41", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 81 }, "id": "9df0574c-6088-4418-92b4-d6d69c9d9d41", "outputId": "250a73d8-a182-4b3d-8833-d12e8a835981" }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
cabcallmepleasetonightyou
0011100
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ], "text/plain": [ " cab call me please tonight you\n", "0 0 1 1 1 0 0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# examine the vocabulary and document-term matrix together\n", "pd.DataFrame(simple_test_dtm.toarray(), columns=vect.get_feature_names_out())" ] }, { "cell_type": "markdown", "id": "ec45268a-bc57-4a34-b496-b30751119403", "metadata": { "id": "ec45268a-bc57-4a34-b496-b30751119403" }, "source": [ "## Multinomial Naive Bayes Implementation\n", "\n", "- This task implements Mutlinomial Naive Bayes from scratch, we have used numpy to vectorize your code and matplotlib to show your analysis.\n", "- Below some information has given from the documentation about Multinomial Naive Bayes, this will give you some idea about using *Smoothing Priors*.\n", "- There is a sub-question for experimenting with $\\alpha > 0$, you don't have to implement it separetely, try to incomporate it in same Model Class / Function." ] }, { "cell_type": "markdown", "id": "d3c9e325-5e71-4810-83c9-a2e3b5a547b2", "metadata": { "id": "d3c9e325-5e71-4810-83c9-a2e3b5a547b2" }, "source": [ "📌 From the scikit-learn documentation:\n", "\n", "- Multinomial Naive Bayes implements the naive Bayes algorithm for multinomially distributed data, and is one of the two classic naive Bayes variants used in text classification (where the data are typically represented as word vector counts, although tf-idf vectors are also known to work well in practice).\n", "\n", "- The distribution $\\theta_y = (\\theta_{y1}, \\theta_{y2}, \\dots, \\theta_{yn})$ is parametrized by vectors for each class $y$, where $n$ is the number of features (in text classification, the size of the vocabulary) and $\\theta_{yi}$ is the probability $P(x_i|y)$ of feature appearing in a sample belonging to class.\n", "\n", "- The parameters $\\theta_y$ is estimated by a smoothed version of maximum likelihood, i.e. relative frequency counting:\n", "\n", "$$\n", "\\hat{\\theta}_{yi} = \\frac{N_{yi} + \\alpha}{N_{y} + \\alpha n}\n", "$$\n", "\n", " where $N_{yi} = \\sum_{x \\in T}{x_i}$ is the number of times feature $i $ appears in a sample of class in the training set $T$, and $N_{y} = \\sum^{n}_{i=1}{N_{yi}}$ is the total count of all features for class $y$.\n", "\n", "- The smoothing priors $\\alpha \\gt 0$ accounts for features not present in the learning samples and **prevents zero probabilities** in further computations. Setting $\\alpha = 1$ is called Laplace smoothing, while $\\alpha \\lt 1$ is called Lidstone smoothing.\n" ] }, { "cell_type": "code", "execution_count": 15, "id": "14862c11-1b3c-4314-a64e-f98a185ce394", "metadata": { "id": "14862c11-1b3c-4314-a64e-f98a185ce394" }, "outputs": [], "source": [ "\"\"\"\n", "Your code here\n", "\"\"\"\n", "from sklearn.feature_extraction.text import CountVectorizer\n", "from sklearn.metrics import accuracy_score\n" ] }, { "cell_type": "markdown", "id": "0dbcd1df-1c3d-477d-b0f6-c8c17868c797", "metadata": { "id": "0dbcd1df-1c3d-477d-b0f6-c8c17868c797" }, "source": [ "## Vectorizing Training Sample\n", "\n", "- Use the Helper code above to vectorize for training samples\n", "- Don't overthink it, its very easy to do" ] }, { "cell_type": "code", "execution_count": 16, "id": "c2561b73-fc1b-47aa-8ae9-6d1ba8ad3015", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "c2561b73-fc1b-47aa-8ae9-6d1ba8ad3015", "outputId": "09c73440-8f6b-4c60-90a0-f29dada93576" }, "outputs": [ { "data": { "text/plain": [ "array(['008704050406', '0121', '01223585236', ..., 'ûïharry', 'ûò',\n", " 'ûówell'], dtype=object)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", "Your code here\n", "\"\"\"\n", "\n", "vect = CountVectorizer()\n", "X_train = vect.fit_transform(X_train)\n", "\n", "vect.get_feature_names_out()" ] }, { "cell_type": "code", "execution_count": 17, "id": "4xY4phXLcCq1", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4xY4phXLcCq1", "outputId": "0b670820-35ff-4f9d-f60b-5ba529a5d70d" }, "outputs": [ { "data": { "text/plain": [ "(4179, 7996)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train = X_train.toarray()\n", "X_train.shape" ] }, { "cell_type": "code", "execution_count": 18, "id": "V3-pDtuhcCtZ", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 488 }, "id": "V3-pDtuhcCtZ", "outputId": "a4a03d05-0073-4e26-bec5-c63a16c56f8f" }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
00870405040601210122358523601223585334012569878902060302070836089020720694000207316241402085076972...åòitsåômorrowåôrentsìllìïìïllûªveûïharryûòûówell
00000000000...0000000000
10000000000...0000000000
20000000000...0000000000
30000000000...0000000000
40000000000...0000000000
..................................................................
41740000000000...0000000000
41750000000000...0000000000
41760000000000...0000000000
41770000000000...0000000000
41780000000000...0000000000
\n", "

4179 rows × 7996 columns

\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ], "text/plain": [ " 008704050406 0121 01223585236 01223585334 0125698789 020603 \\\n", "0 0 0 0 0 0 0 \n", "1 0 0 0 0 0 0 \n", "2 0 0 0 0 0 0 \n", "3 0 0 0 0 0 0 \n", "4 0 0 0 0 0 0 \n", "... ... ... ... ... ... ... \n", "4174 0 0 0 0 0 0 \n", "4175 0 0 0 0 0 0 \n", "4176 0 0 0 0 0 0 \n", "4177 0 0 0 0 0 0 \n", "4178 0 0 0 0 0 0 \n", "\n", " 02070836089 02072069400 02073162414 02085076972 ... åòits \\\n", "0 0 0 0 0 ... 0 \n", "1 0 0 0 0 ... 0 \n", "2 0 0 0 0 ... 0 \n", "3 0 0 0 0 ... 0 \n", "4 0 0 0 0 ... 0 \n", "... ... ... ... ... ... ... \n", "4174 0 0 0 0 ... 0 \n", "4175 0 0 0 0 ... 0 \n", "4176 0 0 0 0 ... 0 \n", "4177 0 0 0 0 ... 0 \n", "4178 0 0 0 0 ... 0 \n", "\n", " åômorrow åôrents ìll ìï ìïll ûªve ûïharry ûò ûówell \n", "0 0 0 0 0 0 0 0 0 0 \n", "1 0 0 0 0 0 0 0 0 0 \n", "2 0 0 0 0 0 0 0 0 0 \n", "3 0 0 0 0 0 0 0 0 0 \n", "4 0 0 0 0 0 0 0 0 0 \n", "... ... ... ... .. ... ... ... .. ... \n", "4174 0 0 0 0 0 0 0 0 0 \n", "4175 0 0 0 0 0 0 0 0 0 \n", "4176 0 0 0 0 0 0 0 0 0 \n", "4177 0 0 0 0 0 0 0 0 0 \n", "4178 0 0 0 0 0 0 0 0 0 \n", "\n", "[4179 rows x 7996 columns]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame(X_train, columns=vect.get_feature_names_out())" ] }, { "cell_type": "markdown", "id": "ec22e087-4e03-4428-8f47-4cb4f2ec1645", "metadata": { "id": "ec22e087-4e03-4428-8f47-4cb4f2ec1645" }, "source": [ "## Calculate Priors and Estimate Model's performance on Training Sample\n", "\n", "- Calculate priors based on Training Sample using your NB implementation\n", "- Evaluate your model's performance on Training Data ($\\alpha = 0$)" ] }, { "cell_type": "code", "execution_count": 19, "id": "1aa85aed-f38c-4acc-9c76-339d5914118d", "metadata": { "id": "1aa85aed-f38c-4acc-9c76-339d5914118d" }, "outputs": [], "source": [ "\"\"\"\n", "Your code here\n", "\"\"\"\n", "\n", "d=[[0 for i in range(len(vect.vocabulary_))] for j in range(2)]\n", "\n", "feature_names = vect.get_feature_names_out()\n", "\n", "for i in range(len(X_train)):\n", " if y_train.iloc[i]==0:\n", " arr = np.where(X_train[i]!=0)[0]\n", " for j in arr:\n", " d[0][j]+=1\n", " else:\n", " arr = np.where(X_train[i]!=0)[0]\n", " for j in arr:\n", " d[1][j]+=1\n", "\n", "counts={}\n", "counts[0]=len(np.where(y_train==0)[0])\n", "counts[1]=len(np.where(y_train==1)[0])" ] }, { "cell_type": "code", "execution_count": 20, "id": "vS8jxW8IcjUR", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "vS8jxW8IcjUR", "outputId": "2330604c-5303-4c85-f03a-61d08b164b3c" }, "outputs": [ { "data": { "text/plain": [ "(4179,)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alpha=0\n", "Y_pred=[]\n", "for i in range(len(X_train)):\n", " arr= np.where(X_train[i]!=0)[0]\n", " prob1 = counts[0]/(counts[0]+counts[1])\n", " prob2 = counts[1]/(counts[0]+counts[1])\n", " for j in arr:\n", " prob1*= (d[0][j]+alpha)/(counts[0] + alpha*X_train.shape[1])\n", " prob2*= (d[1][j]+alpha)/(counts[1] + alpha*X_train.shape[1])\n", " if prob1>prob2:\n", " Y_pred.append(0)\n", " else:\n", " Y_pred.append(1)\n", " #print(prob1,prob2)\n", "Y_pred = np.asarray(Y_pred)\n", "Y_pred.shape" ] }, { "cell_type": "code", "execution_count": 21, "id": "C35FjHKBcjWo", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "C35FjHKBcjWo", "outputId": "6c20df5e-67bf-4b76-dd7b-693a9c267fb1" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy on train data with alpha=0: 0.9932998324958124\n" ] } ], "source": [ "print(\"Accuracy on train data with alpha=0: \"+str(accuracy_score(y_train,Y_pred)))" ] }, { "cell_type": "code", "execution_count": 21, "id": "9eJ9kVDtcjZf", "metadata": { "id": "9eJ9kVDtcjZf" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "d8a6ec92-2ba4-40bf-9b5d-01825338c034", "metadata": { "id": "d8a6ec92-2ba4-40bf-9b5d-01825338c034" }, "source": [ "## Vectorizing Test Sample\n", "\n", "- Use the Training Sample vocabulary to create word count matrix for test samples\n", "- This is also shown in the Helper code" ] }, { "cell_type": "code", "execution_count": 22, "id": "a16cc470-b033-444f-a366-5b3213eba877", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "a16cc470-b033-444f-a366-5b3213eba877", "outputId": "1bae3b6b-54cb-4b1d-d13f-ebf24f1eb1ea" }, "outputs": [ { "data": { "text/plain": [ "(1393, 7996)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", "Your code here\n", "\"\"\"\n", "X_test = vect.transform(X_test)\n", "X_test = X_test.toarray()\n", "X_test.shape\n" ] }, { "cell_type": "markdown", "id": "7c02b84a-3116-4260-846d-50e6849efe48", "metadata": { "id": "7c02b84a-3116-4260-846d-50e6849efe48" }, "source": [ "## Estimate Model's performance on Test Sample\n", "\n", "- Evaluate your model's performance on Test Sample, using the Training Priors ($\\alpha = 0$)" ] }, { "cell_type": "code", "execution_count": 23, "id": "cb763cb8-5a31-4d7d-b538-b741c8cfaae8", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cb763cb8-5a31-4d7d-b538-b741c8cfaae8", "outputId": "ea63b445-0b5d-415d-a1cf-3bceb33c4ba0" }, "outputs": [ { "data": { "text/plain": [ "(1393,)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", "Your code here\n", "\"\"\"\n", "\n", "alpha=0\n", "Y_pred=[]\n", "for i in range(len(X_test)):\n", " arr= np.where(X_test[i]!=0)[0]\n", " prob1 = counts[0]/(counts[0]+counts[1])\n", " prob2 = counts[1]/(counts[0]+counts[1])\n", " for j in arr:\n", " prob1*= (d[0][j]+alpha)/(counts[0] + alpha*X_test.shape[1])\n", " prob2*= (d[1][j]+alpha)/(counts[1] + alpha*X_test.shape[1])\n", " if prob1>prob2:\n", " Y_pred.append(0)\n", " else:\n", " Y_pred.append(1)\n", " #print(prob1,prob2)\n", "Y_pred = np.asarray(Y_pred)\n", "Y_pred.shape" ] }, { "cell_type": "code", "execution_count": 24, "id": "60UuKKyDeTBj", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "60UuKKyDeTBj", "outputId": "17a9ec76-5fd8-47c2-f7eb-63b2e02eef0a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy on test data with alpha=0: 0.9533381191672649\n" ] } ], "source": [ "print(\"Accuracy on test data with alpha=0: \"+str(accuracy_score(y_test,Y_pred)))" ] }, { "cell_type": "markdown", "id": "b415868e-4b61-4a13-abb5-22124e5312bc", "metadata": { "id": "b415868e-4b61-4a13-abb5-22124e5312bc" }, "source": [ "## Select Smoothing Priors\n", "\n", "- Refactor your code to incorporate smoothing priors, select $\\alpha = 0$ for the previous estimates / sub-questions\n", "- Compare the performance with different values of $\\alpha \\gt 0$ as smoothing priors to take care of zero probabilities\n", "- You can display a Plot or Table to show the comparison." ] }, { "cell_type": "code", "execution_count": 25, "id": "a707cc0d-1f16-40a5-93ec-0366165b077c", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 453 }, "id": "a707cc0d-1f16-40a5-93ec-0366165b077c", "outputId": "ef8002dc-f000-4db6-c05a-9241412e0e89" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy on test data with alpha = 0.1 : 0.9827709978463748\n", "Accuracy on test data with alpha = 0.2 : 0.9849246231155779\n", "Accuracy on test data with alpha = 0.30000000000000004 : 0.9827709978463748\n", "Accuracy on test data with alpha = 0.4 : 0.9820531227566404\n", "Accuracy on test data with alpha = 0.5 : 0.9827709978463748\n", "Accuracy on test data with alpha = 0.6000000000000001 : 0.9827709978463748\n", "Accuracy on test data with alpha = 0.7000000000000001 : 0.9820531227566404\n", "Accuracy on test data with alpha = 0.8 : 0.9813352476669059\n", "Accuracy on test data with alpha = 0.9 : 0.9798994974874372\n", "Best accuracy on test is: 0.9849246231155779 for alpha=: 0.2\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY4AAAEGCAYAAABy53LJAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAAsTAAALEwEAmpwYAAAs4UlEQVR4nO3deZgU1fX/8fdhd0TcwIWwjIobEgQdUUED7hiNCy5RRwWjEEXcwHzVH0k0GKIhIorigooBJSJiVIhBoghxA3WQTYIoGkQQlbgjKtv5/XFrpB1mmO6hu6t7+vN6nn6murqq63Q39Om6t+655u6IiIgkq07cAYiISH5R4hARkZQocYiISEqUOEREJCVKHCIikpJ6cQeQDU2bNvXi4uK4wxARySuzZs36n7s3q7i+IBJHcXExZWVlcYchIpJXzOz9ytarqUpERFKixCEiIinJaOIws+5mtsjMFpvZtZU83trMpprZPDObbmYtEh4bYmYLzGyhmQ03M4vWT4+ec0502ymTr0FERH4sY4nDzOoCI4DjgbbA2WbWtsJmtwBj3L09MAi4Kdq3M9AFaA+0Aw4CuibsV+ruHaLbJ5l6DSIisqlMnnF0Aha7+3vuvgYYB5xcYZu2wPPR8rSExx1oBDQAGgL1gY8zGKuIiCQpk4njJ8AHCfeXResSzQV6RMunAtuY2Y7uPoOQSFZEtynuvjBhvwejZqrflTdhVWRmfcyszMzKVq5cmY7XkxPGjoXiYqhTJ/wdOzbuiESk0MTdOX410NXMZhOaopYD682sDbAv0IKQbI40s8OjfUrd/afA4dHtvMqe2N1HunuJu5c0a7bJZch5aexY6NMH3n8f3MPfPn2UPEQkuzKZOJYDLRPut4jW/cDdP3T3Hu7eERgYrfuCcPYx091XufsqYDJwaPT48ujv18DfCE1iB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jN7mlBD6GUzO87d39rC+K8i1CXbPzpuVRNRVawjVH4/sXLyesIZCYTCnae4+1wz60UooS5SJZ1xSMGIftlPA0ax8WyjCaFw5ZdmtjNwfBW7f2xm+0ZzK5yasP5fJEzWY2Ydor97uPt8d/8z8DqhkmuiF4FTzKzIzLaOnvPFal7CtsAKd98AnEdoWqvMMWa2Q9SHcQrh7GdztgFWmFl9whmHyGYpcUiheYTwi/0RAHefS2iieovQX1HVl+y1hD6SVwh9COUuB0qimfv+A1wcrb8y6vSeB6ylwsyM7v4G4Zf+a8CrwP3uPrua2O8CeprZXEIi+qaK7V4DHgfmAY+7e1k1z/u7KIaXCe+DyGapOq5ILRI1NZW4e7+4Y5HaS2ccIiKSEp1xiIhISnTGISIiKVHiEBGRlChxiIhISpQ4REQkJUocIiKSkv8PZwrfXX7CZHcAAAAASUVORK5CYII=\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "\"\"\"\n", "Your code here\n", "\"\"\"\n", "\n", "alphaArr=[]\n", "accArr=[]\n", "maxVal=0\n", "maxAlpha=0\n", "for alpha in range(1,10):\n", " alpha*=0.1\n", " Y_pred=[]\n", " for i in range(len(X_test)):\n", " arr= np.where(X_test[i]!=0)[0]\n", " prob1 = np.log(counts[0]/(counts[0]+counts[1]))\n", " prob2 = np.log(counts[1]/(counts[0]+counts[1]))\n", " for j in arr:\n", " prob1 += np.log((d[0][j]+alpha)/(counts[0] + alpha*X_test.shape[1]))\n", " prob2 += np.log((d[1][j]+alpha)/(counts[1] + alpha*X_test.shape[1]))\n", " if prob1>prob2:\n", " Y_pred.append(0)\n", " else:\n", " Y_pred.append(1)\n", " alphaArr.append(alpha)\n", " accArr.append(accuracy_score(y_test,Y_pred))\n", " print(\"Accuracy on test data with alpha = \"+str(alpha)+\" : \"+str(accuracy_score(y_test,Y_pred)))\n", " if accuracy_score(y_test,Y_pred)>maxVal:\n", " maxVal = accuracy_score(y_test,Y_pred)\n", " maxAlpha=alpha\n", "print(\"Best accuracy on test is: \"+str(maxVal)+\" for alpha=: \"+str(maxAlpha))\n", "\n", "plt.figure()\n", "plt.plot(alphaArr, accArr, 'bo-')\n", "plt.xlabel('Values of alpha')\n", "plt.ylabel('Accuracy')\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "d58051f8-67fb-4632-884b-23c719d4cae7", "metadata": { "id": "d58051f8-67fb-4632-884b-23c719d4cae7" }, "source": [ "## Comparison with Sci-kit Learn Implementation\n", "\n", "- Use sci-kit learn's `sklearn.naive_bayes.MultinomialNB` model to compare your implementation's performance\n", "- (Optional) try other classifiers from `sklearn.naive_bayes` and see if you can make them work`" ] }, { "cell_type": "code", "execution_count": 26, "id": "192a26af-2b0a-4165-9d36-b5ed774c11fb", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "192a26af-2b0a-4165-9d36-b5ed774c11fb", "outputId": "2e52a1f3-ac3f-4dbf-f0a9-752dddab5ef1" }, "outputs": [ { "data": { "text/plain": [ "0.9827709978463748" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", "Your code here\n", "\"\"\"\n", "from sklearn.naive_bayes import MultinomialNB\n", "\n", "model = MultinomialNB()\n", "model.fit(X_train,y_train)\n", "\n", "Y_pred = model.predict(X_test)\n", "accuracy_score(y_test,Y_pred)\n" ] }, { "cell_type": "code", "execution_count": 26, "id": "7h6gHgLEgx_e", "metadata": { "id": "7h6gHgLEgx_e" }, "outputs": [], "source": [] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }