I’m trying this code below for with and with out regularization and the result for with out regularization is not right. Please advise on my mistake:

if lambda_ == 0:
for j in range(nu):
w = W[j,:]
b_j = b[0,j]
for i in range(nm):
x = X[i,:]
y = Y[i,j]
r = R[i,j]
J += np.square(r * (np.dot(w,x) + b_j - y ) )
J = J/2
else:
J += (lambda_/2) * (np.sum(np.square(W)) + np.sum(np.square(X)))
### END CODE HERE ###
return J

It is important to pay attention to two things when refactoring code.

Firstly, the regularization term does not require an if-else condition. If the reg_term is equal to zero, then the corresponding term will be 0 (multiplied by 0). You can view the cost function in greater detail here: