{ "cells": [ { "cell_type": "markdown", "id": "b4454654", "metadata": {}, "source": [ "### Machine Learning: Naive Bayes classifier for categorical data from scratch in Python\n", "$P(C_k|\\textbf{x})\\propto P(C_k) P(\\textbf{x}|C_k)$\n", "###### by Hamed Shah-Hosseini\n", "Explanation at: https://www.pinterest.com/HamedShahHosseini/\n", "
https://github.com/ostad-ai/Machine-Learning" ] }, { "cell_type": "code", "execution_count": 1, "id": "50b8a8b6", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from collections import defaultdict" ] }, { "cell_type": "code", "execution_count": 28, "id": "31065e4c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of samples in dataset:14\n" ] } ], "source": [ "# Reading dataset\n", "weather=pd.read_csv('./datasets/weather.csv')\n", "print(f'Number of samples in dataset:{len(weather)}')" ] }, { "cell_type": "code", "execution_count": 29, "id": "d8583f14", "metadata": {}, "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", "
OutlookTemperatureHumidityWindyPlay
0SunnyHotHighFalseNo
1SunnyHotHighTrueNo
2OvercastHotHighFalseYes
3RainyMildHighFalseYes
4RainyCoolNormalFalseYes
\n", "
" ], "text/plain": [ " Outlook Temperature Humidity Windy Play\n", "0 Sunny Hot High False No\n", "1 Sunny Hot High True No\n", "2 Overcast Hot High False Yes\n", "3 Rainy Mild High False Yes\n", "4 Rainy Cool Normal False Yes" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The first five rows of dataset\n", "weather.head()" ] }, { "cell_type": "code", "execution_count": 30, "id": "37b96427", "metadata": {}, "outputs": [], "source": [ "# naive Bayes for categorial data\n", "#alpha is the smoothing parameter\n", "class NBCategorial:\n", " def __init__(self,alpha=.5):\n", " self.alpha=alpha\n", " def fit(self,data_frame,class_column):\n", " df=data_frame.astype(str)\n", " self.groups=df.groupby(class_column)\n", " self.Ncats=dict() # no. of unique categories in each feature\n", " self.features=df.columns.to_list()\n", " self.features.remove(class_column)\n", " self.probs={key:defaultdict(dict) for key in self.features}\n", " for feature in self.features:\n", " self.Ncats[feature]=len(df[feature].unique())\n", " self.priors=(self.groups.size()/len(df)).to_dict() #prior\n", " for cName,cData in self.groups: \n", " for feature in self.features:\n", " result=pd.value_counts(cData[feature]).to_dict()\n", " num=len(cData[feature])\n", " for value in df[feature].unique():\n", " if value in result.keys():\n", " self.probs[feature][value][cName]=\\\n", " (result[value]+self.alpha)/\\\n", " (num+self.alpha*self.Ncats[feature])\n", " else: # value (category) is not in cData\n", " self.probs[feature][value][cName]=self.alpha/\\\n", " (num+self.alpha*self.Ncats[feature]) \n", " def predict(self,query,with_priors=True):\n", " probs_classes=dict() \n", " for cName,cData in self.groups:\n", " if with_priors:\n", " prob=self.priors[cName]\n", " else:\n", " prob=1.\n", " for q,feature in zip(query,self.features):\n", " if q in self.probs[feature].keys():\n", " prob*=self.probs[feature][q][cName] \n", " else: # haven't seen such a category\n", " num=len(cData[feature])\n", " prob*=self.alpha/(num+\\\n", " self.alpha*(1+self.Ncats[feature]))\n", " probs_classes[cName]=prob\n", " predict=max(probs_classes,key=probs_classes.get)\n", " return predict,probs_classes" ] }, { "cell_type": "code", "execution_count": 31, "id": "6db8fd5b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy:% 92.86\n" ] } ], "source": [ "# Testing with training data, not very usual:)\n", "# That's because the dataset is so small\n", "classCol='Play'\n", "Ns=weather.shape[0] #number of samples\n", "columns=weather.columns.to_list()\n", "classCol_inx=columns.index(classCol)\n", "nbayes=NBCategorial()\n", "nbayes.fit(weather,classCol)\n", "accuracy=0.\n", "for n in range(Ns):\n", " query=weather.astype(str).iloc[n].to_list()\n", " #remove the value of class column\n", " class_q=query.pop(classCol_inx) \n", " accuracy+=nbayes.predict(query)[0]==class_q\n", "accuracy/=Ns \n", "print('Accuracy:%',round(100*accuracy,2))" ] }, { "cell_type": "code", "execution_count": 23, "id": "b83998be", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Predicted class of ['Sunny', 'Mild', 'High', 'False']: No\n" ] } ], "source": [ "# Example: testing with a given query\n", "classCol='Play'\n", "query=['Sunny','Mild','High','False']\n", "nbayes=NBCategorial()\n", "nbayes.fit(weather,classCol)\n", "predict,probs=nbayes.predict(query)\n", "print(f'Predicted class of {query}: {predict}')" ] }, { "cell_type": "code", "execution_count": null, "id": "da82c766", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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 }