0
0
ML Pythonprogramming~5 mins

Naive Bayes classifier in ML Python

Choose your learning style9 modes available
Introduction
Naive Bayes classifier helps us quickly guess the category of something based on simple probability rules. It is easy to use and works well even with small data.
Sorting emails into spam or not spam
Classifying news articles by topic
Detecting sentiment in short product reviews
Filtering messages by language
Predicting if a customer will buy a product based on past behavior
Syntax
ML Python
from sklearn.naive_bayes import GaussianNB
model = GaussianNB()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
GaussianNB is used when features are continuous numbers and assumed to follow a normal distribution.
Other Naive Bayes types exist for different data types, like MultinomialNB for counts.
Examples
Basic example using Gaussian Naive Bayes for continuous data.
ML Python
from sklearn.naive_bayes import GaussianNB
model = GaussianNB()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Using Multinomial Naive Bayes for count data like word frequencies.
ML Python
from sklearn.naive_bayes import MultinomialNB
model = MultinomialNB()
model.fit(X_train_counts, y_train)
predictions = model.predict(X_test_counts)
Sample Program
This program trains a Naive Bayes classifier on iris flower data and shows how well it predicts flower types.
ML Python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score

# Load iris flower data
iris = load_iris()
X, y = iris.data, iris.target

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Create and train the Naive Bayes model
model = GaussianNB()
model.fit(X_train, y_train)

# Predict on test data
predictions = model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, predictions)

print(f"Accuracy: {accuracy:.2f}")
print(f"Predictions: {predictions}")
OutputSuccess
Important Notes
Naive Bayes assumes features are independent, which is often not true but still works well in practice.
It is very fast to train and predict, making it good for large datasets or real-time use.
Performance can drop if features are highly correlated or if data does not fit the assumed distribution.
Summary
Naive Bayes uses simple probability to classify data quickly.
It works well for text classification and small datasets.
Easy to train and understand, but assumes feature independence.