14 Conditional probability and independence#

%%html
<iframe width="700" height="400" src="https://www.youtube.com/embed/pIfpHdGVwLU/" frameborder="0" allowfullscreen></iframe>
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
import seaborn as sns

khanacademy

Conditional probability and independence fig 1

df = DataFrame({
    'On time': [167, 115, 40, 8],
    'Delayed': [3, 5, 15, 12]
}, index=['Sunny', 'Cloudy', 'Rainy', 'Snowy'])
df['Total'] = df['On time'] + df['Delayed']
df = df.append(df.sum().rename('Total'))
/tmp/ipykernel_7685/333771010.py:1: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
  df = df.append(df.sum().rename('Total'))
df[-1:]
On time Delayed Total
Total 330 35 365
#P(Delayed)
df[-1:]['Delayed'] / df[-1:]['Total']
Total    0.09589
dtype: float64
df[-2:-1]
On time Delayed Total
Snowy 8 12 20
#P(Delayed | Snowy)
df[-2:-1]['Delayed'] / df[-2:-1]['Total']
Snowy    0.6
dtype: float64