Issue in C1_W2_Assignment (test cases not updated as per code review?)

Please see the code review comment as found in the notebook

# UNQ_C9 GRADED FUNCTION: get_words_by_threshold

def get_words_by_threshold(freqs, label, threshold, get_ratio=get_ratio):

    # **CODE REVIEW COMMENT: This has been changed!! word_list was described as a dictionary, but defined (and operated on) as a list**

Function written seems to be working all right, but the test cases are failing!

result[key][sec_key] tells me it is expecting a list within a list (instead of a python dict).

Is this bcoz the test cases for the same function are not updated as per the review comment ?? Can’t pass the assignment bcoz of this error!

Hi @Navneet_Mann,

Can you share your lab ID with me ? In the assignment, when you click the top right ā€œHelpā€ button, a panel will open and your lab ID will be shown at the bottom.

I shall take a look.

Thanks,
Mubsi

C1_W2_Assignment
Lab ID: sxmfunlq

Thank you @Mubsi !

Hey @Navneet_Mann,

I have found your mistake in the exercise. I cannot say it out here without giving away the solution. You can see it in your notebook.

Happy learning,
Mubsi

1 Like

Thank you so much for clearing out the confusion!

I met this problem as well. Can you help me to take a look again? Thanks ahead!
@Mubsi

Mubsi is a very busy guy, but there are other mentors to help. Please check your DMs for a message from me.

Thanks @paulinpaloalto, for jumping in. Really appreciate it!

@helenhe, did you get the answers you were looking for ?

Thanks, Mumbai and Paul. It works.

I have the same issue. My Lab ID is: ykcryagjyhgo. Please help.

Only Mubsi can directly look at your assignment and he’s a very busy guy with lots of other responsibilities. Please start by showing us the error output you are getting. Note that this is a very old thread, so don’t assume that this is someone else’s fault because the test cases are out of sync or the like. There is most likely something wrong with your implementation and now you need to debug it.

Yes, I realize it might be an implementation issue. Error output below.

Test your function

w2_unittest.test_get_words_by_threshold(get_words_by_threshold, freqs)

TypeError Traceback (most recent call last)
~/work/w2_unittest.py in test_get_words_by_threshold(target, freqs)
780 try:
→ 781 assert np.isclose(result[key][sec_key], sec_value)
782 successful_cases += 1

TypeError: ā€˜float’ object is not subscriptable

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
in
1 # Test your function
----> 2 w2_unittest.test_get_words_by_threshold(get_words_by_threshold, freqs)

~/work/w2_unittest.py in test_get_words_by_threshold(target, freqs)
786 ā€œnameā€: test_case[ā€œnameā€],
787 ā€œexpectedā€: {key: {sec_key: sec_value}},
→ 788 ā€œgotā€: {key: result[key][sec_key]},
789 }
790 )

TypeError: ā€˜float’ object is not subscriptable

Ok, so that means there is something wrong with the return value from your ā€œget wordsā€ function. What do you see from the previous two cells that execute that function. Here’s what I get for the first one with the negative label:

{':(': {'positive': 1, 'negative': 3675, 'ratio': 0.000544069640914037},
 ':-(': {'positive': 0, 'negative': 386, 'ratio': 0.002583979328165375},
 'zayniscomingbackonjuli': {'positive': 0, 'negative': 19, 'ratio': 0.05},
 '26': {'positive': 0, 'negative': 20, 'ratio': 0.047619047619047616},
 '>:(': {'positive': 0, 'negative': 43, 'ratio': 0.022727272727272728},
 'lost': {'positive': 0, 'negative': 19, 'ratio': 0.05},
 'ā™›': {'positive': 0, 'negative': 210, 'ratio': 0.004739336492890996},
 '怋': {'positive': 0, 'negative': 210, 'ratio': 0.004739336492890996},
 'beli̇ev': {'positive': 0, 'negative': 35, 'ratio': 0.027777777777777776},
 'wi̇ll': {'positive': 0, 'negative': 35, 'ratio': 0.027777777777777776},
 'justi̇n': {'positive': 0, 'negative': 35, 'ratio': 0.027777777777777776},
 'see': {'positive': 0, 'negative': 35, 'ratio': 0.027777777777777776},
 'ļ½ļ½…': {'positive': 0, 'negative': 35, 'ratio': 0.027777777777777776}}

What does your output look like?

Notice in the output I show, it is a dictionary with the ā€œwordā€ as the key. But also notice that the value for each word key is also a dictionary with three entries. That’s the meaning of the doubly nested curly braces there.

Also please check to make sure that your previous get_ratio function passes its tests as well, since the value for each word is the return value from get_ratio.

Thank you. I see the problem. I was returning word_list with only ā€˜ratio’ updated, and not as a nested dictionary with ā€˜positive’, ā€˜negative’, and ā€˜ratio’ updated.