C3W1_Assignment - Issue with Naive Bayes Assignment Unit Tests

Issue 1:

I think my code is correct yet i am getting this failed cases. Anybody else getting same? How to fix?

Getting error in this case also:

image
Also, i got error in assessment

Check if you have used the correct key recalls for word freq. especially for spam

if you are confident about the keys for word freq. I am more suspecting in the next line where we assign the keys to spam and ham, that is where your error is throwing this error.

I am more kind of doubting you have used an extra curly brackets { } especially related to spam keys recall.

Also whenever you share a grader output, click on the show grader output on the grade cell you scored which would give details on why you failed.

Also this assignment needs you to complete assignment and then submit it. If you are using grade up to here, even that can cause grade failure.

Also thank you for not posting codes on public post.

Let me know if it resolve your issue.

Regards
DP

It looks like you’ve made one of the common mistakes here, by missing what was said in section 4.1.1 of the instructions. The clue is that all your results are exactly 1 less than they should be. :nerd_face:

4.1.1 Handling 0 in the Product

Encountering a word that only appears in spam emails or never appears in a spam email may result in 𝑃(word∣spam)=0P(word∣spam)=0 (or the ham analog), leading to the entire product being 00. This scenario is undesirable as a single word could make the entire probability 00. To mitigate this, you will start by counting spam/ham appearances for every word from 1. By artificially assuming that there is at least one spam and one ham email with every word, you eliminate the possibility of 00 appearing in the computations.

Thanks a ton! This solved the issue. I was so puzzled. Maybe we can include this in the test cases?

1 Like

Glad to hear you got to the solution based on that hint. :nerd_face:

That’s a good idea: since this is a fairly common mistake, it would be worth adding a specific test case with a relevant error message. I’ll file an enhancement request, but no guarantees whether or when that would get added. Thanks for the suggestion!

1 Like

But the “meta” principle here is that “saving time” by not reading the instructions carefully is usually not a net savings of time. You save two minutes and then you waste an hour trying to figure out why you fail the tests. The instructions were about as explicit and clear on this point as one could hope for.

But we could write an assert in the general form of:

assert spam != 0 and ham != 0, "Please read section 4.1.1 of the instructions again"

:laughing:

3 Likes

Well, my intent has been to understand the lab/assignment question from the code itself as far as possible to develop the ability to read code. The issue is that i trusted the test cases way too much. The output of the get_word_frequency function was also confusing me as it was counting the word frequency correctly as per the example. But i guess now i should go back and read the instructions carefully if such instances occur in future.

Yes, in this case the point of the instructions was that what you would naturally think of as the correct way to count the frequencies has the 0 issue, so we have to take a somewhat counterintuitive action to avoid that issue.

If you skipped the instructions on purpose but read the code, notice that they tried to help you out by also mentioning the point in the comments in the template code:

 # If the word is not already in the dictionary, manually add it. 
 # Remember that you will start every word count as 1 both in spam and ham
 if word not in word_dict.keys():
       word_dict[word] = {"spam": None, "ham": None}

So they tried their best to help you out, because they knew that the correct answer did not come naturally.

Understood Thanks!