Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.2 kB
5
Indexable
Never
def compute_gradient(X, y, w, b,lamda_ = None): 
    """
    Computes the gradient for logistic regression 
 
    Args:
      X : (ndarray Shape (m,n)) variable such as house size 
      y : (array_like Shape (m,1)) actual value 
      w : (array_like Shape (n,1)) values of parameters of the model      
      b : (scalar)                 value of parameter of the model 
      lambda_: unused placeholder.
    Returns
      dj_dw: (array_like Shape (n,1)) 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. 
    """
    m, n = X.shape
    dj_dw = np.zeros(w.shape)
    dj_db = 0.

    ### START CODE HERE ### 
    for i in range(m):
        z_wb = np.dot(X[i],w)+b
        for a in range(n):
            z_wb_ia = X[i][a]*w[a]
            z_wb += z_wb_ia
        z_wb += b
        f_wb = sigmoid(z_wb)
        dj_db_i = f_wb - y[i]
        dj_db += dj_db_i
        for j in range(n):
            dj_dw_ij = f_wb - y[i]*X[i][j]
            dj_dw[j] += dj_dw_ij
    dj_dw = (1/m)*dj_dw
    dj_db = (1/m)*dj_db
    ### END CODE HERE ###

        
    return dj_db, dj_dw