def plot_from_beta(beta): plt.figure() xplot = np.linspace(0, 1, n_n) yplot = np.linspace(0, 1, n_n) xplot, yplot = np.meshgrid(xplot, yplot) zplot = np.zeros((n_n, n_n)) for i in range(n_n): for j in range(n_n): Xplot = create_X(xplot[i, j], yplot[i, j], degree) zplot[i, j] = Xplot.dot(beta) fig = plt.imshow(zplot.T, cmap='gray') plt.xlabel('X') plt.ylabel('Y') return fig
def fit_terrain_data(terrain1, degree=15, reg_type='OLS', k=5, alpha=0, x_splits=4, y_splits=8, plot_every_area=False): OLS = reg_type == 'OLS' Ridge = reg_type == 'Ridge' Lasso = reg_type == 'Lasso' ny, nx = terrain1.shape mx = int(nx / x_splits) my = int(ny / y_splits) terrain1 = terrain1[:my * y_splits, :mx * x_splits] z_final_predict = np.zeros(terrain1.shape) area_error = np.zeros(y_splits * x_splits) for i_x in range(x_splits): for j_y in range(y_splits): #print( 'areas left=', x_splits*y_splits - (j_y + (i_x)*y_splits)) terrain = terrain1[my * j_y:my * (j_y + 1), mx * i_x:mx * (i_x + 1)] Nx = terrain.shape[0] Ny = terrain.shape[1] x = np.zeros((mx, my)) y = np.zeros((mx, my)) x_line = np.linspace(0, 1, mx) y_line = np.linspace(0, 1, my) for i in range(mx): for j in range(my): x[i, j] = x_line[i] y[i, j] = y_line[j] x = x.flatten() y = y.flatten() z = terrain.flatten() xy = np.c_[x, y] X_plot = create_X(x, y, degree) X = create_X(x, y, degree) kf = oh.k_fold(k) X_rest, X_test, z_rest, z_test = train_test_split( X, z, test_size=int(z.shape[0] / k), shuffle=True) betas, errors, betaSigma = CV_fit(X_rest, z_rest, k, alpha=0, method=reg_type) best_i = np.argmin(errors) beta = betas[best_i, :] area_error[j_y + (i_x) * y_splits] = np.mean(errors, axis=0) z_plot1 = X_plot.dot(beta).reshape(terrain.shape) z_final_predict[my * j_y:my * (j_y + 1), mx * i_x:mx * (i_x + 1)] = z_plot1[:, :] if plot_every_area: plt.figure() plt.title( 'Terrain over Norway, area{}'.format(j_y + (i_x) * y_splits)) plt.imshow(terrain, cmap='gray') plt.xlabel('X') plt.ylabel('Y') plt.figure() plt.title( 'Terrain over Norway prediction, area{}'.format(j_y + (i_x) * y_splits)) plt.imshow(z_plot1, cmap='gray') plt.xlabel('X') plt.ylabel('Y') plt.show() _min = -2 _max = 2.5 fig1 = plt.figure() plt.imshow(z_final_predict, cmap='gray', vmin=_min, vmax=_max) plt.xlabel('X') plt.ylabel('Y') fig2 = plt.figure() plt.imshow(terrain1, cmap='gray', vmin=_min, vmax=_max) plt.title('Terrain') plt.xlabel('X') plt.ylabel('Y') #plt.show() mse = np.mean((z_final_predict - terrain1)**2) print('mse over all points=', mse) print('area error', np.mean(area_error)) print('R2 score', oh.R2_score(z_final_predict, terrain1)) return fig1, fig2
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) #fig.colorbar(surf, shrink=0.5, aspect=5) n = 1000 x_ = np.random.rand(n) y_ = np.random.rand(n) z = oh.frankeFunction(x_, y_) + 0.1 * np.random.randn(n) # Set up the design matrix MSE = [] R2_score = [] for grad in range(1, 6): X = oh.create_X(x_, y_, grad) invXTX = np.linalg.inv(X.T @ X) # Need this anyway #beta = invXTX @ X.T @ z beta = oh.linFit(X, z) ztilde = X @ beta MSE.append(oh.mse(z, ztilde)) R2_score.append(oh.R2_score(z, ztilde)) #sigma = np.sqrt(np.var(ztilde)) #print("Sigma numpy: ", sigma) sigma = np.sqrt(1 / (X.shape[0] - X.shape[1] - 1) * np.sum( (z - ztilde)**2)) #print("Sigma self: ", sigma) betaSigma = np.zeros(len(beta)) relative = betaSigma
Ny = terrain.shape[1] x_m = np.zeros((mx, my)) y_m = np.zeros((mx, my)) x_line = np.linspace(0, 1, mx) y_line = np.linspace(0, 1, my) for i in range(mx): for j in range(my): x_m[i, j] = x_line[i] y_m[i, j] = y_line[j] x = x_m.flatten() y = y_m.flatten() z = terrain.flatten() xy = np.c_[x, y] X_plot = create_X(x, y, degree) X = create_X(x, y, degree) #X, X_trash, x ,x_trash, y, t_trash, z, z_trash = train_test_split(X, x, y, z, test_size=0.8, shuffle=True) #splitting the data in train and test data X_rest, X_test, x_rest, x_test, y_rest, y_test, z_rest, z_test = train_test_split( X, x, y, z, test_size=3 / 5, shuffle=True) f_rest = z_rest number_of_train = len(z_rest) print('number of z_values in train', number_of_train) lamErrOLS = np.zeros(lambdas.shape[0]) lamErrRidge = np.zeros(lambdas.shape[0]) lamErrLasso = np.zeros(lambdas.shape[0]) numBetas = X_rest.shape[1]
variancesRidge = np.zeros((lambdas.shape[0], degrees.shape[0])) biasesRidge = np.zeros((lambdas.shape[0], degrees.shape[0])) biasefsRidge = np.zeros((lambdas.shape[0], degrees.shape[0])) errorsRidge = np.zeros((lambdas.shape[0], degrees.shape[0])) r2sRidge = np.zeros((lambdas.shape[0], degrees.shape[0])) variancesLasso = np.zeros((lambdas.shape[0], degrees.shape[0])) biasesLasso = np.zeros((lambdas.shape[0], degrees.shape[0])) biasefsLasso = np.zeros((lambdas.shape[0], degrees.shape[0])) errorsLasso = np.zeros((lambdas.shape[0], degrees.shape[0])) r2sLasso = np.zeros((lambdas.shape[0], degrees.shape[0])) kf = oh.k_fold(n_splits=k - 1, shuffle=True) degree = 5 X = oh.create_X(x.ravel(), y.ravel(), degree, intercept=True) zPredictsOLS = np.empty((int(z.shape[0] / k), k)) #zTrainOLS = [] #np.empty( (int(z.shape[0]), k) ) zTrainOLS = np.empty((int(z.shape[0]), k)) zPredictsRidge = np.empty((int(z.shape[0] / k), k)) zPredictsLasso = np.empty((int(z.shape[0] / k), k)) zTests = np.empty((int(z.shape[0] / k), k)) X_rest, X_test, x_rest, x_test, y_rest, y_test, z_rest, z_test, f_rest, f_test = train_test_split( X, x, y, z, f, test_size=int(z.shape[0] / k), shuffle=True) #lambdaRidge, lambdaLasso = oh.findBestLambda(X_rest, z_rest, f_rest, k, lambdas, degree) kf.get_n_splits(X_rest) for nlam, _lambda in enumerate(lambdas):
z2 = z.reshape(2, 2) assert oh.bias(z, z) + oh.var(z, z) == oh.mse(z, z) assert oh.bias(z, z + 1) + oh.var(z, z + 1) == oh.mse(z, z + 1) print("The function bias(z,ztilde) works as advertised") # Test R2 score print("Testing the R2 score") z = np.arange(4) + 1 assert oh.R2_score(z, z) == 1 print("Testing a matrix") z2 = z.reshape(2, 2) assert oh.R2_score(z, z) == 1 print("The function R2_score(z,ztilde) works as advertised") print("Testing linear Regression functions") x = np.random.randn(11) y = np.random.randn(11) X = oh.create_X(x, y, 2) np.set_printoptions(precision=2) print(X) z = x + y zTildeOLS = X @ oh.linFit(X, z, model='OLS') assert oh.mse(z, zTildeOLS) < 10**(-28) print("OLS works as advertised") zTildeRidge = X @ oh.linFit(X, z, model='Ridge', _lambda=0.1) assert oh.mse(z, zTildeOLS) < 10**(-28) print("Ridge works as advertised") zTildeLasso = X @ oh.linFit(X, z, model='Lasso', _lambda=0.1) assert oh.mse(z, zTildeOLS) < 10**(-28) print("Ridge works as advertised")
plt.imshow(terrain_train) plt.title("Training sample") plt.show() plt.imshow(terrain_test) plt.title("Test sample") plt.show() plt.imshow(terrain_validate) plt.title("Validation sample") plt.show() x = np.linspace(0, 1, terrain.shape[0]) y = np.linspace(0, 1, terrain.shape[1]) x, y = np.meshgrid(x, y) x = x.ravel().reshape(-1, 1) y = y.ravel().reshape(-1, 1) xy = oh.create_X(x.ravel(), y.ravel(), 7, intercept=True) print("X: ", xy.shape) polyGons = 11 # Sklearn PCA to reduce down to 2 dims! pca = PCA(n_components=0.95) x_train = np.linspace(0, 1, terrain.shape[0])[pX_train] y_train = np.linspace(0, 1, terrain.shape[1])[pY_train] x_train, y_train = np.meshgrid(x_train, y_train) x_train = x_train.ravel().reshape(-1, 1) y_train = y_train.ravel().reshape(-1, 1) xy_train = oh.create_X(x_train.ravel(), y_train.ravel(), polyGons, intercept=False)