Course 2 Week4 Assignment Lab Exercise 3 and 4

I have completed all 4 exercises for the Course 2 Week 4 Assignment Lab and got all the expected outcomes.

However, for Exercise 3 and 4, I am getting AssertionError. The following are my results for Exercise 3 and 4.

I checked the code for

UNIT TESTS

compute_information_gain_test(compute_information_gain)

Result1 and Result2 are being executed with the same parameters of (X, y, node_indexes, 0). Is this a bug is this bit of code? Is this why I am getting AssertionErrors?

Please advise why I am getting these AssertionErrors when running the Unit Tests but my results are the same as the expected outcomes for the Exercise 3 and 4.

Hello @Danny_Go,

First, those expected outcomes only mean your code works for those specific cases, and don’t guarantee for any other cases, so we have more tests including the one mentioned by you - that detected that your code doesn’t return a zero gain when the target is pure (all 0s in your case).

Just think about the principle: if the incoming data is already well grouped, because they are all of the same class, then there should be no need to further split them up. We want to split the data because they have mixed classes, and we hope that after splitting, the child nodes each contain only one class of samples. The maths should be consistent with this rationale too. If the incoming data is already of just one class, then no way can we further split it to achieve any gain, or in other words, the gain should just be zero. Since your code doesn’t return a zero, you get this error.

As for your last question, you may question whether it is necessary to repeat the test two times, but repeating it for two times itself shouldn’t be a problem because, in both times, your code should return a zero gain. Therefore, I think it is not relevant to the error you are seeing. As for why they repeat the test, perhaps this can help catch some error that we cannot think of right here right now.

Since you have looked at the test, the data is very simple. It is so simple that we can literally work out the math on our own on a piece of paper. This is why I suggest you to add a couple of print lines to show the values of all intermediate variable ever used in your code for Exercise 3, and verify them yourself. The value should be consistent with the provided math equations, and the rationale I shared above - that there shouldn’t be any information gain. I hope that in this way you will be able to find the bug yourself - finding bug is a part of the assignment.

As for exercise 4, it seems they are related to each other. Let’s see if it will go away after exercise 3 is fixed.

Good luck!

Raymond

1 Like

When you get an error from running a unit test, it means your code has a defect.

Thank you @rmwkwok and @TMosh. I solved the problems. I was using global parameters instead of local parameters passed into the functions.