i have tried this code for linear regression hardwork pays of problem but its showing error
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dfx=pd.read_csv(‘C:\Users\Kashu\Downloads\Linear_X_Train1.csv’)
dfy=pd.read_csv(‘C:\Users\Kashu\Downloads\Linear_Y_Train1.csv’)
x=dfx.values
y=dfy.values
print(x.shape)
plt.scatter(x,y)
X=(x-x.mean())/x.std()
Y=y
plt.scatter(X,Y)
plt.show()
def hypothesis(xv,theta):
return theta[0]+theta[1]*xv
def error(X,Y,theta):
err=0
m=X.shape[0]
for i in range(m) :
hx=hypothesis(X[i],theta)
err+=(hx-Y[i])**2
return err
##grad
def gradient(X,Y,theta):
grad=np.zeros((2,))
m=X.shape[0]
for i in range(m):
hx=hypothesis(X[i],theta)
grad[0]+= (hx-Y[i])
grad[1]+= (hx-Y[i])*X[i]
return grad
def gradientDescent(X,Y,learning_rate=0.007) :
error_list = []
theta=np.zeros((2,))
for i in range(10000):
grad=gradient(X,Y,theta)
error_list.append(error(X,Y,theta))
theta[0]=theta[0]-learning_rategrad[0]
theta[1]=theta[1]-learning_rategrad[1]
return error_list,theta
error_list,f_theta= gradientDescent(X,Y)
plt.plot(error_list)
print(f_theta)
ERROR
c:\users\kashu\desktop\python\lib\site-packages\ipykernel_launcher.py:9: RuntimeWarning: overflow encountered in square
if name == ‘main’:
c:\users\kashu\desktop\python\lib\site-packages\ipykernel_launcher.py:22: RuntimeWarning: overflow encountered in add
c:\users\kashu\desktop\python\lib\site-packages\ipykernel_launcher.py:21: RuntimeWarning: invalid value encountered in add
c:\users\kashu\desktop\python\lib\site-packages\ipykernel_launcher.py:37: RuntimeWarning: invalid value encountered in double_scalars