# preprocess data scaler = StandardScaler() scaler.fit(X_train) X_train = scaler.transform(X_train) X_test = scaler.transform(X_test) # fit beta, beta_var = Lasso(X_train, z_train, lambda_=1e-5, r_var=1) x_surf = np.linspace(0, terrain.shape[1], terrain.shape[1]) y_surf = np.linspace(0, terrain.shape[0], terrain.shape[0]) X_surf, Y_surf, Z_surf = prep_surface(x_surf, y_surf, beta, deg=3, scaler=scaler) fig = plt.figure() ax = plt.axes(projection="3d") ax.plot_surface(X_surf, Y_surf, Z_surf, cmap="autumn") ax.set_title(f"Terrain data lasso approximation,\n deg=3 and " \ + r"$\lambda=10^{-4}$", fontsize=15) ax.set_xlabel("x", fontsize=12) ax.set_ylabel("y", fontsize=12) ax.set_zlabel("z", fontsize=12) plt.savefig(f"Figures/terrain_lasso_deg3.png", dpi=300) plt.show() plt.imshow((Z_surf - terrain)**2,
X = design_matrix(x, y, n=5) X_train, X_test, z_train, z_test = train_test_split(X, z, test_size=0.35) # preprocess data scaler = StandardScaler() scaler.fit(X_train) X_train = scaler.transform(X_train) X_test = scaler.transform(X_test) # fit beta, beta_var = Ridge(X_train, z_train, lambda_=1e-2, r_var=1) x_surf = np.linspace(0, 1, 100) y_surf = np.linspace(0, 1, 100) X_surf, Y_surf, Z_surf = prep_surface(x_surf, y_surf, beta, deg=5) fig = plt.figure() ax = plt.axes(projection="3d") ax.plot_surface(X_surf, Y_surf, Z_surf, cmap="autumn") ax.set_title(f"Franke Function Ridge approximation,\n deg=5, " \ + f"N=100 and " + r"$\lambda=10^{-2}$", fontsize=15) ax.set_xlabel("x", fontsize=12) ax.set_ylabel("y", fontsize=12) ax.set_zlabel("z", fontsize=12) plt.savefig(f"Figures/franke_ridge_deg5_N100.png", dpi=300) plt.show() plt.imshow((Z_surf - FrankeFunction(X_surf, Y_surf))**2, cmap="RdYlGn", origin="lower", extent=[X_surf.min(), X_surf.max(), Y_surf.min(),
MSE_test[i-1], R2_test[i-1] = MSE_R2(z_hat_test, z_test) # bootstrap MSE_boots[i-1], bias_boots[i-1], var_boots[i-1] = bias_variance_tradeoff(X, z, B) # kfold MSE_kfold[i-1], std_kfold[i-1] = kfold(k, X, z) print(f"Completed loops: {i}") # plot example approximations if i == 5 or i == 10: x_surf = np.linspace(0, terrain.shape[1], terrain.shape[1]) y_surf = np.linspace(0, terrain.shape[0], terrain.shape[0]) X_surf, Y_surf, Z_surf = prep_surface(x_surf, y_surf, beta, deg=i, scaler=scaler_train) fig = plt.figure() ax = plt.axes(projection="3d") ax.plot_surface(X_surf, Y_surf, Z_surf, cmap="autumn") ax.set_title(f"Terrain OLS approximation, deg={i}", fontsize=15) ax.set_xlabel("x", fontsize=12) ax.set_ylabel("y", fontsize=12) ax.set_zlabel("z", fontsize=12) plt.savefig(f"Figures/terrain_OLS_deg{i}.png", dpi=300) plt.show() plt.imshow((Z_surf - terrain)**2, cmap="RdYlGn", extent=[X_surf.min(), X_surf.max(), Y_surf.max(), Y_surf.min()]) plt.title(f"OLS prediction error, deg={i}", fontsize=15)