Please debug this code of multivariate linear regression

def hypothesis(theta,x):
return np.dot(x,theta)

def error(X,Y,theta):
tot_error = 0
m = X.shape[0]
for i in range(m):
tot_error+=(Y[i]-hypothesis(theta,X[i]))**2
return tot_error/m

def gradient(Y,X,theta):
m,n=X.shape
grad= np.zeros((n,))
for j in range(n):
for i in range(m):
y_ = hypothesis(theta,X[i])
grad[j]+=(y_-Y[i])*X[i][j]
return grad/m

def gradientDescent(X,Y,learning_rate,max_itr):
m,n = X.shape
theta=np.zeros((n,))
error_list = []
for i in range(max_itr):
grad = gradient(Y,X,theta)
e = error(X,Y,theta)
error_list.append(e)
for j in range(n):
theta[j]=theta[j]-learning_rate*grad[j]
return theta,error_list

theta,error_list = gradientDescent(X,Y,learning_rate=0.1,max_itr=100)
print(theta)
print(error_list)
plt.plot(error_list)

Great work done, I checked your code for the dataset names “linearX.csv” and “linearY.csv”.
Your learning rate was quite high, i decreased it to 0.001 and the code was running perfectly fine.

Just make sure of one thing as well, since you are not taking bias term, so you need to add column of ones in xdataset. Ignore if already done, but if not then do it like this:
ones = np.ones((xdata.shape[0],1))
xdata = np.hstack((xdata,ones))

Hope this helped :blush:

https://drive.google.com/file/d/1SHifrAVJixwEO-j44BKtsjSfvmwEhlOu/view?usp=sharing

Still the error graph is a increasing graph.Also,when I am increasing no. of iterations to 300,I am getting "RuntimeWarning: overflow encountered in double_scalars "

You have not normalized your data, I normalized it first and then added column of ones and your code ran perfectly fine.

Here is the link for correct code.

Happy Learning :smile: