C2_W4_Decision_Tree_Ex2 and Ex3

Hi mentors

I’m stuck in the exercise 2 split_dataset() function and would appreciate some help. I understand we can’t post codes here, i hope this is okay to post error message:
image

I am unsure error caused by line 25 or actually for loop is also incorrect?

so I tried to below which seemed to pass case 1 and be able able to print case 1 and case 2. However it returned ValueError: not enough values to unpack (expected 2, got 0)

{mentor edit: code removed}

could you provide some guidance please?

Thanks
Christina

In split_dataset(), you need to test the value of X at the indices of i and feature.

Please do not post your code - the error message is fine.

Thanks Tom. I made some changes based on my understand of your suggestions. unsure if I am heading the right direction, but I got this error instead, could you shed a light please?

Your for-loop has the wrong indices (there is no ‘j’ needed), and why are you testing enumerate(X)?

Please re-read my reply from an hour ago. I essentially gave you the answer.

Thank you Tom. I really felt by looping over node_indices and checking node_indices[feature] ==1 to be correct.

Obviously something still is not correct, could you provide further assistance?

Hi @Christina_Fan,
Have you figured this out? If not, from the thread above, it looks like it’s time to step back and make sure you’ve got a clear picture of all the parameters into split_dataset() to help get you back on track.

Here are the comments in the code that explain the input parameters:

def split_dataset(X, node_indices, feature):
    """
    Splits the data at the given node into
    left and right branches
    
    Args:
        X (ndarray):             Data matrix of shape(n_samples, n_features)
        node_indices (list):     List containing the active indices. I.e, the samples being considered at this step.
        feature (int):           Index of feature to split on

X is shape (n_samples, n_features). Think of it as rows of features - one row per sample.

In the assignment, the picture of the first few rows looks like this:

where the 0, 1, 2, 3, … are the indices for each row (sample), and the values in each row are the values for the features.

My guess about what may be throwing you off is the node_indices(list) parameter. This is just a list of the indices (row numbers) of the samples for the current node. This tells us which rows in X we want to look at in order to pick which will go into the left branch and which will go to the right branch.

So, you’ll use each value in node_indices to tell you which rows of X you want to look at, and then the “feature” parameter to tell you the index of which feature in that row you want to look at.

Hopefully, this explanation helps make things click, but you can also go back and re-read the explanations in the lab to make sure you understand what each parameter represents. You can also click on the green “Click for hints” link after the code cell to see what the code should look like. If you use the “Click for hints”, once you get it working, you can go back and make sure you understand why it’s working.

Aww, this is soooooooooo helpful Wendy. I’m so grateful for you taking the time to explain it to me. Yes, I now know where I got wrong and be able to fix it. It finally passed! :raised_hands:

Thank-YOU :heart:

1 Like

Hi Wendy,
I’m hoping you could assist me with exercise 3 here. An error message shown although the output results all are correct.

I’m unsure which part has gone wrong.
Many thanks
Christina

I’ll mark up your image to illustrate what’s happening.

There are four separate tests. The first three are built into the notebook (1, 2, and 3). Your code passes all these tests (your results match the expected values).

The 4th test is in the compute_information_gain_test() function in the public_tests.py file. If you use the File menu and open that file, you can see that it actually has five separate tests.

Your code does not pass one of those tests - it isn’t returning the correct result. You can look at the data used in that test to help you debug your code.

Thank you Tom for your prompt response. I understand what you meant but I don’t know which function codes needs to be fixed?
Because from the trace error in Ex4 which also did not pass the unit test, the error seemed to point out very early on in the Entropy function.
Hence I am unsure if the error in compute_information_gain function has anything to do with the codes in compute_entropy.

Could you assist further please?
Thanks
Christina

@Christina_Fan, good observation that the Exercise 4 test is also failing in compute_information_gain(), inside of its call to compute_entropy(). Definitely go ahead and fix that problem in compute_entropy(). It’s a divide by 0 error on the line:
p_1 = len(y[y == 1]) / len(y), so should be relatively easy to fix. Notice the line in the instructions for compute_entropy:
" * Make sure to check that the data at a node is not empty (i.e. len(y) != 0). Return 0 if it is"

I suspect the problem you’re seeing in the compute_information_gain unit tests is something different, since you’re not seeing a divide by zero error there, but give it a try after you’ve fixed the divide by zero error.

If there is still an error, I’d suggest focusing on fixing compute_information_gain and getting that exercise working before moving on to exercise 4, since that builds on compute_information_gain. You’re off to a good start! I see you’ve added some print statements to help you debug - nice!

1 Like

Hi Wendy and Tom,

Thank you so much for the hint. I added a if condition to fix the zero error. As a result, exercise 4 and onwards all passed nicely.

Yep error in the compute_information_gain is different, I re-checked the hints provided and fixed the error and passed the unit tests.

Great exercises, hard but I learned a lot from solving the errors. Thank you so much for your kindly help along the way :raised_hands: :heart:

Many thanks
Christina

1 Like

Excellent! That’s great, @Christina_Fan!