Answered step by step
Verified Expert Solution
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
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 or ham
Expected Output the results may vary in the last decimal places
Plottery spam
Plottery ham
Pschedule spam
Pschedule hamIn : def probwordgivenclassword cls wordfrequency, classfrequency:
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'
wordfrequency dict: The dictionary containing the words frequency.
classfrequency 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
amountwordandclass wordfrequency NoneNone
pwordgivenclass Noneclssfrequency None
### END CODE HERE ###
return pwordgivenclass
In :
printfPlottery spamprobwordgivenclasslottery cls 'spam', wordfrequency wordfrequency, classf
printfPlottery hamprobwordgivenclasslottery cls 'ham', wordfrequency wordfrequency, classfre
printfPschedule spamprobwordgivenclassschedule cls 'spam', wordfrequency wordfrequency, class
printfPschedule hamprobwordgivenclassschedule cls 'ham', wordfrequency wordfrequency, classf
Expected Output the results may vary in the last decimal places
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started