fbpx
  • +91-9899714317 | +91-8810612382

Naive Bayes Classifier in Python

In machine learning, Naïve Bayes classification is a straightforward and powerful algorithm for the classification task. In this kernel, I implement Naive Bayes Classification algorithm with Python and Scikit-Learn. We build a Naive Bayes Classifier to predict whether a person makes over 50K a year.

Table of Content

  • Introduction to Naive Bayes algorithm
  • Naive Bayes algorithm intuition
  • Types of Naive Bayes algorithm
  • Applications of Naive Bayes algorithm
  • Import libraries
  • Import dataset
  • Exploratory data analysis
  • Declare feature vector and target variable
  • Split data into separate training and test set
  • Feature engineering
  • Feature scaling
  • Model training
  • Predict the results
  • Check accuracy score
  • Confusion matrix
  • Classification metrices
  • Calculate class probabilities
  • ROC – AUC
  • k-Fold Cross Validation
  • Results and conclusion

Introduction to Naive Bayes algorithm

In machine learning, Naïve Bayes classification is a straightforward and powerful algorithm for the classification task. Naïve Bayes classification is based on applying Bayes’ theorem with strong independence assumption between the features. Naïve Bayes classification produces good results when we use it for textual data analysis such as Natural Language Processing.

Naïve Bayes models are also known as simple Bayes or independent Bayes. All these names refer to the application of Bayes’ theorem in the classifier’s decision rule. Naïve Bayes classifier applies the Bayes’ theorem in practice. This classifier brings the power of Bayes’ theorem to machine learning.

Naive Bayes algorithm intuition

Naïve Bayes Classifier uses the Bayes’ theorem to predict membership probabilities for each class such as the probability that given record or data point belongs to a particular class. The class with the highest probability is considered as the most likely class. This is also known as the Maximum A Posteriori (MAP).

The MAP for a hypothesis with 2 events A and B is

MAP (A)

= max (P (A | B))

= max (P (B | A) * P (A))/P (B)

= max (P (B | A) * P (A))

Here, P (B) is evidence probability. It is used to normalize the result. It remains the same, So, removing it would not affect the result.

Naïve Bayes Classifier assumes that all the features are unrelated to each other. Presence or absence of a feature does not influence the presence or absence of any other feature.

In real world datasets, we test a hypothesis given multiple evidence on features. So, the calculations become quite complicated. To simplify the work, the feature independence approach is used to uncouple multiple evidence and treat each as an independent one.

Types of Naive Bayes algorithm

There are 3 types of Naïve Bayes algorithm. The 3 types are listed below:-

  1. Gaussian Naïve Bayes
  2. Multinomial Naïve Bayes
  3. Bernoulli Naïve Bayes

These 3 types of algorithm are explained below.

Gaussian Naïve Bayes algorithm

When we have continuous attribute values, we made an assumption that the values associated with each class are distributed according to Gaussian or Normal distribution. For example, suppose the training data contains a continuous attribute x. We first segment the data by the class, and then compute the mean and variance of x in each class. Let µi be the mean of the values and let σi be the variance of the values associated with the ith class. Suppose we have some observation value xi . Then, the probability distribution of xi given a class can be computed by the following equation –

Multinomial Naïve Bayes algorithm

With a Multinomial Naïve Bayes model, samples (feature vectors) represent the frequencies with which certain events have been generated by a multinomial (p1, . . . ,pn) where pi is the probability that event i occurs. Multinomial Naïve Bayes algorithm is preferred to use on data that is multinomially distributed. It is one of the standard algorithms which is used in text categorization classification.

Bernoulli Naïve Bayes algorithm

In the multivariate Bernoulli event model, features are independent boolean variables (binary variables) describing inputs. Just like the multinomial model, this model is also popular for document classification tasks where binary term occurrence features are used rather than term frequencies.

Applications of Naive Bayes algorithm 

Naïve Bayes is one of the most straightforward and fast classification algorithm. It is very well suited for large volume of data. It is successfully used in various applications such as :

  1. Spam filtering
  2. Text classification
  3. Sentiment analysis
  4. Recommender systems

It uses Bayes theorem of probability for prediction of unknown class.

DATA

Predict whether income exceeds $50K/yr based on census data. Also known as “Census Income” dataset.
Problem Type: Classification

<h4 class="item-title">Shreya Sinha</h4>

Shreya Sinha

Related Posts

Leave a Reply

Your email address will not be published.

J-66, 2ND FLOOR, Rajouri Garden, New Delhi, Delhi 110027

contact@analyticsquare.co.in

Get in touch!

Please enter your email to subscribe to program details


© 2022 Analytic Square All Rights Reserved by site