Download our e-book of Introduction To Python
Shashank Shanu
2 years ago
#Importing Libraries
import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn import metrics
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import confusion_matrix,classification_report,
accuracy_score
#Loading Datasets
dataset = datasets.load_iris()
#Creating Our Naive Bayes Model Using Sckit-learn
gnb = GaussianNB()
gnb.fit(dataset.data, dataset.target)
#Making Predictions
expected = dataset.target
predicted = gnb.predict(dataset.data)
#Getting Accuracy
acc = accuracy_score(expected,predicted)
print("Accuracy of the model: ", acc)
Accuracy of the model: 0.96
# Getting confusion matrix
cm = confusion_matrix(expected,predicted)
print('Confusion Matrix is:',cm, sep='\n')
Confusion Matrix is:
[[50 0 0]
[ 0 47 3]
[ 0 3 47]]
#Getting classification report
cr = classification_report(expected, predicted)
print(cr)
precision-recall f1-score support
0 1.00 1.00 1.00 50
1 0.94 0.94 0.94 50
2 0.94 0.94 0.94 50
accuracy 0.96 150
macro avg 0.96 0.96 0.96 150
weighted avg 0.96 0.96 0.96 150