, , , ,

Classification With Nave Bayes in Scikit-Learn

admin Avatar

Nave Bayes methods are supervised learning algorithms utilizing Bayes theorem, operating under the strong assumption that all predictors are independent. This independence means the presence of one feature does not affect the presence of another within the same class. The following nave Bayes classifiers models are provided:

  • Gaussian Nave Bayes: This classifier assumes that the data from each label follows a simple Gaussian distribution.
  • Multinomial Nave Bayes: This classifier assumes features from a simple Multinomial distribution.
  • Bernoulli Nave Bayes: This classifier assumes binary features and applies Bernoulli Naive Bayes classification, for example, in text classification using a bag of words.
  • Complement Nave Bayes: This classifier addresses serious assumptions of the Multinomial Bayes classifier for imbalanced datasets.

The Nave Bayes classifier can also be utilized in the Scikit-Learn library for datasets. The following example shows how to apply GaussianNB and fit the breast_cancer dataset.

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB

data = load_breast_cancer()
feature_names = data['feature_names']
feature = data['data']
target_names = data['target_names']
target = data['target']

print(feature_names, "\n")
print(feature, "\n")
print(target_names, "\n")
print(target, "\n")

train, test, train_target, test_target = train_test_split(feature, target, test_size = 0.50, random_state = 30)

classifier = GaussianNB()
model = classifier.fit(train, train_target)
prediction = classifier.predict(test)
print(prediction)

Output

['mean radius' 'mean texture' 'mean perimeter' 'mean area'
 'mean smoothness' 'mean compactness' 'mean concavity'
 'mean concave points' 'mean symmetry' 'mean fractal dimension'
 'radius error' 'texture error' 'perimeter error' 'area error'
 'smoothness error' 'compactness error' 'concavity error'
 'concave points error' 'symmetry error' 'fractal dimension error'
 'worst radius' 'worst texture' 'worst perimeter' 'worst area'
 'worst smoothness' 'worst compactness' 'worst concavity'
 'worst concave points' 'worst symmetry' 'worst fractal dimension']

[[1.799e+01 1.038e+01 1.228e+02 ... 2.654e-01 4.601e-01 1.189e-01]
 [2.057e+01 1.777e+01 1.329e+02 ... 1.860e-01 2.750e-01 8.902e-02]
 [1.969e+01 2.125e+01 1.300e+02 ... 2.430e-01 3.613e-01 8.758e-02]
 ...
 [1.660e+01 2.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 1 0 1 1 1 1 1 0 1 1
 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1
 1 1 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 0 0 1 0 1 0 1 1 1 1 1 0 1 1 0 1 0 1 0 0
 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 0 0 0 0 0 0 1]

[0 0 1 0 1 0 1 1 1 1 1 0 1 1 0 1 0 0 1 1 1 1 0 0 1 0 1 1 1 0 1 1 0 0 1 1 1
 1 1 1 0 1 1 1 0 1 0 0 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 1
 0 0 1 1 0 0 0 1 1 1 0 1 1 1 1 0 0 0 1 1 1 0 1 1 0 1 1 1 0 0 1 1 1 1 1 0 1
 1 1 0 0 1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1
 0 0 1 1 1 0 1 1 1 0 1 1 0 1 0 0 1 1 0 1 1 1 0 1 1 0 1 1 1 0 0 1 1 0 1 1 0
 1 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 0 1 0 0 0 1 1 1 0 0 1 1 1 0 1 1 1 1 0 0 0
 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 1 0 1 1 1 1 0 0 1
 0 1 0 1 1 1 0 1 1 1 1 0 1 1 0 0 0 0 1 1 1 0 1 1 1 1]
References
  1. Hackeling, G. (2017). Mastering Machine Learning with scikit-learn, 2nd Edition. Packt Publishing Ltd.
  2. Géron, A. (2019). Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, 2nd Edition. O’Reilly Media, Inc.
  3. Tutorials Point. Scikit Learn Tutorial. Retrieved November 20, 2025, from https://www.tutorialspoint.com/.

Tagged in :

admin Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Love