heta will be np.array([theta0,theta1,…,thetaN])
def hypothesis(X,theta):
returning h(Xi) for each sample (Xi) hence returning an array of size X.shape[0]
return np.sum(X*theta[1:], axis=1) + theta[0]
def gradient(X,Y,theta):
grad = np.zeros(X.shape[1]+1) # n-featured data have (n+1) parameters.
hx = hypothesis(X,theta)
grad[0] = np.sum((hx-Y))
for i in range(1,len(grad)) :
d(cost) / d(theta[i]) = sum((hx-y)x[i])
grad[i] = np.sum((hx-Y)(X[:,i-1]))
return grad
def error(X,Y,theta):
hx = hypothesis(X,theta)
return np.sum((hx-Y)**2)
def gradientDescent_multivariate(X,Y,learning_rate=0.01):
theta = np.zeros(X.shape[1]+1)
grad = gradient(X,Y,theta)
error_list = []
err = error(X,Y,theta)
error_list.append(err)
while True:
theta = theta - learning_rate * grad
err = error(X,Y,theta)
print(err)
temp = abs(err - error_list[-1])
if temp < 0.00001:
break
error_list.append(err)
return theta
my doubt is :- when i am pritning err it is increasing instead of decreasing hence my algo is not going to converge , i did everything right , i think so can u have a look ,