Naive Bayes using Python

The Python implementation of the algorithm is in the sklearn library. The whole process is much simpler. First, load the iris dataset:

from sklearn import datasets 
irisb = datasets.load_iris() 
iris = irisb['data'] 
iris.shape 

Call upon the built-in Gaussian naive Bayes estimator for a model and prediction in one step:

from sklearn.naive_bayes import GaussianNB 
gnb = GaussianNB() 
y_pred = gnb.fit(irisb.data, irisb.target).predict(irisb.data) 

Determine the accuracy of the model:

print("Number of errors out of a total %d points : %d"  
      % (irisb.data.shape[0],(irisb.target != y_pred).sum())) 
Number of errors out of a total 150 points : 6

We end up with very similar results for estimation accuracy.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.112.1