World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Shashank Shanu
a year 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