here is my code:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
from sklearn.datasets import make_regression
#get or create a Dataset
X,y = make_regression(n_samples=10000,n_features=20,n_informative=20,noise=10,random_state=1)
#Normalisation
u = np.mean(X,axis=0)
std = np.std(X,axis=0)
X = (X-u)/std
#Add a column of 1's for vectorization
ones = np.ones((X.shape[0],1))
X = np.hstack((ones,X))
print(X.shape)
print(y.shape)
(10000, 21)
(10000,)
def hypothesis(X,theta):
return np.dot(X,theta)
def error(X,y,theta):
e = 0.0
m = X.shape[0]
y_ = hypothesis(X,theta)
e = np.sum((y-y_)**2)
return e/m
def gradient(X,y,theta):
y_ = hypothesis(X,theta)
grad = np.dot(X.T,(y - y_))
m = X.shape[0]
return grad/m
def gradient_descent(X,y,learning_rate = 0.01,max_iters=300):
n = X.shape[1]
theta = np.zeros((n,))
error_list = []
for i in range(max_iters):
e = error(X,y,theta)
error_list.append(e)
#Batch Gradient descent -> computr thr Gradient wrt entire dataset
grad = gradient(X,y,theta)
theta = theta - learning_rate*grad
return theta,error_list
Batch Gradient descent - 300 iteration , lr = 0.01
theta,error_list = gradient_descent(X,y)
plt.figure()
plt.plot(np.arange(len(error_list)),error_list)
plt.show()
In this snip, I’m getting this increment. Help me where I’m getting wrong.