Lab 4 solved (1)
Lab 4 solved (1)
2022F-BSE-014
LAB # 04
SUPERVISED LEARNING (NAÏVE BAYES ALGORITHM)
OBJECTIVE
Implementing supervised learning, Naïve Bayes algorithm for training, testing and classification.
Lab Tasks:
Fig 1
1. Implement Naïve Bayes Algorithm on the above dataset in Fig 1 to predict whether the players can
play or not when the weather is overcast and the temperature is mild.
In [75]: le = preprocessing.LabelEncoder()
weather = ['sunny','sunny','overcast','rainy','rainy','rainy','overcast','sunny','sunny','rain
temperature = ['hot','hot','hot','mild','cool','cool','cool','mild','cool','mild','mild','mild
play = ['no','no','yes','yes','yes','no','yes','no','yes','yes','yes','yes','yes','no']
In [ ]:
In [76]: le = preprocessing.LabelEncoder()
weather_encoded = le.fit_transform(weather)
temperature_encoded = le.fit_transform(temperature)
play_encoded = le.fit_transform(play)
Out[78]: ▾ GaussianNB i ?
GaussianNB()
[1 0 0]
[[1 0]
[1 1]]
0.6666666666666666
In [ ]:
In [84]: le = preprocessing.LabelEncoder()
age_encoded = le.fit_transform(age)
income_encoded = le.fit_transform(income)
student_encoded = le.fit_transform(student)
credit_rating_encoded = le.fit_transform(credit_rating)
class_buyes_computer_encoded = le.fit_transform(class_buys_computer)
Out[86]: ▾ GaussianNB i ?
GaussianNB()
[1 1 0]
[[1 0]
[0 2]]
1.0
home task
In [91]: df = pd.read_csv('random_dataset.csv')
age = df['Age']
salary = df['Salary']
experience = df['Experience']
education = df['Education Level']
isEmployed = df['Is Employed']
In [92]: le = preprocessing.LabelEncoder()
age_encoded = le.fit_transform(age)
salary_encoded = le.fit_transform(salary)
experience_encoded = le.fit_transform(experience)
education_encoded = le.fit_transform(education)
isEmployed_encoded = le.fit_transform(isEmployed)
Out[95]: ▾ GaussianNB i ?
GaussianNB()
[1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 0 1 0 0 1]
[[ 4 2]
[ 4 10]]
0.7
from github:
In [ ]: