计算每一步的梯度更新
def step_gradient(b_current, w_current, points, learningRate):
b_gradient = 0
w_gradient = 0
N = float(len(points))
for i in range(0, len(points)):
x = points[i,0]
y = points[i,1]
b_gradient += -(2/N) * (y - ((w_current * x) + b_current)) #求导结果
w_gradient += -(2/N) * x * (y-((w_current * x) + b_current)) #求导结果
new_b = b_current - (learningRate * b_gradient)
new_w = w_current - (learningRate * w_gradient)
return [new_b, new_w]
梯度下降过程
def gradient_decent_runner(points, starting_b, starting_w, learning_rate, num_iterations)
b = starting_b
w = starting_w
for i in range(num_iterations):
b, w = step_gradient(b, m, np.array(points), learning_rate)
return [b, w]