Course 2 Week 4 Assignment


I’ve been on the last assignment for Course 2. My code works and produces the required results; however, I have encountered issues with some embedded Assertion Errors and as a result can’t pass the assignment.
For instance, on the compute_entropy function, my code provides the correct result. I have even gone ahead to check the hint and implement accordingly., but the code returns ‘AssertionError: Entropy must be 0 with array of ones’.

Another instance is the compute_information_gain function, my code produces the expected output. However, it also returns an error ‘AssertionError: Information gain must be 0 when target variable is pure. Got nan and nan’. To rectify this, I imported the math module, added a condition ‘if math.isnan(information_gain): information_gain =0’, yet it returned same error.

Despite these, I went ahead to implement the function to get best split. While it produces the right output, it ptoduces yet another error ‘AssertionError: When the target variable is pure, there is no best split to do. Expected -1, got 0’
Note that these errors come when you run the cells used to check correct implementations of functions.

What can I do to rectify these please?

Hello @Chukwuka_Chikwendu,

For this, I guess the problem is in the assignment work, please check out these notes which are also in the exercise’s description.

Let’s see if the other errors go away after you make some changes in compute_entropy ? Because the other exercises depend on it.

Let me know if you have any questions about the notes.


Hello Raymond,

It still doesb’t work. Like I said, I had even clicked to reveal the hints and implement accordingly.

The error that you reported says “AssertionError: Entropy must be 0 with array of ones”

The note says

So, is your code able to set the entropy to 0 in the situations mentioned in the note?

So the question is, when the input is an array of ones, such as np.array([1,1,1,1]), what does your function do? Why doesn’t it return a zero?

If you can’t figure it out by just reading the code, then I would suggest you to use the print function inside your function to print something out for you to keep track of the progress of your function, and make sure you see for your eye that each step is working really as your expectation because if you can’t “read” the error out, then something isn’t working as you are expecting for.

There is an error in your code. Passing all of the test cases is not sufficient to prove your code is perfect.