C1_W2_Assignment_Exercise 2 - train_naive_bayes

hi ,
I am getting below error ,i could not understand ,what mistake i did.


Thank you

I added several print statements to instrument my train_naive_bayes function and here’s what I see when I run that test cell:

type(wordlist) <class 'list'>
V = 9165, len(wordlist) 11436
V: 9165, V_pos: 5804, V_neg: 5632, D: 8000, D_pos: 4000, D_neg: 4000, N_pos: 27547, N_neg: 27152
freq_pos for smile = 47
freq_neg for smile = 9
loglikelihood for smile = 1.5577981920239676
0.0
9165

The most common error on this function is simply to add one to N_pos and N_neg for each occurrence. The intent here is that you add the actual frequency each time by looking it up in the freqs dictionary that was passed in as an argument.

Of course there are other moving parts here, e.g. check the order of operations on your computation of p_w_pos and p_w_neg.

Dear Sir ,


I am getting same values
but getting same error

Yes, your values do all agree with mine. It’s probably time to look at your code. We aren’t supposed to do that in public on this thread, but we can do it by DM. I will send you a DM about how to do that.

Hello! could you explain to me the concept of N_pos and N_neg?

We have a dictionary called freqs where the keys are (word, sentiment) where sentiment is either 1 for “positive” or 0 for “negative”. What that dictionary tells us is the total number of times a given word appears in a positive tweet and appears in a negative tweet.

In order to implement the Naive Bayes formula we need to know the total number of occurrences of unique words in positive tweets and negative tweets. That is what the numbers N_pos and N_neg represent: the total number of occurrences of positive words and negative words.

To compute those numbers we can loop over the keys in the freqs dictionary. Note that some words appear in both positive and negative tweets, but not all words do.