C2_W4_Decision_Tree_with_Markdown - tests start failing

Greetings TMosh, thank you very much for your great work. I’m similarly having a problem with this lab.

I have checked the indents and believe all is as it should be. I have tried multiple variations for computing P1. Lots of assertion errors. I relented and looked at the hint. I was pretty confident with the entropy formula further down. I checked that as well because I continued to get errors. I have tried running it code separate in another notebook and it seems to work (of course, I’m probably missing some crucial detail).

Here is a screen grab of the assertion error.

AssertionError: Entropy must be 0 with array of ones

Should I be delving into assertion errors for the answer?

Thank you very much.

Ubik

1 Like

Yes. The assertion tells you exactly what the problem is. When the unit test feeds your function an array of all-ones, it does not give the correct result.

You can read the details of the test it is failing by opening the public_tests.py file (using the File menu), and then finding the compute_entropy_test() function.

1 Like

Greetings again TMosh, apparently, haha, I’m still stuck.

I presume this function compute_entropy(y) takes its input for y outside the function so it can deal with different y training sets if required. So the public_tests.py supply the missing information. Would that be correct?

I have created another notebook page to test everything – but I do add y = y_train inside the function so I can see what it computes.

When I call compute_entropy(y) outside the function, with this array – y_train = np.array([1,1,0,0,1,0,0,1,1,0])

5/10

I get 1.0 – most impure

When I change the y_train array to equal ([1,1,0,1,1,1])

5/6

I get 0.6428571428571429

Are these correct? The code in the lab is the same as what I’m using to generate these answers (except for putting the training array inside the function).

Thank you very much for your help.

Ubik

ps. I had originally used p1 = np.sum(y) / …
for the first part of the assignment for p1 – I presume that this isn’t the equivalent of the supplied hint’s expression. But it seems to compute the same?

1 Like

I don’t totally understand your last message.

Please check your private messages for instructions.

My sincere apologies. Yes, I didn’t want to cut and paste code… difficult to describe my process without illustrations.

1 Like

Summary
In compute_entropy(), when checking the p1 values, be careful to use the correct logical operator.

Hint: ‘or’ is not the correct choice.

1 Like

@TMosh I’m also having some challenges with this exercise (# UNQ_C3). I successfully computed compute_entropy() & split_dataset in the first 2 exercises. But I am getting an error “TypeError: unsupported operand type(s) for *: ‘float’ and ‘NoneType’” on my unit test. Not sure how to address at this point.

1 Like

I assume you didn’t modify any of the pre-written code for X_right, X_node, etc.

I would guess there’s a problem with your calculation of one of these variables:

  • w_left
  • left_entropy
  • right_entropy

First thing (but not the only thing) to check is for any spelling errors.

1 Like

I didn’t modify any of the pre-written code. I have checked spellings as well.

I do notice something odd about the comment in the pre-written code, where is explains the args and return value. It only lists 3 of the 4 args, and returns “cost” instead of “information gain” which is the function that we are defining

1 Like

You are correct, the doctext for that function is incorrect. I’ll submit a ticket to have it fixed.

It remains true that I think your code for those variables may be incorrect.

The “NoneType” reference implies that one of those variables is set to None.

1 Like

Thank you. I discovered the bug. My “return entropy” code from exercise #1 was written within the “if” conditional statement, instead of outside.

2 Likes

Thanks for your report. That’s a tricky bug to find, nice work!

2 Likes

Hi, I am having the same problem with the last code. I have successfully executed all the 4 exercises but because of the last code, I am not able to submit my assignment. I keep getting the same error no matter how I change the above code.

1 Like

What does your code do if the length of y is zero? How does it assign a value to p1 in that case?

Hi @TMosh,
So I did that too and while it passed the exercise, the error was the same.
Can I DM you the code I have used?

1 Like

Sorry, I don’t understand this statement.