C2_W4_Decision_Tree Practice Lab error when Submitting

Hi mentors,

I’ve tried every known fix for Cell #7 in the C2_W4 Decision Tree lab, including:

  • Correct logic for compute_entropy() using Coursera’s own hints
  • Testing output for [0, 1] → returns 1.0
  • Early return structure, float-safe comparisons, len(y) == 0 handling

Still getting this error (see attached screenshot).

Any suggestions?
Thank you!

1 Like

Hello, Mark, how are you? Have you solved it yourself?

My best 2-minute suggestion is for you to analyze the problem and add some prints to see if things work as expected:

Below is how I would analyze it:

You see, the maths is not complex and we should get 1 as the answer. You could also add some temporary prints like print(p1) in between the lines to see if things work as expected, but please remember to remove those prints after debugging is done as they may interfere with the autograder.

You spot the print that does not give you expected result, you find the line that needs your attention!

The whole process should take less than 2 minutes.

Cheers,
Raymond

1 Like

Hi Raymond,

Thanks again for your help with Cell #7 — the entropy logic and your suggestion to test with [0, 1, 0, 1] really cleared things up and helped me pass that part of the assignment.

However, I’m now running into a new issue with Cell #9. The autograder fails because generate_split_viz() is trying to load images that aren’t present in the environment, even though I do see 0.png through 9.png in the notebook folder when running locally (screenshot attached). The error is a ValueError about unpacking widths and heights due to an empty list of images — I’ve attached the traceback as well.

Since this cell isn’t graded, I tried commenting it out, but Coursera’s autograder still compiles it and fails. I’m stuck here — do you happen to have any suggestions for handling this cleanly? Or should I post this out to the broader class community?

Thanks again — your guidance really helped get me through Cell #7.

Try Again
• Grade Received: 0%
• To Pass: 80% or higher
Submissions
Required
June 30, 2025 9:18 PM PDT (Highest Grade)


Cell #9. Can't compile the student's code. Error: ValueError('not enough values to unpack (expected 2, got 0)')
 Traceback (most recent call last):
  File "/home/www/app/grading/exceptions.py", line 112, in handle_solution_errors
    yield {}
  File "/home/www/app/grading/abstract.py", line 393, in _grade
    context = compiled_code.run(cell_index=cell.index)
  File "/home/www/app/grading/submission/compiled_code.py", line 195, in run
    return list(self._code_items.values())[cell_num - 1].run()
  File "/home/www/app/grading/submission/compiled_code.py", line 54, in run
    return import_module(self.import_statement, items)
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/tmp/student_solution_cells/cell_9.py", line 26, in <module>
    generate_split_viz(root_indices, left_indices, right_indices, feature)
  File "/tmp/utils.py", line 32, in generate_split_viz
    G.add_node(idx,image= generate_node_image(indices))
  File "/tmp/utils.py", line 9, in generate_node_image
    widths, heights = zip(*(i.size for i in images))
ValueError: not enough values to unpack (expected 2, got 0)

Hello Mark, I am happy to read that you finished the entropy part. I am now outside but I can get back to you about your last post in an hour.

Cheers,
Raymond

Hello Mark,

Let’s take a look at this:

You see - the same error is produced if images is an empty list, so it is possible that the same happened to your code instead of being any problem about locating the image files.

With this in mind, we should try to hypothesize some possible sources of problem. I would first suggest you to read the last part of the error traceback to find out what is substituted into images (the kind of question to ask ourselves is: could that be an empty list, or are we certain that it is not an empty list?)

I will leave this part to you, but at the end, it will come down to the outputs of your exercise work - split_dataset. Below is a screenshot of cell #9, in which we see that those outputs are printed for comparing with the expected output:

So, if you run the notebook from the 1st cell down to the 9th again, what are the printed values there? do they match with the expected outputs? Any empty lists? Afterall I am not examining your code, so even if my questions here do not hit the problem, you might still try to trace the bug yourself, but please just don’t comment this cell out, because this cell contains some test cases that we should pass.

Cheers,
Raymond

PS: I edited your last post for better formatting. If you want to know what I have changed, click the edit button of the post - essentially I added [/quote] and wrapped the traceback with a pair of “```”

Hi Raymond,

Thanks for your message and the helpful direction. I went back and re-ran the notebook from the top, especially focusing on Cell 9 like you suggested. I double-checked the outputs of split_dataset() and confirmed that it’s returning non-empty lists — but the test still expects [1, 2] and [0, 3, 4], while my version gives [1, 2, 4] for that case.

I tried a few different ways to match what the grader seems to want, but no luck so far. I haven’t attached the notebook yet, but I can definitely share it if that helps. Let me know if you have any other ideas — I really appreciate your time.

— Mark

Hello, Mark @Mark29,

It’s great to read the progress you had made, so, from the quote below, it seems that you were suggesting that your exercise didn’t match with one of the expected outputs?

However, I am not sure which case it is, because, in Cell #9, there are 5 test cases, and I checked each of them but none was expecting for [1, 2] and [0, 3, 4]. However, regardless of which case it was, what I can suggest would be, like last time, for you to add prints and check the logic (not math this time) yourself. For example, you might add prints like below:

With these prints, you might focus on the failed case, start from the inputs and work the logic yourself to get the outputs and check them against your code’s outputs and the assignment’s expectation. Below are my prints but you would find that I made a little change in what to print:

You see, it’s a 3-step work and can easily be repeated for every test case.

Lastly, next time, please share a screenshot of the cell’s output for any output-expectation mismatch or error or warning, because while your description is important and helpful, the output might contain additional information which can help us make suggestions to you. Also, if I read your notebook, I will know what’s going wrong, but instead of me telling you what I know, as a mentor, I think it is better that I give you some general suggestion and then you be the first one to find out and know what’s going wrong.

In my experience, as long as learner only edited code inside the exercise area (between the ### START CODE HERE ### and ### END CODE HERE ### lines), even there was bug in their code, they would finally be able to figure it out with the help of, for example, hints, prints, and math/logic check.

Cheers,
Raymond

Hi Raymond,

Thanks again for your help and encouragement with this lab. I’ve been working through the assignment and trying to follow your advice about using prints and logic checks to figure things out.

I wanted to share two issues I’m running into when trying to pass all the autograder tests:

  1. Cell #9 – If I leave split_dataset() as-is, the grader crashes when it calls generate_split_viz(). I fixed this by adjusting the function so it always returns something (like moving one sample over if one side is empty). After that, Cell 9 runs and doesn’t crash. (See screenshot 1)

  2. Cell #12 – The problem is, once I make that fix, the output from compute_information_gain() changes slightly and then the unit test fails because the expected value is different. (See screenshot 2)

I added print() statements inside the cells while testing and was able to get all the outputs to look correct when I ran them myself — so I think the logic works, but the autograder is catching the conflict between the two cells.

I only changed code between the ### START CODE HERE ### and ### END CODE HERE ### sections. I’ve attached the screenshots here, and I can also send the notebook file if that helps — just let me know.

If you have any suggestions or tips on how I can get both of those cells to pass at the same time, I’d really appreciate it!

Thanks so much again,
Mark


Hello, Mark!

How’s the day? I am having a very good morning. :smiley:

Although I have read through your whole message, my response will just focus on the following part and I will explain why:

Your adjustment seems to suggest that your split_dataset had returned empty list and that crashed generate_split_viz. Given this, my suggestion would be not to “adjust”, but to make sure the logic for split_dataset is correct.

First, I can guarantee you that no test, and I mean no test, expects split_dataset to return any empty list. This means that if you test split_dataset with the assignment’s test cases and if your implementation of split_dataset is correct, it won’t return empty list and generate_split_viz won’t crash.

Second, just now, I submitted that assignment and scored 100%, so I hope you will believe that the way out of this was not to silent error by adjustment, but to make sure the work is correct and this is what assignment is for. :wink:

Here is what I would suggest you to do:

  1. Last time, you had shared expectation that I couldn’t find any test that produced them, so I wonder if it was a case not from the original notebook. This time, your screenshot seems to suggest that it was not a Coursera environment. please open the notebook on Coursera, follow these steps to get a new copy of the notebook.

  2. Copy the working solution for exercise 1 to the new notebook, then work on exercise 2 and only make changes between the ### START CODE HERE ### and ### END CODE HERE ### lines

  3. Add print in between each line of your code to show what the line has achieved and if the achieved result sticks to what is worked out by yourself.

  4. Run the tests in Cell #9 and if any test fails, debug it, and if any problem comes up, share a screenshot of the problem (including the whole error message).

  5. Only after you clear Cell #9, proceed to subsequent cells and exercises. (This is why I don’t respond to your message about your Cell #12 because exercise 4 depends on a correct exercise 3 and exercise 3 depends on a correct exercise 2, so we need to clear exercise 2 first.

Mark, I would just like to make two more points:

  1. When you take screenshot to share here, what we want to see is the output coming from the notebook on the Coursera environment. Your last screenshot apparently isn’t that. If you can’t share that screenshot we need, we wouldn’t be able to help you.

  2. As you see, we are exchanging messages once every one to a few days, and I probably won’t come here in the next couple of days, so if you have any follow-ups, I would recommend you to open a new topic so that other participants of this forum are more likely to respond.

Cheers,
Raymond