Pages

Wednesday, 20 February 2019

Implement Naive Bayes Algorithm using Cross Validation (cross_val_score) in Python

In my previous post, I had implemented Naive Bayes algorithm using train_test_splitToday, I will implement Naive Bayes algorithm using cross validation techniques (cross_val_score). I will use 10 fold cross validation and same wine dataset. 

You can also download my Jupyter notebook containing below code of Naive Bayes cross validation implementation.

Step 1: Import the required Python libraries

from sklearn import datasets
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import cross_val_score

Step 2: Load and examine the dataset

dataset = datasets.load_wine()
dataset.feature_names
dataset.target_names
dataset.data.shape
dataset.target.shape
dataset.data[0:5]
dataset.target[0:5]

Step 3: Create an NB model

model = GaussianNB()  

Step 4: Apply 10 Fold Cross Validation and check accuracy

scores = cross_val_score(model, dataset.data, dataset.target, cv=10, scoring="accuracy")
print(scores)
meanScore = scores.mean()
print(meanScore * 100)

As this is a 10 fold cross validation, 10 scores will get displayed:
[1.         0.93333333 1.         1.         1.         0.86666667
 0.93333333 0.93333333 1.         1.        ]

To consolidate all the scores, take mean of all the scores and we get final accuracy:
96.16959064327484

No comments:

Post a Comment