Unit Test failing at practice Lab:Decision Tree

Graded Lab:

I am getting this error, and am unable to understand why this is failing.
The funny thing is , I am getting these three expected output. But its failing at #Unit Tests.
Would appreciate any hints.

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


Cell #11. Can’t compile the student’s code. Error: AssertionError(‘Wrong information gain. Expected 0.019973 got: [0.]’)
Traceback (most recent call last):
File “/home/www/app/grading/exceptions.py”, line 112, in handle_solution_errors
yield {}
File “/home/www/app/grading/abstract.py”, line 393, in _grade
context = compiled_code.run(cell_index=cell.index)
File “/home/www/app/grading/submission/compiled_code.py”, line 195, in run
return list(self._code_items.values())[cell_num - 1].run()
File “/home/www/app/grading/submission/compiled_code.py”, line 54, in run
return import_module(self.import_statement, items)
File “/usr/local/lib/python3.7/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 1006, in _gcd_import
File “”, line 983, in _find_and_load
File “”, line 967, in _find_and_load_unlocked
File “”, line 677, in _load_unlocked
File “”, line 728, in exec_module
File “”, line 219, in _call_with_frames_removed
File “/tmp/student_solution_cells/cell_11.py”, line 23, in
File “/tmp/public_tests.py”, line 96, in compute_information_gain_test
assert np.isclose(result, 0.019973, atol=1e-6), f"Wrong information gain. Expected {0.019973} got: {result}"
AssertionError: Wrong information gain. Expected 0.019973 got: [0.]

Hi, I ran into the same issue and solved it eventually! This means you have passed the simple tests on the three Y’s in exercise 3, but failed more complex hidden ones in compute_information_gain_test().

For me, the reason seems to be the compute_entropy() function, in which I first set p = np.sum(y==1) / len(y). This worked for all the simple tests in exercise 1-4. However, for exercise 3 specifically, one of the hidden Y is acually an empty array, which resulted in p = np.nan = 0 / 0, and resulted error in entropy. If that’s what you did there, you can add a condition of only calculating entropy for non-empty Y’s, and let p = 0 and entropy = 0 for the empty ones.

Hope this helps!


Thanks LisaG.