Vectorized Compute Gradient for Multiple Variable Linear Regression

def compute_gradient(X, y, w, b): 
    """
    Computes the gradient for linear regression 
    Args:
      X (ndarray (m,n)): Data, m examples with n features
      y (ndarray (m,)) : target values
      w (ndarray (n,)) : model parameters  
      b (scalar)       : model parameter
      
    Returns:
      dj_dw (ndarray (n,)): The gradient of the cost w.r.t. the parameters w. 
      dj_db (scalar):       The gradient of the cost w.r.t. the parameter b. 
    """
    
# moderator edit: code removed
        
    return dj_db, dj_dw

If you have better implementation feel free to share with me.

Welcome to the community!

Please do not post your code on the forum. Because this is a graded assignment, it’s not allowed by the community guidelines. I have edited your post to remove the code.

Thanks for warning me.
My intension was vectorization of that function.
Can you please delete this topic @TMosh

Hello @alperenunlu,

Does your dj_dw has a shape of (n, )? It seems not summing up over samples. You have used np.mean in dj_db to sum something up but I don’t see any action of summing things in dj_dw.

You are posting this topic in C1 W2. Which lab is it?

Cheers,
Raymond