MLS Course 1 Week 2 Linear Regression Graded Lab - Getting incorrect cost values

Receiving error: AssertionError: Case 2: Cost must be 2 but got 0.5. Exercise 1.

Have used all hints to create code, using optional labs as guides for code structure also - not understanding how it is outputting 0.5 instead of 2, and unsure how to resolve since the majority of the code used is from your hint boxes. Tried to search for this question but was not finding an answer.
Thank you for any insight you are able to provide.

Hello Tyler @Tyler_Hodgson,

Welcome to our community! Since I suppose this is your first assignment for the MLS, let me suggest the way we can debug our code for this and future assignment.

The error you see is from one of the public tests which are used for learners to check whether the code is okay before submitting to the grader, and that’s why you can check out the tests themselves. If you click “File” > “Open” > “”, you will see all the tests for this assignment and among which the compute_cost_test function should be our focus because this is the exercise you are working on I suppose, and in which “Case 2” is the test case in question from the error you shared here.

So you see that there are only 4 data points in the test, and so it’s completely feasible for us to debug the code by comparing:

  1. the cost, calculated with a calculator, by using the formula presented in the description of the exercise
  2. the cost, calculated with a calculator, by following through the code you have implemented for your exercise.

Being able to debug your code is a necessary skill to do machine learning because we need to code to do machine learning. Plugging in numbers into the expected formula and into the code to check for consistency is a very common way of debugging.

Good luck!

Thank you very quick response! I came back to my work a few hours later and realized I had not formatted my for loop correctly and was missing part of the equation from where it should have been. I’m sure I will run into similar scenarios in the next exercises so I will try to use the public tests you suggested in the future, thank you again for your quick reply!

1 Like

Hello @Tyler_Hodgson,

Thank you for letting me know. It is also in my experience that taking a short break is helpful for debugging, and since you talk about formatting, I would also like to bring up about indentation since it might be helpful for other learners who find your thread. Indentation is one of the common error I have seen here and so I wrote this post of less than 5 minutes read time for anyone who wants to know how to indent correctly and effectively.

Being able to debug ourselves make our own machine learning journeys a more effective and hopfully more fruitful ones!