How to implement NaiveBayes Classifier using sklearn

This recipe helps you implement NaiveBayes Classifier using sklearn. The naive Bayes Classification algorithm is a supervised learning algorithm and is based on the Bayes theorem.

Recipe Objective - How to implement NaiveBayes Classifier using sklearn?

Naive Bayes classifiers are a collection of classification algorithms based on Bayes' Theorem. It is not a single algorithm but a family of algorithms where all of them share a common principle, i.e. every pair of features being classified is independent of each other.

The naive Bayes Classification algorithm is a supervised learning algorithm and is based on the Bayes theorem. It comprises of two words -

Naive: It assumes that the occurrence of a specific feature is independent of the occurrence of other features.

Bayes: It is based on the Bayes theorem. Steps to implement Naive Bayes Classification using Python are as follows-

Explore Interesting IoT Project Ideas for Practice

Links for the more related projects:-

https://www.projectpro.io/projects/data-science-projects/deep-learning-projects
https://www.projectpro.io/projects/data-science-projects/neural-network-projects

Example:-

Step:1 Importing Libraries:-

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics

Let's pause and look at these imports. We have exported train_test_split which helps in randomly breaking the dataset into two parts. Here sklearn.dataset is used to import one classification-based model dataset. Also, we have exported the Gaussian Naive Bays library to build our model.

Step:2 Setting up the data

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)

Here, we have used the load_iris function to import our dataset in two list forms (X and y) and therefore kept return_X_y to be True. Further with have broken down the dataset into 2 parts, train, and test with a ratio of 3:4.

Step:3 model building

gnb = GaussianNB()

We have simply built a classification model with GaussianNB with default values.

Step 4 - Fit the model and predict for test set

gnb.fit(X_train, y_train)
y_pred= gnb.predict(X_test)

Here we have simply fit used the fit function to fit our model on X_train and y_train. Now, we are predicting the values of the X_test using our built model.

Step 5 - Printing the accuracy

print(metrics.accuracy_score(y_test, y_pred)*100)

94.66666666666667

Here we have calculated the accuracy score using the matrics library.

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.