线性回归问题实战
Linear Equation(线性方程):y = w * x + b
实现步骤
1:根据随机初始化的 w x b 和 y 来计算 loss
2:根据当前的 w x b 和 y 的值来计算梯度
3:更新梯度,循环将新的 w′ 和 b′ 复赋给 w 和 b ,最终得到一个最优的 w′ 和 b′ 作为方程最终的参数
Step1:Compute Loss
def computer_error_for_line_given_points(b, w, points):
totalError = 0
for i in range(0, len(points)):
x = points[i, 0]
y = points[i, 1]
totalError += (y - (w * x + b)) ** 2
return totalError / float(len(points))
Step2:Compute Gradient and update
def step_gradient(b_current, w_current, points, learningRate):
b_gradient = 0
w_gradient = 0
N = float