I want to build a linear regression model to predict y values. I have my X and Y values as NumPy arrays. However, the model doesn't give the correct value even when the given dataset is straightforward to learn (for ex: x= [10, 15], y=[20, 30]). I am new to machine learning and it is my first experience in this field. Thank you for your help :)
Here is the code:
x = [10, 15, 23, 30, 40, 49, 57, 60]
y = [10000, 21000,25000, 30000,45000, 52000, 65000, 70000]
x = np.array(x)
y = np.array(y)
n = len(x)
m = np.ones(n)
b = np.ones(n)
def hypothesis(x, m, b):
h = x*m + b
return h
def cost(y, h):
c = (1/(2*n))*(np.sum(np.square(h - y)))
return c
def grad_des(x, y, h, theta, L):
theta = theta - L*(x/n)*(np.sum(h - y))
return theta
iter = 100000
L= 0.00005
for i in range(iter):
h = hypothesis(x, m, b)
c = cost(y, h)
m = grad_des(x, y, h, m, L)
b = grad_des(x, y, h, b, L)
if i%10 == 0:
print('h:', h, "c:", c,)