Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Artificial Intelligence(AI) & Machine Learning(ML) Training in Jaipur
Online Training - Youtube Live Class Link
0 like 0 dislike
in RTU B.Tech (CSE-VI Sem) Machine Learning Lab by Goeduhub's Expert (3.1k points)
Write a program to implement the naïve Bayesian classifier for a sample training data set stored as a .CSV file. Compute the accuracy of the classifier, considering few test data sets.

Goeduhub's Online Courses @Udemy

For Indian Students- INR 570/- || For International Students- $12.99/-


Course Name

Apply Coupon


Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon


Computer Vision with OpenCV | Deep Learning CNN Projects

Apply Coupon


Complete Machine Learning & Data Science with Python Apply Coupon


Natural Language Processing-NLP with Deep Learning in Python Apply Coupon


Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon


Complete Python Programming from scratch with Projects Apply Coupon

1 Answer

0 like 0 dislike
by Goeduhub's Expert (3.1k points)
edited by
Best answer

Naive Bayes Classifier

  • It is supervised learning algorithm used for classification based on Bayes' Theorem 
  • NBC is not just an algorithm, but a collection of many algorithms that work on the same concept, the Bayes' Theorem

Industrial Use of Naive Bayes Classifier

  1. News Categorization
  2. Spam filtering 
  3. Object and face recognition.
  4. Medical Diagnosis 
  5. Weather Prediction etc..

Type of Naive Bayes Classifier 

We have three type of naive bayes classifier 

  1. Gaussian 
  2. Multinomial 
  3. Bernoulli

Bayes' Theorem

NBS works only on the bass theorem. Let's see what the bass theorem is.

                                            P(H/E) =  P(E/H) P(H)/P(E)

  • H- Hypothesis  , E-Event / Evidence 
  • Bayes' Theorem works on conditional probability
  • We have been given that if the event has happened or the event is true, then we have to calculate the probability of Hypothesis on this event.
  • Means the chances of happening H when the event E is happened.

P(H) - It is said priori (A prior probability), Probability of H before E is happen.

P(H/E) - Posterior probability, Probability of E  after event E is true.

Note: As our question is, we have implement a naive bayes classifier on .csv file,Here we will use the naive  bayes classifier on wine data-set.

Wine Dataset Description

  • The wine dataset contains the results of a chemical analysis of wines grown in a specific area of Italy.
  • It contains total 178 samples (data), with 13 chemical analysis (features) recorded for each sample.
  • And contains three classes (our target), with no missing values.

Implementation of Algorithm

#Import important libraries 

import numpy as np

import pandas as pd

#Import dataset 

from sklearn import datasets

#Load dataset

wine = datasets.load_wine()

#print(wine)#if you want to see the data you can print data

Note:Here we have just loaded the data, you can download and load the data, you can also load it direct from sklearn .Our data dictionary is in the form of dictionary you can print and see it.

#print the names of the 13 features

print ("Features: ", wine.feature_names)

#print the label type of wine

print ("Labels: ", wine.target_names)


Note: Here we have seen our target and our features name by printing it, with this data we will train our data.




#print the wine labels (0:Class_0, 1:class_2, 2:class_2)

y=print (



  1. Here we have seen the values ​​of 14 samples for our 13 features by printing only five.
  2. On the basis of these features, our wine classes are made up of three, 0, 1, 2.

# Import train_test_split function

from sklearn.model_selection import train_test_split

# Split dataset into training set and test set

X_train, X_test, y_train, y_test = train_test_split(,, test_size=0.30,random_state=109)

Note: Split our data into training data and testing data , 70 % training data and 30 % testing data. From training data our model learn and from testing data, we can see how much our model learned.

#Import Gaussian Naive Bayes model

from sklearn.naive_bayes import GaussianNB

#Create a Gaussian Classifier

gnb = GaussianNB()

#Train the model using the training sets, y_train)

#Predict the response for test dataset

y_pred = gnb.predict(X_test)



Note: We have used the Gussian model here,and then tested with test data

#Import scikit-learn metrics module for accuracy calculation

from sklearn import metrics

# Model Accuracy

print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

#confusion matrix 

from sklearn.metrics import  confusion_matrix





  1. To check how good our model is, we have obtained the accuracy of our model.
  2. Here we calculated both confusion matrix and accuracy.
  3. We can see from the confusion matrix that our model has predict a total of 5 values ​​wrong and are correct prediction.

Click here for more programs of  RTU ML LAB

Artificial Intelligence(AI) Training in Jaipur 

Machine Learning(ML) Training in Jaipur 

Our Mentors(For AI-ML)

Sharda Godara Chaudhary

Mrs. Sharda Godara Chaudhary

An alumna of MNIT-Jaipur and ACCENTURE, Pune


Ms. Nisha

An alumna of IIT-BHU


About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © Social::   |  |