1
votes

I have these gradient descent algorithm for multivariate regression but it raises an

ValueError: operands could not be broadcast together with shapes (3,) (3,140).

I checked out other answers on broadcasting errors on stackoverflow and the documentation which says the dimensions of the matrices must be same or either of the matrix must be 1.But how can i make my theta of the same dimension.

Please don't mark it duplicate.

My x has dim (140,3) , y has (140,1), alpha=0.0001

def find_mse(x,y,theta):
    return np.sum(np.square(np.matmul(x,theta)-y))*1/len(x)       



def gradientDescent(x,y,theta,alpha,iteration):
    theta=np.zeros(x.shape[1])
    m=len(x)
    gradient_df=pd.DataFrame(columns=['coeffs','mse'])

    for i in range(iteration):
        gradient = (1/m) * np.matmul(x.T, np.matmul(x, theta) - y)
        theta = np.mat(theta) - alpha * gradient
        cost = compute_cost(X, y, theta)
        gradient_df.loc[i] = [theta,cost]

    return gradient_df   
1
Welcome to SO; it is impossible for us to help simply by reading your code, please see How to create a Minimal, Complete, and Verifiable example - desertnaut
Which line specifically throws the error? - Miriam Farber
2nd line in the loop....which is generating the error. theta=theta-alpha*gradient - dutta.ari

1 Answers

0
votes

Your are multiplying x with shape (140, 3) with theta to produce output which should have shape (140, 1). To achieve this your theta should have shape of (3, 1). You need to initialize theta as following

theta = np.zeros((x.shape[1], y.shape[1]))