06 Probability with Venn diagrams#

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

khanacademy

Probability with Venn diagrams fig 1Probability with Venn diagrams fig 2

suits = []
suit = ['A', 'J', 'K', 'Q', '2', '3', '4', '5', '6', '7', '8', '9', '10']
for i in ['Spades', 'Diamonds', 'Hearts', 'Clubs']:
    for j in suit:
        suits.append(j+'-'+i)
def P(cond, total_lst):
    cond_lst = list(filter(cond, total_lst))
    return len(cond_lst) / len(total_lst), cond_lst
# P(Jack)
p, _ = P(lambda x: x.startswith('J'), suits) 
print(p)
0.07692307692307693
# P( J And H )
p, _ = P(lambda x: x.startswith('J') and x.endswith('Hearts'), suits)
print(p)
0.019230769230769232
# P( J or H )
p, _ = P(lambda x: x.startswith('J') or x.endswith('Hearts'), suits)
print(p)
0.3076923076923077
# P( J )
p_jack, p_jack_lst = P(lambda x: x.startswith('J'), suits)
# P( H )
p_hearts, p_hearts_lst = P(lambda x: x.endswith('Hearts'), suits)
p_jack_set = set(p_jack_lst)
p_hearts_set = set(p_hearts_lst)
p_jack_interset_p_hearts = p_jack_set.intersection(p_hearts_set)
venn2(subsets=[len(p_hearts_set),
               len(p_jack_set), 
               len(p_jack_interset_p_hearts)],
      set_labels=('# Hearts', '# Jacks')
     )
<matplotlib_venn._common.VennDiagram at 0x7fedf1dccca0>
../_images/06 Probability with Venn diagrams_13_1.png