Step-1:Assigning features and label variables
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']
Step-2: Encoding the data
[95]: # Import LabelEncoder
from sklearn import preprocessing
#creating labelEncoder
le = preprocessing.LabelEncoder()
# Converting string labels into numbers.
wheather_encoded=le.fit_transform(weather)
print(wheather_encoded)
[2 2 0 1 1 1 0 2 2 1 2 0 0 1]
[97]: # Converting string labels into numbers
temp_encoded=le.fit_transform(temp)
label=le.fit_transform(play)
print("Temp:",temp_encoded)
print("Play:",label)
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]
Step-3:Combinig weather and temp into single listof tuples
[102]: features=zip(wheather_encoded,temp_encoded)
final=list(features)
[103]: final
[103]: [(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)]
Step-4: Fitting the model and predicting the classfier
[104]: #Import Gaussian Naive Bayes model
from sklearn.naive_bayes import GaussianNB
#Create a Gaussian Classifier
model = GaussianNB()
# Train the model using the training sets
model.fit(final,label)
#Predict Output
predicted= model.predict([[0,2]]) # 0:Overcast, 2:Mild
print("Predicted Value:", predicted)
Predicted Value: [1]
#Here, 1 indicates that players can ‘play’