I wrote the following code while solving the assignment, but i am not able to retrieve the predicted values.
I get the correct output plot but I am not able to get the final Ypredic values
number = X.shape[1]
def hypothesis(x,theta,number):
total = 0
total+= theta[0]
for i in range(len(x)):
total+=theta[i+1]*x[i]
return total
def error(X,Y,theta):
m = X.shape[0]
n = X.shape[1]
err = 0
for i in range(m):
hx = hypothesis(X[i],theta,n)
err+= (hx-Y[i])**2
return err
def gradient(X,Y,theta):
m = X.shape[0]
n = X.shape[1]
grad = np.zeros((n+1,))
for i in range(m):
hx = hypothesis(X[i],theta,n)
grad[0] += (hx - Y[i])
for j in range(n):
grad[j+1]+=(hx-Y[i])*(X.item((i,j)))
return grad
def gradientDescent(X,Y,learning_rate=0.0001):
m = X.shape[0]
n = X.shape[1]
theta = np.zeros((n+1,))
itr = 0
max_itr = 100
err_list = []
while(itr<=max_itr):
grad = gradient(X,Y,theta)
err = error(X,Y,theta)
err_list.append(err)
theta[0] = theta[0] - learning_rate*grad[0]
for i in range(1,n+1):
theta[i] = theta[i] - learning_rate*grad[i]
itr+=1
return theta,err_list
final_theta,error_list = gradientDescent(X,Y)
testDataSize = xtest.shape[0]
no = xtest.shape[1]
ypredic = []
for i in range(testDataSize):
ypredic.append(hypothesis(xtest[i],final_theta,no))
Please help me,since i am stuck since two days.