All numbers in y_ are same

All the numbers in y_ are same in boston_house prediction code.


Here is my code-

Hi @sanchit123manchanda
Where have you defined yk in your code? I couldn’t find it anywhere.

I have corrected it. Pls check now

Your implementation of gradient() function had a bug.
Correct:

def gradient(X, y, theta):
    m, n = X.shape

    grad = np.zeros((n,))
    for j in range(n):
        for i in range(m):
            y_ = hypothesis(X[i],theta)
            grad[j] += (y_ - y[i])*X[i][j]

    return grad/m

Yours:

def gradient(X,y,theta):
    m,n = X.shape
    
    grad = np.zeros((n,))
    
    # for all values of j
    for j in range(n):
        # sum over all examples
        for i in range(m):
            y_ = hypothesis(X[i],theta)
            grad[j] += (y_ - y[j])*X[i][j]
    # out of bothe th loops
    
    return grad/m

Notice that you were accessing y[j] instead of y[i] when computing the gradients.

Hope this helps! You can now mark the issue as resolved in the “My Doubts” section.

1 Like