0% found this document useful (0 votes)
6 views

21brs1474 Lab3

Uploaded by

chickenwinglassi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

21brs1474 Lab3

Uploaded by

chickenwinglassi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

21brs1474-lab3

August 2, 2024

[21]: import pandas as pd


from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB, CategoricalNB
from sklearn.metrics import accuracy_score, classification_report,␣
↪confusion_matrix

from sklearn.preprocessing import LabelEncoder

data = pd.read_csv('Categorical 2.csv')

X = data.drop('Target', axis=1)
y = data['Target']

label_encoders = {}
for column in X.columns:
le = LabelEncoder()
X[column] = le.fit_transform(X[column])
label_encoders[column] = le

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,␣


↪random_state=42)

gaussian_nb = GaussianNB()
gaussian_nb.fit(X_train, y_train)

categorical_nb = CategoricalNB(alpha=0.5)
categorical_nb.fit(X_train, y_train)

y_pred_gaussian = gaussian_nb.predict(X_test)
y_pred_categorical = categorical_nb.predict(X_test)

print("VIVEK NARAYANAN C V")


print("21BRS1474")

1
print("Gaussian Naive Bayes:")
print("Accuracy:", accuracy_score(y_test, y_pred_gaussian))
print("Classification Report:\n", classification_report(y_test,␣
↪y_pred_gaussian,zero_division=1))

print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred_gaussian))

print("\nCategorical Naive Bayes:")


print("Accuracy:", accuracy_score(y_test, y_pred_categorical))
print("Classification Report:\n", classification_report(y_test,␣
↪y_pred_categorical,zero_division=1))

print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred_categorical))

# Model Parameters
print("\nGaussian Naive Bayes Parameters:", gaussian_nb.get_params())
print("Categorical Naive Bayes Parameters:", categorical_nb.get_params())

# Conclusion
gaussian_accuracy = accuracy_score(y_test, y_pred_gaussian)
categorical_accuracy = accuracy_score(y_test, y_pred_categorical)

if gaussian_accuracy > categorical_accuracy:


print("\nConclusion: Gaussian Naive Bayes performed better with an accuracy␣
↪of", gaussian_accuracy)

elif categorical_accuracy > gaussian_accuracy:


print("\nConclusion: Categorical Naive Bayes performed better with an␣
↪accuracy of", categorical_accuracy)

else:
print("\nConclusion: Both models performed equally with an accuracy of",␣
↪gaussian_accuracy)

VIVEK NARAYANAN C V
21BRS1474
Gaussian Naive Bayes:
Accuracy: 0.6462191358024691
Classification Report:
precision recall f1-score support

not_recom 1.00 1.00 1.00 870


priority 0.81 0.31 0.45 873
recommend 1.00 0.00 0.00 2
spec_prior 0.81 0.60 0.69 785
very_recom 0.08 1.00 0.14 62

accuracy 0.65 2592


macro avg 0.74 0.58 0.46 2592
weighted avg 0.86 0.65 0.70 2592

2
Confusion Matrix:
[[870 0 0 0 0]
[ 0 273 0 109 491]
[ 0 0 0 0 2]
[ 0 63 0 470 252]
[ 0 0 0 0 62]]

Categorical Naive Bayes:


Accuracy: 0.904320987654321
Classification Report:
precision recall f1-score support

not_recom 1.00 1.00 1.00 870


priority 0.83 0.90 0.86 873
recommend 1.00 0.00 0.00 2
spec_prior 0.89 0.87 0.88 785
very_recom 0.67 0.06 0.12 62

accuracy 0.90 2592


macro avg 0.88 0.57 0.57 2592
weighted avg 0.90 0.90 0.90 2592

Confusion Matrix:
[[870 0 0 0 0]
[ 0 785 0 88 0]
[ 0 0 0 0 2]
[ 0 100 0 685 0]
[ 0 58 0 0 4]]

Gaussian Naive Bayes Parameters: {'priors': None, 'var_smoothing': 1e-09}


Categorical Naive Bayes Parameters: {'alpha': 0.5, 'class_prior': None,
'fit_prior': True, 'force_alpha': 'warn', 'min_categories': None}

Conclusion: Categorical Naive Bayes performed better with an accuracy of


0.904320987654321

You might also like