0.工具
import copy, math, sys
import numpy as np
1.线性回归模型
def f_wb(x,w,b):return np.dot(w,x) + b
2.成本函数
def compute_cost(X, y, w, b):m,_ = X.shapeJ_wb = 0.0for i in range(m):J_wb += (f_wb(X[i],w,b) - y[i])**2J_wb /= 2*mreturn J_wb
3.梯度计算
def compute_gradient(X, y, w, b):m,n = X.shapedj_dw = np.zeros(n)dj_db = 0.0for i in range(m):err = f_wb(X[i],w,b) - y[i]for j in range(n):dj_dw[j] += err*X[i,j]dj_db += errfor j in range(n):dj_dw[j] /= mdj_db /= mreturn dj_dw, dj_db
4.梯度下降法
def gradient_descend(X, y, w_init, b_init, alpha, num_iters):m,n = X.shapeJ_history = []p_history = []w = copy.deepcopy(w_init)b = b_initfor t in range(num_iters):dj_dw, dj_db = compute_gradient(X, y, w, b)w_temp = w - alpha * dj_dwb_temp = b - alpha * dj_dbw = w_tempb = b_tempJ_history.append(compute_cost(X, y, w, b))if t% math.ceil(num_iters / 10) == 0:print(f"Iteration {t:4d}: Cost {J_history[-1]:8.2f} ")return w, b, J_history
5.测试算法正确性
X_train = np.array([[2104, 5, 1, 45], [1416, 3, 2, 40], [852, 2, 1, 35]])
y_train = np.array([460, 232, 178])
m,n = X_train.shapew_initial = np.zeros(n)
b_initial = 0.0iterations = 1000
alpha = 1e-7w_final, b_final, J_hist = gradient_descend(X_train, y_train, w_initial, b_initial, alpha, iterations)print(f"b,w found by gradient descent: {b_final:0.2f},{w_final} ")
for i in range(m):print(f"prediction: {np.dot(X_train[i], w_final) + b_final:0.2f}, target value: {y_train[i]}")
输出结果如下
Iteration 0: Cost 28989.11
Iteration 100: Cost 696.86
Iteration 200: Cost 696.65
Iteration 300: Cost 696.43
Iteration 400: Cost 696.21
Iteration 500: Cost 696.00
Iteration 600: Cost 695.78
Iteration 700: Cost 695.57
Iteration 800: Cost 695.36
Iteration 900: Cost 695.14
b,w found by gradient descent: -0.00,[ 0.20253263 0.00112386 -0.00213202 -0.00933401]
prediction: 425.71, target value: 460
prediction: 286.41, target value: 232
prediction: 172.23, target value: 178