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

Lab4-naive_bayes_matrix_evaluation

February 9, 2022

JAINIL PATEL 19CP067 EVALUATION MATRIX (NAIVE BAYES)


[5]: #outlook-> rainy=1, overcast=2, sunny=3
#temperatire-> hot=1, mild=2, cold=3
#humidity-> high=1, normal=2
#windy= true=1, false=2
#label-> no=1 yes=0
import math
import random
import numpy as np
import pandas as pd
dataset=[[1,1,1,2,1],
[1,1,1,1,1],
[2,1,1,2,0],
[3,2,1,2,0],
[3,3,2,2,0],
[3,3,2,1,1],
[2,3,3,1,0],
[1,2,1,2,1],
[1,3,2,2,0],
[3,2,2,2,0],
[1,2,2,1,0],
[2,2,1,1,0],
[2,1,2,2,0],
[3,2,1,1,1]]
label=[row[4] for row in dataset]
#EXTRACT OUTLOOK FOR NAIVE BAYES CLASSIFICATION
outlook=[row[0] for row in dataset]
print(label)
print(outlook)

[1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1]
[1, 1, 2, 3, 3, 3, 2, 1, 1, 3, 1, 2, 2, 3]

[7]: play=0
notplay=0
for i in range(len(label)):
if(label[i]==0):

1
play+=1
else:
notplay+=1

print("played:",play,"not played: ",notplay)


#total games played and not played

played: 9 not played: 5

[14]: #naive bayes function


def solution(day):
freq_array=np.array([[0,0],[0,0],[0,0]])
for i in range(len(label)):
if label[i]==0:
freq_array[outlook[i]-1][0]+=1
if label[i]==1:
freq_array[outlook[i]-1][1]+=1
#print(freq_array)
weather=np.array([[0.0,0.0],[0.0,0.0],[0.0,0.0]])
prob=np.zeros(3)
yes_no=np.zeros(2)
total_yes=freq_array[0][0]+freq_array[1][0]+freq_array[2][0]
total_no=freq_array[0][1]+freq_array[1][1]+freq_array[2][1]
#print(total_yes)
for i in range(len(label)):
prob[outlook[i]-1]+=1
yes_no[label[i]]+=1
#print(prob)
#print(yes_no)
prob/=len(label)
yes_no/=len(label)
#print(prob)
#print(yes_no)
for i in range(0,3):
for j in range(0,2):
if j==0:
weather[i][j]=((freq_array[i][0]/total_yes)*yes_no[0])/prob[i]
else:
weather[i][j]=((freq_array[i][1]/total_no)*yes_no[1])/prob[i]
#print(" not play= ", weather[day-1][1])
var=int(weather[day-1][1]*100)
if(var>50):
return 1
else:
return 0

2
[16]: TN=0
TP=0
FP=0
FN=0
rowx = [[1,1],[2,0],[3,0],[3,1],[1,0]]
#actual data to make confusion matrixâŘč ind1=outllok ,ind2=actually played/
,→notplayed

for x in rowx:
# store predicted class
y=solution(x[0])
#store predicted class based on probablity
if(x[1]!=y):#compare actual data with predicted data
if(y>x[1]):
FP+=1
else:
FN+=1
else:
if(y==1):
TP+=1
else:
TN+=1

[17]: print("Accuracy",(TP+TN)/(TP+TN+FP+FN))

Accuracy 0.6

[ ]:

You might also like