Assertion error in C2_W4 practice lab on best split function

I am getting an assertion error for the get_best_split() function. I have tried implementing without looking at the hints and have got this error so I looked at the hints in the practice lab for help, but I still get the same error and have no idea as to why it is coming.

This is the error I keep getting:

Best feature to split on: 2
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-25-c263c9190d86> in <module>
      3 
      4 # UNIT TESTS
----> 5 get_best_split_test(get_best_split)

~/work/public_tests.py in get_best_split_test(target)
    120     result = target(X, y, node_indexes)
    121 
--> 122     assert result == -1, f"When the target variable is pure, there is no best split to do. Expected -1, got {result}"
    123 
    124     y = X[:,0]

AssertionError: When the target variable is pure, there is no best split to do. Expected -1, got 1

One thing that seems very puzzling here is that if the feature is pure then the information gain will always be 0, since entropy is at its maximum of 1. So the code in my implemented function must return -1 since the if statement would never execute as 0 > 0 is false (any split would be 0, and max_info_gain would stay 0) and so the value of best_feature should not change from -1. However the assert is getting 1 and I have no idea why.

All the other functions before this one work correctly and pass all the assertion tests and I have checked this function many times. I would be very grateful for an explanation as to why this error is occuring and the mistake that I have made, especially since the code appears correct. Thank you.

Hi @Yusuf_Zaheed

In call of function compute_information_gain() you will not pass(use) the (X_train, y_train) variables but use the variables that passes to you as a parameter in get_best_split() function to make you code generalize for any test case …so you will use (X ,y) variables instead of (X_train, y_train) .

Note please do not post your code on the forum. That isn’t allowed by the community standards.

please feel free to ask any questions,
Thanks!
Abdelrahman

I agree with Mentor Abdelrahman: X_train and y_train are global variables within the notebook. Do not use them inside any of the functions.

Thank you very much, so the global variables are the cause of the error. So is in not allowed then to post a piece of code in the event there is an error to make it easier for the error to be found? Is there any alternative way to show you the code in the event a strange error happens again in future?

If a mentor needs to see your code, they will ask you to send it to them via a personal message.

On the discussion forum, you can always post a screen capture of any assert logs or error messages you get in the notebook.