"""用验证集比较复杂度,选定后才使用测试集。""" import json # NumPy 构建设计矩阵和计算均方误差,Matplotlib 对比不同阶数的拟合曲线。 import matplotlib.pyplot as plt import numpy as np def predict(x, coefficient): """幂次从 0 到 d,第一列对应截距。""" return np.vander(x, N=len(coefficient), increasing=True) @ coefficient def main(): # 模拟现象的真关系是 y=2x+1,训练观测加固定扰动。 train_x = np.arange(-3.0, 4) train_y = 2 * train_x + 1 + np.array([.6, -.9, .8, -.8, .7, -.6, .3]) val_x = np.array([-2.5, -.5, 1.5, 2.5]) val_y = 2 * val_x + 1 test_x = np.array([-2.75, -.25, .75, 2.75]) test_y = 2 * test_x + 1 # 每条多项式有 degree+1 个系数。三组候选的参数长度不同,用列表保存。 degrees = [1, 3, 6] candidates, train_mse, val_mse = [], [], [] for degree in degrees: # 只用训练集拟合。除以 3 让幂次矩阵的数值范围更适中。 design = np.vander(train_x / 3, degree + 1, increasing=True) # lstsq 直接最小化平方残差,返回元组的第一项是系数;无需构造正规方程。 coefficient = np.linalg.lstsq(design, train_y, rcond=None)[0] candidates.append(coefficient) train_mse.append(np.square(predict(train_x / 3, coefficient) - train_y).mean()) val_mse.append(np.square(predict(val_x / 3, coefficient) - val_y).mean()) # argmin 给出最小验证 MSE 的位置。模型选定后,测试集只用于一次评价。 winner = int(np.argmin(val_mse)) test_mse = np.square(predict(test_x / 3, candidates[winner]) - test_y).mean() assert train_mse[-1] < 1e-20 and val_mse[-1] > val_mse[0] assert degrees[winner] == 1 # 圆点是训练观测,叉号是验证观测;连续曲线用于看高阶模型在点间的摆动。 grid = np.linspace(-3, 3, 121) fig, ax = plt.subplots(figsize=(6, 3.6), layout="constrained") ax.scatter(train_x, train_y, label="Training observations") ax.scatter(val_x, val_y, marker="x", label="Validation observations") for degree, coefficient in zip(degrees, candidates): ax.plot(grid, predict(grid / 3, coefficient), label=f"Degree {degree}") ax.set(xlabel="x", ylabel="y") ax.legend() print("LAB_RESULT " + json.dumps({ "degree": degrees[winner], "train_mse": train_mse, "val_mse": val_mse, "test_mse": test_mse, })) plt.show() if __name__ == "__main__": main()