Hi,
I’m getting this error when testing my function train_naive_bayes
I have already got the expected output.
My labID is aashcwtc
Thank you!
Hi,
I’m getting this error when testing my function train_naive_bayes
Thank you!
Usually, a sanity test is shown in the notebook along with the expected values. The test_<method>
contains more test cases. Please checking your implementation. Should you feel it’s right, click my name and message your notebook as an attachment.
Here are a couple of hints for you to fix train_naive_bayes
:
N_pos
and N_neg
, don’t add 1 to increment the count. This comment should help # Increment the number of positive words by the count for this (word, label) pair
.freq_pos
and freq_neg
, you shouldn’t hardcode the word.Hi,
Thanks for the response. If i don’t hardcode the word, then I use
for word in vocab:
freq_pos = freqs[(word,1.0)]
freq_neg = freqs[(word,0.0)]
But I got an error message saying that there is a KeyError. Can you please help me with this? Thank you.
I am assuming that I come accross a word in vocab that does not exist in the dictionary freqs, so it raises a key error? But shouldn’t vocab come from freqs.keys()? I am confused
There is no guarantee that any given word has both a positive and negative frequency entry in the dictionary. So your code needs to handle that. The cleanest way is to use the “get()” method, but if you like writing more code, you can also use “if” clauses.
Thank you for the response. After using get(), I’m having this error.
{moderator edit - solution code removed}
I think the problem still exists since for some words, they don’t have both (word,1.0) and (word,0.0) keys, then the get() function will return a “Nonetype” and this raises an error
Problem solved by adding “if” clauses, thank you!
You’re using “get()” incorrectly. The second argument is the value to be returned when the key is not found. You set that as “None”, which is why you got that error. Try 0. instead and it will work a lot better.