From 27c697273375a41a33617bfd901dc59342beafc2 Mon Sep 17 00:00:00 2001 From: lionp0 Date: Wed, 20 Nov 2024 18:37:22 +0100 Subject: [PATCH 1/2] Added Todos with numerations to the first part --- src/regularization.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/regularization.py b/src/regularization.py index 38479df..c04d282 100644 --- a/src/regularization.py +++ b/src/regularization.py @@ -24,13 +24,13 @@ def set_up_point_matrix(axis_x: np.ndarray, degree: int) -> np.ndarray: np.ndarray: The polynomial point matrix A. """ mat_a = np.zeros((len(axis_x), degree)) - # TODO implement me! + # TODO 1.1.1 implement me! return mat_a if __name__ == "__main__": b_noise_p = pandas.read_csv("./data/noisy_signal.tab", header=None) - b_noise = np.asarray(b_noise_p) + b_noise = np.asarray(b_noise_p) # This is the artificial data we work with x_axis = np.linspace(0, 1, num=len(b_noise)) @@ -38,3 +38,22 @@ def set_up_point_matrix(axis_x: np.ndarray, degree: int) -> np.ndarray: plt.show() # TODO put your code here! + + # 1.1 Regression + + # 1.1.2 Create the point-matrix A for n=2 + + # 1.1.3 Calculate the estimated coefficients for the polynomial + # You can use np.linalg.pinv to compute the Pseudo-Inverse + + # 1.1.4 Plot the original data as well as the estimated polynomial by evaluating it. + + # 1.2 Higher order Polynomial + + # 1.2.1 Create the point-matrix A for n=300 + + # 1.2.2 Calculate the estimated coefficients for the polynomial + + # 1.2.3 Plot the original data as well as the estimated polynomial by evaluating it. + + # 1.3 Regularization From 967f17509bd1e534b39e7cb6b324db9df7d13146 Mon Sep 17 00:00:00 2001 From: lionp0 Date: Wed, 20 Nov 2024 19:18:25 +0100 Subject: [PATCH 2/2] Added Todos with numerations to the second part --- src/pegel_bonn.py | 42 ++++++++++++++++++++++++++++++++++++++++++ src/regularization.py | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/pegel_bonn.py b/src/pegel_bonn.py index 933924f..d373229 100644 --- a/src/pegel_bonn.py +++ b/src/pegel_bonn.py @@ -20,7 +20,10 @@ day, month, year = ts_date.split(".") hour, minute = ts_time.split(":") datetime_list.append(datetime(int(year), int(month), int(day))) + if hour == " 04" or hour == " 05": + keep_indices.append(idx) + # 2.3.0 # uncomment to start in the year 2000. # levels = levels[:-30_000:4] # datetime_list = datetime_list[:-30_000:4] @@ -37,3 +40,42 @@ plt.ylabel("Water level [cm]") plt.title("Rhine water level in Bonn") plt.show() + + # 2.1 Regression + + # 2.1.1 Set up the point matrix for n=2 + # The corresponding function that you have implemented in Part 1 has already been imported and is ready to use! + + # 2.1.2 Estimate the coefficients + + # 2.1.3 Evaluate the straight line and plot your result + + # 2.1.4 Calculate the data where the Rhine will be dried out + + + + # 2.2 Fitting a higher-order Polynomial + + # 2.2.1 Take a look at the datetime_stamps and scale the axis + + # 2.2.2 Set up the point matrix with n=20 + + # 2.2.3 Compute the coefficients for the polynomial + + # 2.2.4 Evaluate the polyomial + + # 2.2.5 Plot the results + + + + # 2.3 Regularization + + # 2.3.0 Uncomment the lines of code above + + # 2.3.1 Compute the SVD of the point-matrix for n=20 + + # 2.3.2 Compute the filter matrix + + # 2.3.3 Estimate the coefficients by applying regularization + + # 2.3.4 Plot the evaluated, regularized polynomial diff --git a/src/regularization.py b/src/regularization.py index c04d282..7eda9c4 100644 --- a/src/regularization.py +++ b/src/regularization.py @@ -37,7 +37,7 @@ def set_up_point_matrix(axis_x: np.ndarray, degree: int) -> np.ndarray: plt.plot(x_axis, b_noise, ".", label="b_noise") plt.show() - # TODO put your code here! + # TODO put your code for Part 1 here! # 1.1 Regression @@ -48,6 +48,8 @@ def set_up_point_matrix(axis_x: np.ndarray, degree: int) -> np.ndarray: # 1.1.4 Plot the original data as well as the estimated polynomial by evaluating it. + + # 1.2 Higher order Polynomial # 1.2.1 Create the point-matrix A for n=300 @@ -56,4 +58,35 @@ def set_up_point_matrix(axis_x: np.ndarray, degree: int) -> np.ndarray: # 1.2.3 Plot the original data as well as the estimated polynomial by evaluating it. + + # 1.3 Regularization + + # 1.3.1 Compute the SVD of A + + # 1.3.2 Compute the filter matrix + + # 1.3.3 Estimate the coefficients by applying regularization + + # 1.3.4 Plot your results + + #----------------------------------------------------------------------------------------# + # Optional Task 1.4 Model Complexity + + # For every degree from 1 to 20: + + # 1.4.1 Set up the point matrix for the current degree + + # 1.4.2 Estimate the coefficients for the polynomial via the pseudoinverse + + # 1.4.3 Compute the predictions by evaluating the estimated polynomial at the x-values + + # 1.4.4 Compute the MSE between the predictions and the original b-values + + # 1.4.5 Plot the MSE-error against the degree + + # 1.4.6 Take a look at the graph with all 20 MSE's and see if there is a link between degree and MSE + # Estimate the optimal degree of polynomial and fit the polynomial with this new degree + # --> so perform the usual steps but only once with the optimal degree + + # Plot the result