一. 多项式拟合
import numpy as np
import matplotlib.pyplot as plt
def cost_function(w, b, data):
total_cost = 0
M = len(data)
for i in range(M):
x = data[i, 0]
y = data[i, 1]
total_cost = 0.5/M * (np.square(y-w*x-b)).sum()
return total_cost
def step_gradient(b_current, w_current, data, alpha):
b_sum = 0.0
w_sum = 0.0
M = float(len(data))
# 对每个点代入公式求和
for i in range(0, len(data)):
x = data[i, 0]
y = data[i, 1]
tmp = w_current * x + b_current - y
b_sum += tmp
w_sum += tmp * x
# 用公式求当前梯度
grad_w