-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment4.py
80 lines (72 loc) · 3.04 KB
/
Assignment4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import pandas as pd
data = pd.read_csv('play_tennis.csv')
def prior(data, Y):
column = sorted(list(data[Y].unique()))
prior = {}
for i in column:
prior[i] = len(data[data[Y]==i])/len(data)
return prior
def likelyhood(data, X, X_label, Y, Y_label):
data = data[data[Y] == Y_label]
length = len(data)
datax = data[data[X] == X_label]
lengthx = len(datax)
return lengthx/length
def bayes(data, X, X_label, Y, Y_label):
Y_dict = prior(data, Y)
X_dict = prior(data, X)
p_of_y = Y_dict[Y_label]
p_of_x = X_dict[X_label]
return (likelyhood(data, X, X_label, Y, Y_label) * p_of_y)/p_of_x
def predict(data, X, X_label, Y, Y_label):
dic = {}
choices = sorted(data[Y].unique())
if Y_label == choices[0]:
opposite = choices[1]
if Y_label == choices[1]:
opposite = choices[0]
bayest = bayes(data, X, X_label, Y, Y_label)
dic[Y_label] = bayest
dic[opposite] = 1 - bayest
print(f'P({Y_label}|{X_label}):')
if dic[Y_label] > dic[opposite]:
answer = Y_label + ' is a higher probability in terms of playing.'
else:
answer = opposite + ' is a higher probability in terms of playing.'
return (dic, answer)
def bayes4(data, X, X_label, Y, Y_label, X2, X_label2, X3, X_label3, X4, X_label4):
Y_dict = prior(data, Y)
X_dict = prior(data, X)
X_dict2 = prior(data, X2)
X_dict3 = prior(data, X3)
X_dict4 = prior(data, X4)
p_of_y = Y_dict[Y_label]
p_of_x = X_dict[X_label]
p_of_x2 = X_dict2[X_label2]
p_of_x3 = X_dict3[X_label3]
p_of_x4 = X_dict4[X_label4]
return (likelyhood(data, X, X_label, Y, Y_label) * likelyhood(data, X2, X_label2, Y, Y_label) * likelyhood(data, X3, X_label3, Y, Y_label) * likelyhood(data, X4, X_label4, Y, Y_label) * p_of_y)/(p_of_x * p_of_x2 * p_of_x3 * p_of_x4)
def predict4(data, X, X_label, Y, Y_label, X2, X_label2, X3, X_label3, X4, X_label4):
dic = {}
choices = sorted(data[Y].unique())
if Y_label == choices[0]:
opposite = choices[1]
if Y_label == choices[1]:
opposite = choices[0]
bayest = bayes4(data, X, X_label, Y, Y_label, X2, X_label2, X3, X_label3, X4, X_label4)
dic[Y_label] = bayest
dic[opposite] = 1-bayest
print(f'P({Y_label}|{X_label, X_label2, X_label3, X_label4 }):')
if dic[Y_label] > dic[opposite]:
answer = Y_label + ' is a higher probability in terms of playing.'
else:
answer = opposite + ' is a higher probability in terms of playing.'
return dic, answer
#Tests
print(prior(data, 'Outlook'))
print(likelyhood(data, 'Outlook', 'Sunny', 'Play Tennis', 'Yes'))
print(bayes(data, 'Outlook', 'Sunny', 'Play Tennis', 'Yes'))
print(predict(data, 'Outlook', 'Sunny', 'Play Tennis', 'Yes'))
print('______________________________________________________________________________')
print(bayes4(data, 'Outlook', 'Sunny', 'Play Tennis', 'Yes','Temperature', 'Mild', 'Humidity', 'Normal','Wind', 'Weak'))
print(predict4(data, 'Outlook', 'Sunny', 'Play Tennis', 'Yes','Temperature', 'Mild', 'Humidity', 'Normal','Wind', 'Weak'))