ML specialization, 2nd course, decision tree

in the practice lab for the decision tree in the part that we have to get the best split, my code works and I get the 2nd feature as the best feature to split the node upon. but I get this assertion error that I have no ideas about. This causes the next cell to not work correctly also. how should I fix it??

Hi @Ali_Shendabadi, welcome to the community!

The tests in get_best_split_test() are checking your implementation for several situations, to help confirm it handles all the cases it should. So, even though your code is working properly for the case where you passed it x_train, y_train, and root_indices, there’s another case where it is not working properly.

If you ever want to look at the test code to hep you understand what it’s checking for, you can find it in, which you can get to by going to the File menu and choosing “Open…”

In this case, the code for the test that’s failing looks is:

    y = X[:,0]
    result = target(X, y, node_indexes)
    assert result == 0, f"If the target is fully correlated with other feature, that feature must be the best split. Expected 0, got {result}"

Basically, the test is checking to confirm that if y exactly matches one of the features in every case, then that feature is the one chosen as the best to split on. It checks this by setting y to match the first feature of X (feature 0). Then it checks to make sure your implementation chooses feature 0 as the best match.
So, the error message tells you the test expected your function to return 0, but instead it returned -1.

Since -1 is the initial value for best_feature, it sounds like the code you implemented never chose a new value for best_feature in this case. Look back at your code and see if you can figure out why it is not handling this case. If you don’t see by just looking, it might be helpful to temporarily add some print statements to see what’s going on.