C3_W2_Collaborative_RecSys_Assignment Exercise 1

Hey everyone,

I encountered a problem while working on Exercise 1 of the C3_W2_Collaborative_RecSys_Assignment, but I managed to figure it out. I wanted to share my solution with you.

In the assignment, we were asked to implement the cost function for collaborative filtering with regularization. I initially had some issues with my code, but after reviewing the hints, I made the necessary modifications.

Here’s the updated code for the cofi_cost_func function:

pythonCopy code

def cofi_cost_func(X, W, b, Y, R, lambda_):
    nm, nu = Y.shape
    J = 0

   # moderator edit: code removed - not allowed

    return J

In this updated code, I have added the regularization term to the cost function calculation using the formula moderator edit - code removed. This helps prevent overfitting and improves the accuracy of the collaborative filtering model.

After making these modifications, the code produced the correct results for the provided test cases. However, I noticed that there were some discrepancies in the expected outputs mentioned in the comments. The correct output values for the test cases should be as follows:

  • For the “Evaluate cost function with regularization” test: Expected output is 28, which matches the calculated result.
  • For the “Public tests” case: The expected output is 27, but the provided code does not seem to account for regularization correctly. I suspect that the expected output might be incorrect in this case.

I am struggling with the reconciliation effort with the expected output too. I get 28.5 for the “Evaluate cost with regularization” test and 54 for the “public tests” case. Can certainly use some guidance to wrap this practice lab up.

The expected value for the regularized test (to two decimal places) is 28.09. Your answer will have additional digits, but they’re not really significant for the purposes of this test.

The “public tests” actually contain five different tests - only one of them gives the result 27.0.

Note: I’m going to delete the code from your post, since sharing your code on the forum for a graded assignment is not allowed by the Code of Conduct.

hi @TMosh , I am getting this error , for this excercise


Can you please advice on this ?

AssertionError Traceback (most recent call last)
1 # Public tests
2 from public_tests import *
----> 3 test_cofi_cost_func(cofi_cost_func)

~/work/public_tests.py in test_cofi_cost_func(target)
14 J = target(X_r, W_r, b_r, Y_r, R_r, 2);
15 assert not np.isclose(J, 13.5), f"Wrong value. Got {J}. Did you multiply the regularization term by lambda_?"
—> 16 assert np.isclose(J, 27), f"Wrong value. Expected {27}, got {J}. Check the regularization term"

AssertionError: Wrong value. Expected 27, got inf. Check the regularization term

hi @TMosh , I am able to resolve .
Please ignore the question

1 Like

I get the same error. Can you help me?

Sorry, i am currently not available.