Skip to content

Instantly share code, notes, and snippets.

@sidv1905
Last active March 30, 2021 09:26
Show Gist options
  • Save sidv1905/609cfbc58e3e5b23f4c9d85a3186b120 to your computer and use it in GitHub Desktop.
Save sidv1905/609cfbc58e3e5b23f4c9d85a3186b120 to your computer and use it in GitHub Desktop.
Naive Bayes classifier with sklearn
# Import LabelEncoder
from sklearn import preprocessing
#Generating the Gaussian Naive Bayes model
from sklearn.naive_bayes import GaussianNB
# Assign features and encoding labels
weather=['Sunny','Sunny','Overcast','Rainy','Rainy','Rainy','Overcast','Sunny','Sunny',
'Rainy','Sunny','Overcast','Overcast','Rainy']
humidity=['High','High','High','Medium','Low','Low','Low','Medium','Low','Medium','Medium','Medium','High','Medium']
batfirst=['No','No','Yes','Yes','Yes','No','Yes','No','Yes','Yes','Yes','Yes','Yes','No']
# Creating labelEncoder
le = preprocessing.LabelEncoder()
# Converting string labels into numbers.
weather_encoded=le.fit_transform(weather)
hum_encoded=le.fit_transform(humidity)
label=le.fit_transform(batfirst)
print(weather_encoded,hum_encoded,label)
#Combining weather and humidity in a single tuple as features
features=list(zip(weather_encoded,hum_encoded))
#Create a Gaussian Classifier
model = GaussianNB()
model.fit(features,label) #Train the model using training set.
print("Enter Weather and Humidtity conditions : ")
w,h=map(int, input().split())
#Predict Output
predicted= model.predict([[w,h]]) # ''' For Weather : 0:Overcast, 2:Sunny , 1:Rainy ''' For Humidity : 0:High, 2:Medium, 1:low
print(predicted) # --> [1] that means yes, the player should bat first and [0] that means No, player should bowl first.
@Savani5700
Copy link

There is some Value Error like as..................................................................................
ValueError Traceback (most recent call last)
in
29
30 print("Enter Weather and Humidtity conditions : ")
---> 31 w,h=map(int, input().split())
32
33 #Predict Output

ValueError: invalid literal for int() with base 10: '1,1'

@sidv1905
Copy link
Author

There is some Value Error like as..................................................................................
ValueError Traceback (most recent call last)
in
29
30 print("Enter Weather and Humidtity conditions : ")
---> 31 w,h=map(int, input().split())
32
33 #Predict Output

ValueError: invalid literal for int() with base 10: '1,1'

Use space " " in between the input of Weather and humidity variable. The code is written to accept a space between. not ","(comma). Hope this helps. Its expecting a space thats why throwing a error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment