Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

5/12/24, 6:14 PM Car buying- Naive bayes - Colab

import pandas as pd

from google.colab import files

from sklearn.naive_bayes import CategoricalNB

from sklearn import preprocessing

from sklearn.metrics import classification_report

from sklearn.metrics import confusion_matrix

data=pd.read_csv('/content/data.csv')

objlist=data.select_dtypes(include="object").columns
len=preprocessing. LabelEncoder()

for col in objlist:


data[col]=len.fit_transform(data[col].astype(str))

print (data)

x=data.values[:,:-1]

y=data.values[:,-1]

model=CategoricalNB()

model.fit(x,y)

ypred=model.predict(x)

print('Actual Output (buys_car) : ',y)

print('\nPredicted Output (buys_car)', ypred)

cm=confusion_matrix(y,ypred)

print('Confusion Matrix \n',cm)

print('Classification Report', classification_report(y, ypred))

Unnamed: 0 Unnamed: 1 Unnamed: 2 Unnamed: 3 Unnamed: 4 Unnamed: 5


0 7 5 4 3 3 3
1 8 5 4 3 3 3
2 0 0 1 0 0 2
3 8 4 0 1 2 0
4 8 4 0 1 1 0
5 8 1 0 1 2 1
6 8 3 3 1 2 1
7 8 3 2 2 2 1
8 8 3 2 2 1 0
9 8 1 2 2 1 1
10 8 4 3 1 2 0
11 1 4 2 2 2 1
12 2 3 3 2 2 1
13 3 4 3 2 1 1
14 4 1 3 1 1 1
15 5 2 0 2 2 1
16 6 3 3 1 1 0
Actual Output (buys_car) : [3 3 2 0 0 1 1 1 0 1 0 1 1 1 1 1 0]

Predicted Output (buys_car) [3 3 2 0 0 1 1 1 1 1 0 1 1 1 1 1 0]


Confusion Matrix
[[4 1 0 0]
[0 9 0 0]
[0 0 1 0]
[0 0 0 2]]
Classification Report precision recall f1-score support

0 1.00 0.80 0.89 5


1 0.90 1.00 0.95 9
2 1.00 1.00 1.00 1
3 1.00 1.00 1.00 2

accuracy 0.94 17
macro avg 0.97 0.95 0.96 17
weighted avg 0.95 0.94 0.94 17

https://colab.research.google.com/drive/1S4jLWheirwnrUhrYeavZNwSLOBS67aul#scrollTo=nTF9_NlAvldT&printMode=true 1/2
5/12/24, 6:14 PM Car buying- Naive bayes - Colab

https://colab.research.google.com/drive/1S4jLWheirwnrUhrYeavZNwSLOBS67aul#scrollTo=nTF9_NlAvldT&printMode=true 2/2

You might also like