def hypothesis(X, theta):
return np.dot(X,theta)
def gradient(X, y, theta):
y_= hypothesis(X, theta)
print(y_)
print(y_-y)
grad = np.dot(X.T,(y_-y))
m = X.shape[0]
print(grad)
return grad/m
def error(X, y, theta):
m = X.shape[0]
y_ = hypothesis(X,theta)
e = np.sum((y-y_)**2)
return e/m
def grd_d(X, y, learning_r = 0.1, max_iters = 100):
n = X.shape[1]
error_list = []
theta = np.zeros((n,1))
theta = theta.T
print(theta)
for i in range(max_iters):
e = error(X, y, theta)
error_list.append(e)
grad = gradient(X, y,theta)
print(grad)
theta = theta - learning_r*grad
return theta,error_list
X = pd.read_csv(’./Training Data/Train/Train.csv’)
print(X)
X = X.to_numpy()
y = X[:,[5,]]
print(y)
print(y)
X = X[:,[0,1,2,3,4]]
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))
print(X)
print(X.shape[1])
theta,error = grd_d(X, y)
plt.plot(error)
print(theta)
X_test = pd.read_csv(’./Test Cases/Test/Test.csv’)
X_test = X_test.to_numpy()
print(theta)
y_ = []
ones = np.ones((X_test.shape[0],1))
X_test = np.hstack((ones,X_test))
m = X_test.shape[0]
for i in range(m):
pred = hypothesis(X[i], theta)
y_.append(pred)
y_ = np.array(y_)
print(y_)
df = pd.DataFrame(data = y_, columns = [“target”])
df.to_csv(‘ans.csv’)