Skip to content

Commit 12533db

Browse files
authored
Merge pull request #9 from Machine-Learning-Foundations/src_code
Added Todos with numerations to the first part
2 parents 12241f6 + 967f175 commit 12533db

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

src/pegel_bonn.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
day, month, year = ts_date.split(".")
2121
hour, minute = ts_time.split(":")
2222
datetime_list.append(datetime(int(year), int(month), int(day)))
23+
if hour == " 04" or hour == " 05":
24+
keep_indices.append(idx)
2325

26+
# 2.3.0
2427
# uncomment to start in the year 2000.
2528
# levels = levels[:-30_000:4]
2629
# datetime_list = datetime_list[:-30_000:4]
@@ -37,3 +40,42 @@
3740
plt.ylabel("Water level [cm]")
3841
plt.title("Rhine water level in Bonn")
3942
plt.show()
43+
44+
# 2.1 Regression
45+
46+
# 2.1.1 Set up the point matrix for n=2
47+
# The corresponding function that you have implemented in Part 1 has already been imported and is ready to use!
48+
49+
# 2.1.2 Estimate the coefficients
50+
51+
# 2.1.3 Evaluate the straight line and plot your result
52+
53+
# 2.1.4 Calculate the data where the Rhine will be dried out
54+
55+
56+
57+
# 2.2 Fitting a higher-order Polynomial
58+
59+
# 2.2.1 Take a look at the datetime_stamps and scale the axis
60+
61+
# 2.2.2 Set up the point matrix with n=20
62+
63+
# 2.2.3 Compute the coefficients for the polynomial
64+
65+
# 2.2.4 Evaluate the polyomial
66+
67+
# 2.2.5 Plot the results
68+
69+
70+
71+
# 2.3 Regularization
72+
73+
# 2.3.0 Uncomment the lines of code above
74+
75+
# 2.3.1 Compute the SVD of the point-matrix for n=20
76+
77+
# 2.3.2 Compute the filter matrix
78+
79+
# 2.3.3 Estimate the coefficients by applying regularization
80+
81+
# 2.3.4 Plot the evaluated, regularized polynomial

src/regularization.py

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,69 @@ def set_up_point_matrix(axis_x: np.ndarray, degree: int) -> np.ndarray:
2424
np.ndarray: The polynomial point matrix A.
2525
"""
2626
mat_a = np.zeros((len(axis_x), degree))
27-
# TODO implement me!
27+
# TODO 1.1.1 implement me!
2828
return mat_a
2929

3030

3131
if __name__ == "__main__":
3232
b_noise_p = pandas.read_csv("./data/noisy_signal.tab", header=None)
33-
b_noise = np.asarray(b_noise_p)
33+
b_noise = np.asarray(b_noise_p) # This is the artificial data we work with
3434

3535
x_axis = np.linspace(0, 1, num=len(b_noise))
3636

3737
plt.plot(x_axis, b_noise, ".", label="b_noise")
3838
plt.show()
3939

40-
# TODO put your code here!
40+
# TODO put your code for Part 1 here!
41+
42+
# 1.1 Regression
43+
44+
# 1.1.2 Create the point-matrix A for n=2
45+
46+
# 1.1.3 Calculate the estimated coefficients for the polynomial
47+
# You can use np.linalg.pinv to compute the Pseudo-Inverse
48+
49+
# 1.1.4 Plot the original data as well as the estimated polynomial by evaluating it.
50+
51+
52+
53+
# 1.2 Higher order Polynomial
54+
55+
# 1.2.1 Create the point-matrix A for n=300
56+
57+
# 1.2.2 Calculate the estimated coefficients for the polynomial
58+
59+
# 1.2.3 Plot the original data as well as the estimated polynomial by evaluating it.
60+
61+
62+
63+
# 1.3 Regularization
64+
65+
# 1.3.1 Compute the SVD of A
66+
67+
# 1.3.2 Compute the filter matrix
68+
69+
# 1.3.3 Estimate the coefficients by applying regularization
70+
71+
# 1.3.4 Plot your results
72+
73+
#----------------------------------------------------------------------------------------#
74+
# Optional Task 1.4 Model Complexity
75+
76+
# For every degree from 1 to 20:
77+
78+
# 1.4.1 Set up the point matrix for the current degree
79+
80+
# 1.4.2 Estimate the coefficients for the polynomial via the pseudoinverse
81+
82+
# 1.4.3 Compute the predictions by evaluating the estimated polynomial at the x-values
83+
84+
# 1.4.4 Compute the MSE between the predictions and the original b-values
85+
86+
# 1.4.5 Plot the MSE-error against the degree
87+
88+
# 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
89+
# Estimate the optimal degree of polynomial and fit the polynomial with this new degree
90+
# --> so perform the usual steps but only once with the optimal degree
91+
92+
# Plot the result

0 commit comments

Comments
 (0)