Created
May 3, 2019 10:49
-
-
Save firstfu/8a9f1e0be11b40dcdcd4e923c8a3ddea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
dataDic = { | |
'Weather': [ | |
'Sunny', | |
'Overcast', | |
'Rainy', | |
'Sunny', | |
'Sunny', | |
'Overcast', | |
'Rainy', | |
'Rainy', | |
'Sunny', | |
'Rainy', | |
'Sunny', | |
'Overcast', | |
'Overcast', | |
'Rainy', | |
], | |
'Play': [ | |
'No', | |
'Yes', | |
'Yes', | |
'Yes', | |
'Yes', | |
'Yes', | |
'No', | |
'No', | |
'Yes', | |
'Yes', | |
'No', | |
'Yes', | |
'Yes', | |
'No', | |
] | |
} | |
# ################################################## | |
# Bayes: p(Yes/Sunny) = P(Yes)*P(Sunny/Yes)/p(Sunny) | |
# ################################################## | |
df = pd.DataFrame(dataDic) | |
# allEvent | |
allEvent = len(df) | |
# P(Yes) | |
pYes = len(df[df['Play'] == 'Yes']) / allEvent | |
# p(Sunny) | |
pSunny = len(df[df['Weather'] == 'Sunny']) / allEvent | |
# P(Sunny/Yes) | |
pSunny_Yes = len(df[(df['Play'] == 'Yes') & (df['Weather'] == 'Sunny')]) / len( | |
df[df['Play'] == 'Yes']) | |
outcome = (pYes * pSunny_Yes) / pSunny | |
print(outcome) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment