Error in Grading Submission for Graded Lab Exercise 8 in C3W1 Module 1

For exercise 8 in the graded lab, I am getting the following error:

There was an error grading your submission. Details: operands could not be broadcast together with shapes (0,) (8,)

I have the following output:


The number of units of HAND WARMER BIRD DESIGN sold: 14
The number of units of CHRISTMAS CRAFT TREE TOP ANGEL sold: 8

I am iterating over the range of the length of product names and appending the units_sold for the given index if it matches the condition. I am not sure if I am misunderstanding what the question is asking or if the error is something else. Everything else passes.

I’m not a mentor for that course, but this thought comes to mind:

Perhaps the test is providing a null list, and your code is expecting the same list as was used in the notebook.

The grader uses entirely different tests than the notebook, and it doesn’t provide the same global variables.

Is there anything to do on my end to debug? I’ve debugged and printed the lists, checked the variables as the loop ran, etc. and everything seems to check out. Or is this a grader issue?

@chris.favila, is this course in your scope?

hi @Joseph_Lilleberg

I am not mentor either for this course but here is how you can debug your code (I am just explaining your error)

your error indicates an issue with performing an operation (like addition, subtraction, multiplication, etc.) between two arrays or array-like objects that have incompatible shapes for NumPy’s broadcasting rules.

Explanation of the shapes:
(0,): This represents a 1-dimensional array with zero elements. It’s essentially an empty array.
(8): This represents a 1-dimensional array with 8 elements.

Why broadcasting fails:
NumPy’s broadcasting rules dictate how operations are performed on arrays of different shapes.
For two arrays to be broadcastable, their dimensions must either be equal, or one of them must be 1 (when comparing from the trailing dimension forward).

So in your case:
You have an empty array (shape (0,)) and an array with 8 elements (shape (8,)).
When attempting an operation, NumPy tries to align the dimensions. The dimension 0 and 8 are neither equal nor is one of them 1. Therefore, broadcasting cannot occur.

Common causes and solutions:

This error often arises when:
1. An array is unexpectedly empty:
Cause: A previous operation, filtering, or data loading step resulted in an empty array when a non-empty one was expected.

Solution: Debug the code leading up to the operation to ensure the array intended to have elements actually contains them. Check for empty results from functions or conditional statements.

2.Incorrect indexing or slicing:
Cause: You might be trying to access elements of an array using an index or slice that results in an empty selection, and then attempting to use this empty selection in an operation with another array.

Solution: Verify your indexing and slicing operations to ensure they are selecting the intended elements and that the resulting shape is compatible with the other operand.

3.Lastly dimension mismatch in a more complex scenario:
Cause: While the example is simple, in more complex scenarios (e.g., image processing, machine learning), a dimension might be accidentally dropped or added, leading to an empty dimension in one array while the other has a fixed size.

Solution: Carefully examine the shapes of all arrays involved in the operation using .shape and ensure they align with the broadcasting rules. You might need to reshape, add np.newaxis, or remove/add dimensions to make them compatible.

For example

import numpy as np

# Original problematic scenario (assuming x became empty)
x = np.array([]) # shape (0,)
y = np.array([1, 2, 3, 4, 5, 6, 7, 8]) # shape (8,)

try:
    result = x + y
except ValueError as e:
    print(f"Error: {e}")

# Corrected scenario: Ensure x has a compatible shape or is a scalar
x_corrected = np.array([0]) # Or a scalar like 0
result_corrected = x_corrected + y
print(f"Corrected result: {result_corrected}")

From one of older threads mentions to exercise 4. or 5 where the two lists need to have same length.

Regards
DP

1 Like

Hi Joseph. Can you send me your notebook via direct message? Will check if this is a grader issue. You can click my name then the blue Message button. Thanks!

1 Like

I am familiar with NumPy and Broadcasting, however this assignment is only dealing with python lists. If the grader is converting these to NumPy arrays, it’s likely on the backend and out of scope for this particular lab. That being said, I do appreciate the lengthy response and explanation.

1 Like

hi @Joseph_Lilleberg

the numpy array use example was just a way to explain the python list to explain your error output and not in literal way to use numpy as I don’t know the codes

but surely your error tells one of the two listed item is empty and when you tried to operands like addition, substraction, multiplication, this error was thrown.

anyways the l.t. of the course has asked for the notebook and he will probably get back to you with solution.

in case not, you can dm your codes by DM to review.

Good luck!!

I understand and thank you again for taking the time to help me. It’s very much appreciated!

Posting the resolution here for reference. The solution was fine. What probably happened is the grader received an incomplete notebook, hence the error in the last exercises. This happens sometimes too in Coursera, where you need to manually press save before submitting to ensure it gets the solved notebook. Will keep an eye on this if it comes up more often.

3 Likes