'how can I turn my linear regression model from univariate into the multvariate?
I have built this univariate linear regression model from scratch and I am conceptually ok with how the multivariate version of it works.
the problem is that how can I code it ?
can you please show it to me by just changing and modifying my code.
thanks.
this is the model:
#THE HYPOTHESIS FUNCTION
def H0(theta0,theta1,X_norm):
h = [(theta0 + (theta1 * X_norm[i]) ) for i in range(len(X_norm))]
return h
#THE COST FUNCTION
def cost_function(h,y_norm):
cost = (1/(2*len(y_norm))) * sum([(h[i] - y_norm[i] )**2 for i in range(len(y_norm))])
return cost
#THE GRADIENT DESCNET
def gradient_descent(theta0,theta1,h,X_norm,y_norm):
a = 1
theta0 = theta0 - a * (1/len(y_norm)) * sum([ (h[i] - y_norm[i]) for i in range(len(y_norm))])
theta1 = theta1 - a * (1/len(y_norm)) * sum([ ( (h[i] - y_norm[i] ) * X_norm[i] ) for i in range(len(y_norm))])
return theta0,theta1
#THE TRAINING STEP
def train(theta0,theta1,X_norm,y_norm,n):
costs = []
for i in range(n):
h = H0(theta0,theta1,X_norm)
cost = cost_function(h,y_norm)
theta0,theta1 = gradient_descent(theta0,theta1,h,X_norm,y_norm)
costs.append(cost)
return theta0,theta1,costs
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
