<jupyter_start><jupyter_text>**Aim: Implement Naive Bayes classifier : Whether Example**  Step 1: Import necessary libraries.We will use preprocessing and naive bayes libraries of sklearn<jupyter_code>from sklearn import preprocessing
from sklearn.naive_bayes import GaussianNB, MultinomialNB
import pandas as pd<jupyter_output><empty_output><jupyter_text>Step 2: Prepare dataset.Create feature set for weather and temperature, and classlabel play.<jupyter_code>weather = ['Sunny', 'Sunny', 'Overcast', 'Rainy', 'Rainy','Rainy', 'Overcast',
           'Sunny', 'Sunny', 'Rainy', 'Sunny', 'Overcast', 'Overcast', 'Rainy']

temp = ['Hot','Hot','Hot','Mild','Cool','Cool','Cool','Mild',
        'Cool','Mild','Mild','Mild','Hot','Mild']

play=['No','No','Yes','Yes','Yes','No','Yes','No','Yes',
      'Yes','Yes','Yes','Yes','No']
df = pd.DataFrame(list(zip(weather,temp,play)), columns=['Weather', 'Temperature', 'Play'])
df.head()<jupyter_output><empty_output><jupyter_text>Step 3: Digitize the data set using encoding<jupyter_code>#creating labelEncoder
le = preprocessing.LabelEncoder() # apply different label

# Converting string labels into numbers.
weather_encoded=le.fit_transform(weather)
print("Weather:" ,weather_encoded)
temp_encoded=le.fit_transform(temp)
label=le.fit_transform(play)

print("Temp:",temp_encoded)
print("Play:",label)<jupyter_output>Temp: [1 1 1 2 0 0 0 2 0 2 2 2 1 2]
Play: [0 0 1 1 1 0 1 0 1 1 1 1 1 0]<jupyter_text>Step 4: Merge different features to prepare dataset<jupyter_code>#Combinig weather and temp into single listof tuples
features=tuple(zip(weather_encoded,temp_encoded))
print("Features:",features)<jupyter_output>Features: ((2, 1), (2, 1), (0, 1), (1, 2), (1, 0), (1, 0), (0, 0), (2, 2), (2, 0), (1, 2), (2, 2), (0, 2), (0, 1), (1, 2))<jupyter_text>Step 5: Train ’Naive Bayes Classifier’<jupyter_code>#Create a Classifier
model=MultinomialNB()
# Train the model using the training sets
model.fit(features,label)<jupyter_output><empty_output><jupyter_text>Step 6: Predict Output for new data<jupyter_code>#Predict Output
predicted= model.predict([[0,2]]) # 0:Overcast, 2:Mild
print("Predicted Value:", predicted)
predicted= model.predict([[0,1]]) # 0:Overcast, 1:Hot
print("Predicted Value:", predicted)
predicted= model.predict([[2,2]]) # 2:Sunny, 2:Mild

print("Predicted Value:", predicted)<jupyter_output>Predicted Value: [1]<jupyter_text>Exercise:**Manually calculate output for the following cases and compare it with system’s output.**1. Will you play if the temperature is 'Hot' and weather is 'overcast'?2. Will you play if the temperature is 'Mild' and weather is 'Sunny'?<jupyter_code>print(df.shape)
df.head(14)<jupyter_output>(14, 3)<jupyter_text>- A - Weather- B - Temp- C - Play- P(C=Yes) = 9/14- P(C=No) = 5/14- P(A=Overcast) = 4/14- P(A=Rainy) = 5/14- P(A=Sunny) = 5/14- P(B=Hot) = 4/14- P(B=Mild) = 6/14- P(B=Cool) = 4/14- P(A=Overcast/C=Yes) = 4/9- P(A=Rainy/C=Yes) = 3/9- P(A=Sunny/C=Yes) = 2/9- P(A=Overcast/C=No) = 0- P(A=Rainy/C=No) = 2/5- P(A=Sunny/C=No) = 3/5- P(B=Hot/C=Yes) = 2/9- P(B=Mild/C=Yes) = 4/9- P(B=Cool/C=Yes) = 3/9- P(B=Hot/C=No) = 2/5- P(B=Mild/C=No) = 2/5- P(B=Cool/C=No) = 1/5  Exercise-1**P(C=Yes/A=Overcast,B=Hot) = ( P(A=Overcast/C=Yes) * P(B=Hot/C=Yes) * P(C=Yes) ) / (P(A=Overcast,B=Hot))**- (0.45 * 0.23 * 0.64) / (P(A=Overcast,B=Hot))- (0.066) / (P(A=Overcast,B=Hot))Doing `marginalization` for denominator (model parameters)**P(A=Overcast,B=Hot) = P(A=Overcast,B=Hot,C=Yes) + P(A=Overcast,B=Hot,C=No)**- (0.45 * 0.23 * 0.64) + (0 * 0.4 * 0.36)- 0.066**Final Answer**- (0.066) / (P(A=Overcast,B=Hot))- (0.066) / (0.066)- 1<jupyter_code>print("Expected O/P Probability = 1, Yes")
predicted = model.predict([[0,1]])
print("Predicted Value:", predicted)<jupyter_output>Expected O/P Probability = 1, Yes
Predicted Value: [1]<jupyter_text>Exercise-2**P(C=Yes/A=Sunny,B=Mild) = ( P(A=Sunny/C=Yes) * P(B=Mild/C=Yes) * P(C=Yes) ) / (P(A=Sunny,B=Mild))**- (0.23 * 0.45 * 0.64) / (P(A=Sunny,B=Mild))- (0.066) / (P(A=Sunny,B=Mild))Doing `marginalization` for denominator (model parameters)**P(A=Sunny,B=Mild) = P(A=Sunny,B=Mild,C=Yes) + P(A=Sunny,B=Mild,C=No)**- (0.23 * 0.45 * 0.64) + (0.6 * 0.4 * 0.36)- (0.066) + (0.086)- 0.152**Final Answer**- (0.066) / (P(A=Sunny,B=Mild))- (0.066) / (0.152)- 0.43<jupyter_code>print("Expected O/P Probability = 0.43, No")
predicted= model.predict([[2,2]]) # 2:Sunny, 2:Mild
print("Predicted Value:", predicted)<jupyter_output>Expected O/P Probability = 0.43, No
Predicted Value: [1]
{'fraction_non_alphanumeric': 0.21039768940235504, 'fraction_numerical': 0.055098866918462566, 'mean_word_length': 5.894333843797856, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '298440', 'license': 'MIT', 'n_tokens_mistral': 2067, 'n_tokens_neox': 1875, 'n_words': 394, 'path': 'Lab-3/1_NB_Classifier_Whether.ipynb', 'repo_name': 'yash-a-18/002_YashAmethiya', 'size': '4501'}