Question: 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
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
