Naive Bayes Classification
Naive Bayes Classification
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Load dataset
data = {
'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rain', 'Rain', 'Rain', 'Overcast', 'Sunny',
'Sunny', 'Rain', 'Sunny', 'Overcast', 'Overcast', 'Rain'],
'Temperature': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool', 'Mild', 'Cool',
'Mild', 'Mild', 'Mild', 'Hot', 'Mild'],
'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal', 'High',
'Normal', 'Normal', 'Normal', 'High', 'Normal', 'High'],
'Windy': [False, True, False, False, False, True, True, False, False, False, True,
True, False, True],
'Play Tennis': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes',
'Yes', 'Yes', 'No']
}
df = pd.DataFrame(data)
# Calculate accuracy
accuracy = np.mean(y_pred == y_test)
print("Accuracy:", accuracy)
# Confusion Matrix
def confusion_matrix(y_true, y_pred, labels):
cm = np.zeros((len(labels), len(labels)), dtype=int)
label_map = {label: i for i, label in enumerate(labels)}
for i in range(len(y_true)):
cm[label_map[y_true[i]]][label_map[y_pred[i]]] += 1
return cm
labels = np.unique(y)
cm = confusion_matrix(y_test, y_pred, labels)
print("Confusion Matrix:")
print(cm)
**********************