Skip to content

Test for Naive Bayes Classifier

Mateus de Assis Silva edited this page Jun 19, 2020 · 1 revision

In Mitchell, p.71, we see a table related to the "Play Tennis" problem. In p.190 we are given the instance ['Sunny', 'Cool','High','Strong']. We are then ask to predict if it should to play tennis or not (to classify the instance).

First of all, we implement the table of page 71:

import pandas as pd

d = {'Outlook':['Sunny','Sunny','Overcast','Rain','Rain','Rain','Overcast','Sunny','Sunny','Rain','Sunny','Overcast','Overcast','Rain'],
     'Temperature':['Hot','Hot','Hot','Mild','Cool','Cool','Cool','Mild','Cool','Mild','Mild','Mild','Hot','Mild'],
     'Humidity':['High','High','High','High','Normal','Normal','Normal','High','Normal','Normal','Normal','High','Normal','High',],
     'Wind':['Weak','Strong','Weak','Weak','Weak','Strong','Strong','Weak','Weak','Weak','Strong','Strong','Weak','Strong'],
     'PlayTennis':['No','No','Yes','Yes','Yes','No','Yes','No','Yes','Yes','Yes','Yes','Yes','No']}
df = pd.DataFrame(data= d)   
df  

Such dataframe can be seen below:

Outlook Temperature Humidity Wind PlayTennis
Sunny Hot High Weak No
Sunny Hot High Strong No
Overcast Hot High Weak Yes
Rain Mild High Weak Yes
Rain Cool Normal Weak Yes
Rain Cool Normal Strong No
Overcast Cool Normal Strong Yes
Sunny Mild High Weak No
Sunny Cool Normal Weak Yes
Rain Mild Normal Weak Yes
Sunny Mild Normal Strong Yes
Overcast Mild High Strong Yes
Overcast Hot Normal Weak Yes
Rain Mild High Strong No

The instance can be coded as:

instance = ['Sunny', 'Cool','High','Strong']

Since I already coded the Naive Bayes Classifier, we just need to apply it:

classificacao = naive_bayes(df, instance)

As we can seen, the answer is 'N'o