def cost(w, x, y, N): result = 0 for n in range(0, N): val = polynomial(w, x[n]) - y[n] val = val * val result = result + val return 0.5 * result / N
def gradient(w, x, y, N): grad = w * 0. I = len(w) - 1 pw = range(I + 1) for n in range(0, N): val = polynomial(w, x[n]) - y[n] phi = np.power(x[n], pw) grad = grad + phi * val return grad / N
def step(w, x, y, g, N): grad = w * 0. I = len(w) - 1 pw = range(I + 1) alpha1 = 0 alpha2 = 0 for n in range(0, N): val = polynomial(w, x[n]) - y[n] phi = np.power(x[n], pw) dot = np.dot(phi, g) alpha1 = alpha1 + val * dot alpha2 = alpha2 + dot * dot return alpha1 / alpha2