Wednesday, 10 April 2019

Implement Naive Bayesian Classification (Python)

import pandas as pd
from sklearn.preprocessing import LabelEncoder,OneHotEncoder

df=pd.read_csv('naive.csv')


X=df.iloc[:,:2].values

Y=df.iloc[:,2].values

le=LabelEncoder()

X[:,0]=le.fit_transform(X[:,0])

X[:,1]=le.fit_transform(X[:,1])

print(X)
print(Y)

ohe=OneHotEncoder()

X=ohe.fit_transform(X).toarray()

from sklearn.naive_bayes import GaussianNB

ob=GaussianNB()
ob.fit(X,Y)

print(ob.predict([[0,1,1,0]]))

Dataset:

wind,humidity,temprature
weak,normal,hot
strong,high ,hot
weak,normal,mild
strong,high ,mild
weak,normal,cool
strong,normal,mild
weak,high ,mild
strong,normal,hot
strong,normal,mild
strong,normal,cool

Output:

[[1 1]
 [0 0]
 [1 1]
 [0 0]
 [1 1]
 [0 1]
 [1 0]
 [0 1]
 [0 1]
 [0 1]]
['hot' 'hot' 'mild' 'mild' 'cool' 'mild' 'mild' 'hot' 'mild' 'cool']

['mild']

No comments:

Post a Comment