<jupyter_start><jupyter_text>Task 1: Try the algo on Dataset1 - OneHotEncoding of features: and Train test Division 70%-30%<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score
dataset = pd.read_csv(r"./PracticeDataSets/Dataset1.csv")
print("Number of examples and feature:", dataset.shape)
dataset.head()
dataset.info()
dataset.describe()
start = "\033[1m"
end = "\033[0;0m"

print(start + "Unique Outlook values: " + end, dataset['Outlook'].unique())

print(start + "\nUnique Temperature values: " + end, dataset['Temp'].unique())

print(start + "\nUnique Humidity values: " + end, dataset['Humidity'].unique())

print(start + "\nUnique Wind values: " + end, dataset['Wind'].unique())

print(start + "\nUnique Play values " + "\033[91m (Class Label): " + end, dataset['Play'].unique())
pie_chart = dataset['Play'].value_counts().plot.pie(autopct = "%1.1f%%", radius = 2, startangle = 90)
pie_chart.set_ylabel('')
plt.show()
dummy = pd.get_dummies(dataset.iloc[:,:-1])

cols = dataset.columns.tolist()
cols.remove("Play")

dataset = dataset.drop(cols, axis = 1)
dataset = pd.concat([dummy,dataset], axis = 1)
# print("\n\nDummy :\n")
# dummy.head()
print("\n\nFinal dataset :\n")
dataset.head()
print("\nLabel\n\n")
labels = dataset['Play']
labels
#split data set into train and test sets
data_train, data_test, target_train, target_test = train_test_split(dataset.iloc[:,:-1],
                        labels, test_size = 0.30, random_state = 2)
# set Random state of model equals to your roll number
# creating intsance
gnb = GaussianNB()

# fitting the model or training
gnb.fit(data_train,target_train)
# testing
target_predict = gnb.predict(data_test)
# Accuracy
Accuracy = accuracy_score(target_test, target_predict)
print("Accuracy:", Accuracy)
# Confusion Matrix
cm = confusion_matrix(target_test, target_predict)
print("Confusion Matrix:\n")
cm
# Precision
precision = precision_score(target_test, target_predict, average=None)
print("Precision:\n")
precision
# Recall
recall = recall_score(target_test, target_predict, average=None)
print("Recall:\n")
recall<jupyter_output>Recall:<jupyter_text>**(1) What will be the value of Play, if Outlook is ’Rainy’, Temperature is ’Mild’, Humidity =’Normal’, and Wind = ’False’?**<jupyter_code>'''
Rainy = 0,1,0
Mild = 0,0,1
Normal = 0,0,1
False = 1,0
'''
q1 = [0,1,0,0,0,1,0,0,1,1,0]<jupyter_output><empty_output><jupyter_text>**(2) What will be the value of Play, if Outlook is ’Sunny’, Temeprature is ’Cool’, Humidity =’High’, and Wind = ’True’?**<jupyter_code>'''
Sunny = 0,0,1
Cool = 1,0,0
High = 1,0,0
True = 0,1
'''
q2 = [0,0,1,1,0,0,1,0,0,0,1]
features = dataset.columns.tolist()
features.remove("Play")
df = pd.DataFrame([q1,q2], columns = features)
df.head()
ans = gnb.predict(df)
print("Play predicted:\nq1: {}\nq2: {}".format(ans[0],ans[1]))<jupyter_output>Play predicted:
q1: N
q2: Y
{'fraction_non_alphanumeric': 0.1599743013170575, 'fraction_numerical': 0.028911018310311596, 'mean_word_length': 6.029345372460496, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '309711', 'license': 'MIT', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1097, 'n_words': 272, 'path': 'Lab-3/Task-1.ipynb', 'repo_name': 'yash-a-18/002_YashAmethiya', 'size': '3113'}