C2_W3_Assignment - AssertionError in eval_cat_err()

As part of the final W3 assignment, in Section 4 Excercise 2, we have to write a simple code eval_cat_err(y, yhat) to calculate the categorization error. When running the Unit Tests for this code I get the correct expected categorization error values as below, but also an Assertion error . I’ve checked my code against that provided in the hints, it’s exactly the same. I’ve tried clearing browser cache and restarting my laptop, no difference. So how to remove this error?

categorization error 0.333, expected:0.333
categorization error 0.250, expected:0.250
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - – - - - -
AssertionError Traceback (most recent call last)
in
7
8 # BEGIN UNIT TEST
----> 9 test_eval_cat_err(eval_cat_err)
10 # END UNIT TEST
11 # BEGIN UNIT TEST

~/work/public_tests_a1.py in test_eval_cat_err(target)
46 y_tmp = np.array([1, 1, 1, 0, 0, 0])
47 result = target(y_hat, y_tmp)
—> 48 assert np.isclose(result, 3./6., atol=1e-6), f"Wrong value. Expected 0.5, but got {result}"
49
50 y_hat = np.array([[1], [2], [0], [3]])

AssertionError: Wrong value. Expected 0.5, but got 0.16666666666666666

Hi!

Can you send me your code in a direct message so I can help?

Thanks for your lightening prompt reply Sam. Have pasted my code below. I can’t see any obvious difference between my code and the code in the Hints.
Best Regards
Sumitro

UNQ_C2

GRADED CELL: eval_cat_err

def eval_cat_err(y, yhat):
“”"
Calculate the categorization error
Args:
y : (ndarray Shape (m,) or (m,1)) target value of each example
yhat : (ndarray Shape (m,) or (m,1)) predicted value of each example
Returns:|
cerr: (scalar)
“”"
m = len(y)
incorrect = 0
for i in range(m):
### START CODE HERE ###
if y[i]!=yhat[i]:
incorrect=+1
cerr=incorrect/m
### END CODE HERE ###

return(cerr)

Hi,

I’m unable to reproduce the error.
Can you finish the lab and try to submit it?

Hi Sam,

I finished the lab. Along the way I noticed that the categorization error was always 0.003
The results I got for the categorization error for the two models were

categorization error, training, simple model, 0.003, complex model: 0.003
categorization error, cv, simple model, 0.003, complex model: 0.003

So the cell was actually computing incorrectly.

I submitted and got this grading:
Code Cell UNQ_C1: Function ‘eval_mse’ is correct.
Code Cell UNQ_C2: Function ‘eval_cat_err’ is incorrect. Check implementation.
Code Cell UNQ_C3: The value of your variable ‘model’ is correct.
Code Cell UNQ_C4: The value of your variable ‘model_s’ is correct.
Code Cell UNQ_C5: The value of your variable ‘model_r’ is correct.

In desperation I deleted the middle statement
incorrect+=1
and retyped it. And it ran without the assertion error !!

The rest of the assignment then gave correct values:
categorization error, training, simple model, 0.062, complex model: 0.003
categorization error, cv, simple model, 0.087, complex model: 0.122

i re-submitted and got 100%

So the problem is gone if not solved. The only thing i can think of is that that particular Jupiter Notebook cell somehow got corrupted. Is that possible? In your experience have you ever encountered a situation where re-typing fixed the problem?

Thanks
Sumitro

Hi Sumitro,

Going back over your code I noticed this line.

incorrect =+ 1

=+ and += are unfortunately not the same.

Sorry I didn’t notice this typo before.

Sam

Thanks for pointing out that typo. That removes the mystery. I typed it correctly the second time.
Best wishes
Sumitro