// error is
IndexError Traceback (most recent call last)
in
62
63
—> 64 Y_test=hypothesis(X_test,theta)
65
66
in hypothesis(x, theta)
23 n=x.shape[0]
24 for i in range (n):
—> 25 y_+=(theta[i]*x[i])
26 return y_
27
IndexError: index 6 is out of bounds for axis 0 with size 6
code
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
boston=load_boston()
#X1=boston.data
#Y1=boston.target
#X=pd.DataFrame(X1).values
#Y=pd.DataFrame(Y1).values
Xv=pd.read_csv(“x_sample.csv”).values
normalisation part
a=Xv.shape[1]
X=Xv[: ,:a-1]
Y=Xv[ : ,a-1:a]
u=np.mean(X,axis=0)
std=np.std(X,axis=0)
X=(X-u)/std
ones=np.ones((X.shape[0],1))
X=np.hstack((ones,X))
def hypothesis(x,theta):
y_=0.0
n=x.shape[0]
for i in range (n):
y_+=(theta[i]*x[i])
return y_
def error (X,Y,theta):
e=0.0
m=X.shape[0]
for i in range(m):
y_=hypothesis(X[i],theta)
e+=(y_-Y[i])**2
return e/m
def gradient(X,Y,theta):
m,n=X.shape
grad=np.zeros((n,))
for j in range(n):
for i in range(m):
y_=hypothesis(X[i],theta)
grad[j]+=(Y[i]-y_)*X[i][j]
return grad/m
def gradec(X,Y,lr=0.1,max=300):
m,n=X.shape
theta=np.zeros((n,))
for j in range(300):
grad=gradient(X,Y,theta)
for i in range(X,Y,theta):
theta[i]=theta[i]-lr*grad[i]
return theta
theta=graddec(X,Y)
print(theta)
X_test=pd.read_csv(“xtest.csv”).values
one=np.ones((X_test.shape[0],1))
X_test=np.hstack((one,X_test))
Y_test=hypothesis(X_test,theta)
df=pd.DataFrame(data=Y_test,columns=[“target”])
df.to_csv(“airquality.csv”,index=true)
