Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 2 In the next exercise, you will implement the function to compute ( word spam ) and ( word ham ) . Since the

Exercise 2
In the next exercise, you will implement the function to compute (wordspam) and (wordham). Since the computations are the same for both types of emails, you will create a function to compute (wordclass) where class can be either spam (1) or (ham)0.
Expected Output (the results may vary in the last decimal places)
P(lottery | spam)=0.00807899461400359
P(lottery | ham)=0.0002883506343713956
P(schedule | spam)=0.008976660682226212
P(schedule | ham)=0.10294117647058823In []: def prob_word_given_class(word, cls, word_frequency, class_frequency):
Calculate the conditional probability of a given word occurring in a specific class.
Parameters:
word (str): The target word for which the probability is calculated.
cls (str): The class for which the probability is calculated, it may be 'spam' or 'ham'
word_frequency (dict): The dictionary containing the words frequency.
class_frequency (dict): The dictionary containing the class frequency.
Returns:
float: The conditional probability of the given word occurring in the specified class.
"'"!
### START CODE HERE ###
# Get the amount of times the word appears with the given class (class is stores in spam variable)
amount_word_and_class = word_frequency [None][None]
p_word_given_class = None/clss_frequency [None]
### END CODE HERE ###
return p_word_given_class
In []:
print(f"P(lottery | spam)={prob_word_given_class('lottery', cls = 'spam', word_frequency = word_frequency, class_f
print(f"P(lottery | ham)={prob_word_given_class('lottery', cls = 'ham', word_frequency = word_frequency, class_fre
print(f"P(schedule | spam)={prob_word_given_class('schedule', cls = 'spam', word_frequency = word_frequency, class
print(f"P(schedule | ham)={prob_word_given_class('schedule', cls = 'ham', word_frequency = word_frequency, class_f
Expected Output (the results may vary in the last decimal places)
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+8. Will the merger or acquisition affect specific competitors?

Answered: 1 week ago