Week 1 Programming Assignemnt Last Question (Naive Bayes Classifier)

Can someone give me the solution to the last Naive Bayes Question in the Week 1 Programming assignment? I think I have the correct solution but two of the output probabilities are not correct.

Giving you the solution would break the Code of Conduct.

1 Like

I already submitted and got it wrong that is why I wanted to check the solution. I think my solution was correct but I don’t know why its wrong for two of the cases and right for just one of them.

Check if your code uses any global variables instead of local parameters, or if if uses any hard coded dimensions. These are common mistakes

The correct solution should give you three times the correct answer.

Would you mind sharing what your output is? It would be easier for us to help you.

Moderator edit, code removed.

Attaching my solution and the output.

Do not post your code. That is not allowed on the forum. I have edited your post.

Sorry, I did not know that. Thanks!

Can you tell me what is that I did wrong?

Thanks for sharing! Personally I do not have enough information yet to help you. Do you have any suspicions on why the first and second outputs are 45.26% and 48.28%?

Hey Muhammad, what’s the value of likelihood_word_given_ham in the first example with ‘lottery’?
Change the code temporarily to print it and focus there. If not zero - it should be, if it is zero, then you have a mistake in the very last expression… speaking out of experience :slight_smile:

1 Like

Instead of opening a new thread, I would also like to post my question here since it is very much related to the OP‘s question. First of all, I passed this assignment with 100%, but something is still not clear to me. Is it possible that when calculating likelihoods, such as „likelihood_word_given_spam“ the actual way to calculate this value is AB/(B+C) and not AB as was suggested at the start of the assignment (written None*None)? I manage to get 100% score but I had additional division both in likelihood_word_given_spam and likelihood_word_given_ham expressions. For sure, one variable that needs to be used is „cumulative_product_spam“ calculated before, but I had to use more variables such as class_freq for spam and also class_freq for ham. Denominator was total number.

It is a bit hard to explain without breaking rules for posting and revealing solution, but can you please provide hints in pseudo code or a mini explanation, how likelihoods are supposed to be calculated. I cannot think of any way that would use simple multiplication of two variables as suggested in the template.

I also got 100% on the assignment as well and did use only two variables for the calculation of likelihood_word_given_spam, so the None * None suggestion was actually helpful. However, I did not adhere to the hints for the calculations of cumulative_product_spam and cumulative_product_ham.

Perhaps there are more than one way to solve this exercise, but the hints did indeed seem to confuse us one way or another.

Hi Tom, without sharing actual code, can you please explain what was the actual variable that replaced second „None“ in the template? First variable must be cumulative probability calculated in the previous code section.
I am very curious to learn what what be the this second variable used in multiplication.
In my case when calculating cumulative_product_ham(spam) I did use A / B form where A was word_freq… and B was class_freq….

Thank you!

The first variable is indeed the calculated cumulative product, though mine might be different from yours. The second variable is the class frequency.

Likelihood should be the probability of the word appearing in a spam or non-spam email. In other words, the conditional probability on the right hand side of Bayes’s theorem beside the prior probability. However, I am receiving an error in this function.

1 Like