<jupyter_start><jupyter_text>What is the probability that an email be spam?Resource: [Link](https://github.com/Make-School-Courses/QL-1.1-Quantitative-Reasoning/blob/master/Notebooks/Conditional_Probability/Conditional_probability.ipynb)![ham_data.png](attachment:ham_data.png)  We know an email is spam, what is the probability that password be a word in it? (What is the frequency of password in a spam email?)* Dictionary of spam where its key would be unique words in spam emails and the value shows the occurance of that word<jupyter_code>spam_data = {
    "password": 2,
    "review": 1,
    "send": 3,
    "us": 3,
    "your": 3,
    "account": 1
}<jupyter_output><empty_output><jupyter_text>![math.svg](attachment:math.svg)<jupyter_code>p_password_given_spam = float(spam_data['password']) / float(sum(spam_data.values()))
print(p_password_given_spam)<jupyter_output>0.153846153846<jupyter_text>Activity: Do the above computation for each word by writing code<jupyter_code>spam = {}
ham = {}

spam_v = float(4)/float(6)
ham_v = float(2)/float(6)


def open_text(text, histogram):
    full_text = text.split()
    for word in full_text:
        if word not in histogram:
            histogram[word] = 1
        else:
            histogram[word] += 1

spam_texts = ['Send us your password', 'review us', 'Send your password', 'Send us your account']
ham_texts = ['Send us your review', 'review your password']

for text in spam_texts:
    open_text(text, spam)

for text in ham_texts:
    open_text(text, ham)

ls1 = []
ls2 = []

for i in spam:
    # obtain the probability of each word by assuming the email is spam
    p_word_given_spam = float(spam[i]) / float(sum(spam.values()))    
    
    # obtain the probability of each word by assuming the email is ham 
    p_word_given_ham =  0 if i not in ham else float(ham[i]) / float(sum(ham.values()))

    
    p_word_in_email = float(p_word_given_spam * spam_v) + float(p_word_given_ham * ham_v)
    
    # obtain the probability that for a seen word it belongs to spam email
    p_word_is_in_spam = float(p_word_given_spam * spam_v) / float(p_word_in_email)
    
    # obtain the probability that for a seen word it belongs to ham email
    p_word_is_in_ham = float(p_word_given_ham * ham_v) / float(p_word_in_email)

    print('WORD: {}\nProbability in spam: {}\nProbability in ham: {}\n'. format(i, p_word_is_in_spam, p_word_is_in_ham))<jupyter_output>WORD: account
Probability in spam: 1.0
Probability in ham: 0.0

WORD: review
Probability in spam: 0.35
Probability in ham: 0.65

WORD: us
Probability in spam: 0.763636363636
Probability in ham: 0.236363636364

WORD: Send
Probability in spam: 0.763636363636
Probability in ham: 0.236363636364

WORD: password
Probability in spam: 0.682926829268
Probability in ham: 0.317073170732

WORD: your
Probability in spam: 0.617647058824
Probability in ham: 0.382352941176
{'fraction_non_alphanumeric': 0.10052173913043479, 'fraction_numerical': 0.05008695652173913, 'mean_word_length': 4.296500920810313, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '490216', 'license': 'MIT', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1004, 'n_words': 295, 'path': 'Ham_Spam.ipynb', 'repo_name': 'timomak/quantitative-reasoning', 'size': '2875'}