Error in Final Exam of C2_W4

Hi, I’m getting errors from the unittest (see below) in this exam although the code seems to be yielding the expected output, could you please help? thanks,

ERROR:

Information Gain from splitting the root on brown cap: 0.034851554559677034
Information Gain from splitting the root on tapering stalk shape: 0.12451124978365313
Information Gain from splitting the root on solitary: 0.2780719051126377


AssertionError Traceback (most recent call last)
in
9
10 # UNIT TESTS
—> 11 compute_information_gain_test(compute_information_gain)

~/work/public_tests.py in compute_information_gain_test(target)
113
114 result = target(X, y, node_indexes, 1)
→ 115 assert np.isclose(result, 0, atol=1e-6), f"Wrong information gain. Expected {0.0} got: {result}"
116
117 print(“\033[92m All tests passed.”)

AssertionError: Wrong information gain. Expected 0.0 got: nan

There is nothing apparently wrong with my code, can someone please help? thanks

Hi Napolean!

If we look at the failed test we can see that the function is producing a nan.
What is nan and how is it produced?

One way is division by 0.
After nan has been produced any arithmetic with nan is nan.

I would check everywhere division is performed in compute_information_gain() and compute_entropy() for where division by 0 can occur and try to prevent it.

Hope this helps!

Thanks for your help, this was indeed the issue, I managed to resolve it upon further inspection.

Information Gain from splitting the root on brown cap: 0.034851554559677034
Information Gain from splitting the root on tapering stalk shape: 0.12451124978365313
Information Gain from splitting the root on solitary: 0.2780719051126377

AssertionError Traceback (most recent call last)
in
9
10 # UNIT TESTS
—> 11 compute_information_gain_test(compute_information_gain)

~/work/public_tests.py in compute_information_gain_test(target)
113
114 result = target(X, y, node_indexes, 1)
→ 115 assert np.isclose(result, 0, atol=1e-6), f"Wrong information gain. Expected {0.0} got: {result}"
116
117 print(“\033[92m All tests passed.”)

AssertionError: Wrong information gain. Expected 0.0 got: nan

“nan” means your code is not computing a real-valued result. “nan” means “not a number”.