# MLS Course 3 Week 2 Collaborative Filtering Ex. 1 - Python Syntax

I ran the cost function without regularization and got the expected output for lambda = 0. I believe I coded the addition of the regularization term correctly, but am not getting the expected output for lambda = 1.5

I suspect my issue is indentation, but what is a reliable way to debug when there is a lot of nested summation going on?

Hi,

I would break the operation into the individual terms then combine them at the end.
After if performance is an issue I would optimize.

Also, if you need help, you can send me your code in a direct message and I’ll take a look.

Hi @Richard_Rasiej You are going in the right direction there must be a calculation error not logical.
As @SamReiswig mentioned, you should do the calculation in 2 steps it will make the calculation easy and have less chance of error. I share one such method you should try.
I ran the cost function without regularization and got the expected output for lambda = 0.
For 0 you didn’t get any error true but for 1.5 regularization term effect a lot.

I believe I coded the addition of the regularization term correctly,
You should check again, I think you divide the first term by 4 rather than 2 I can only confirm after seeing the code so send me in inbox message

I suspect my issue is indentation, but what is a reliable way to debug when there is a lot of nested summation going on?

As shown in the image perform the first term and add it to variable j
Make sure you do not divide and square the first term
In the second step add the regularization term and add the above “j” value with square and square it and the whole second equation should be divided by 2 as every term consist of 2 in the denominator.

Hi there I’m experiencing the same problem multiple times. I Have had many expected outputs as well as the correct output eg… 21.17 …157.90 … 42,13 …43,08 please help I have read the instructions above but i’m unable to come up with a right representation or perhaps I don’t understand what you or colleagues are trying to say because when I try that I get cost values of 500 , 2000+

or 3000+ with regularization added in the equation

Hi, @Junta442 rather than writing a whole equation in one code use two codes which eventually helps to reduce errors. And I also replied to your personal message just check it out.

I finally got it, thanks!! I need to be more careful when I examine older code that indentation is consistent. Also need to be careful not to divide by 2 twice.

@VIVEK_PATEL1 Thanks a million !!! I opened up my mind to your logic

1 Like