Skip to content

Instantly share code, notes, and snippets.

@firstfu
Created May 3, 2019 10:49
Show Gist options
  • Save firstfu/8a9f1e0be11b40dcdcd4e923c8a3ddea to your computer and use it in GitHub Desktop.
Save firstfu/8a9f1e0be11b40dcdcd4e923c8a3ddea to your computer and use it in GitHub Desktop.
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