I’m having trouble with my code in the practice Lab. Actually, the error that I get is quite similar to the error reported in this question, which led me to try debug my code as explained on this topic. However, I wasn’t lucky enough to be able to solve my code
This is the error message that I get:
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)
<ipython-input-14-2a50df79e115> in <module>
9
10 # UNIT TESTS
---> 11 compute_information_gain_test(compute_information_gain)
~/work/public_tests.py in compute_information_gain_test(target)
104
105 result = target(X, y, node_indexes, 1)
--> 106 assert np.isclose(result, 0, atol=1e-6), f"Wrong information gain. Expected {0.0} got: {result}"
107
108 print("\033[92m All tests passed.")
AssertionError: Wrong information gain. Expected 0.0 got: nan
Expected Output:
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
Therefore, my code is failing somewhere using that small data sample provided in the public tests, when expected a result of 0, got a NaN.
However, following the steps of the debuging tutorial, I’ve copied and pasted the sample data and the code, but my code resulted in a 0, not in a NaN.
I don’t know how to continue from here. Any help, please? My Lab Id is cwoqaljn
Debugging is part of the assignment, and sometimes it’s just not a straight forward thing but this is part of life, right? Here I can give you one more suggestion. You must have used some variables in your code to temporarily store all the intermediate results, what you can do is to add a line each time you store an intermediate result, and the line is print(variable_name) where you need to replace variable_name with the name of that variable.
Printing the content of those variables allows you to inspect the progress of your own code, so that you can ask yourself whether the content is correct or not, and if one of those printing results does not match with your expectation, then you know better where the bug is.
An alternative way to print the content is to add a number such that it becomes, for example, print(1, variable_name). If you are storing 5 intermediate results, you will have to do 5 printing, so in order to distinguish one printing result from another, you might add a different number to each of those 5 printing lines.
We can’t access your account and we won’t. Your assignment is yours and as I said, debugging is part of that work, so I am afraid you will have to keep trying. However, it is my personal experience and also some learners’ experience that sometimes it makes a difference just by taking a break and I believe a break can help reset your focus so that next time when you debug your code again, you might discover what you had overlooked. Since there is a bug and you couldn’t find it, then something must have been overlooked.
Good luck @AlvaroViudez, as you also know, you are not the only one who encountered an error like that, and I believe you will also be one of them who is able to figure it out yourself.
Your hint on printing temporal variables has just helped me know what was happening. Actually, the problem was on a previous function, “compute_entropy”, which passed all the tests however it wasn’t prepared for returning 0 if the list of values was empty.