Skip to content

Commit 5a5b477

Browse files
committed
mods wrt OLS, homework
- projection consistently in SVD - new books added - OLS link in homework, improved comments - typo
1 parent 46d7cca commit 5a5b477

File tree

3 files changed

+62
-25
lines changed

3 files changed

+62
-25
lines changed

homework/homework.ipynb

+16-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"\n",
2424
"# Homework Template\n",
2525
"\n",
26-
"Make sure that you copy the template and the groudn truth data into mnt/home/... to have persistent storage. All data in the virtual machine is lost, once the virtual machine is deleted."
26+
"Make sure that you copy the template and the ground truth data into mnt/home/... to have persistent storage. All data in the virtual machine is lost, once the virtual machine is deleted."
2727
]
2828
},
2929
{
@@ -135,6 +135,14 @@
135135
"plt.tight_layout()"
136136
]
137137
},
138+
{
139+
"cell_type": "markdown",
140+
"id": "ee27e362",
141+
"metadata": {},
142+
"source": [
143+
"We might get an idea to use a linear model, i.e. **linear regression** / ordinary least squares, which we have dealt with in exercise 5 [Line Fit with Linear Regression](../line_fit_linear_regression.ipynb). The code below will do this, but the model performance is rather not convincing, as the data originates in fact from a non-linear model. Hence, we need to go for a non-linear model...and thus we should solve the homework task :)"
144+
]
145+
},
138146
{
139147
"cell_type": "code",
140148
"execution_count": 6,
@@ -152,17 +160,18 @@
152160
"source": [
153161
"# simple linear model, train with full data set\n",
154162
"x_left_inverse = np.linalg.inv(x.T @ x) @ x.T\n",
155-
"w = x_left_inverse @ y # get weights via left inverse = train the model\n",
156-
"y_predict = x @ w # predict = forward propagation\n",
163+
"w = x_left_inverse @ y # get model weights via left inverse = fit/ train the model\n",
164+
"y_predict = x @ w # predict = forward propagation, often also denoted y_hat\n",
157165
"\n",
158-
"# get residual e, loss L & empirical risk ER\n",
166+
"# get error / residual e, get loss L & get empirical risk ER\n",
159167
"e = y - y_predict\n",
160168
"L = e.T @ e\n",
161169
"ER = L / N\n",
162170
"print('empirical risk:', ER[0, 0]) # 0.003427800540873722 = 3.427800540873722e-3\n",
163-
"# non-linear model in homeweork task has ER = 5.7457115873891e-5\n",
164-
"# so it might explain the data better than the simple non-linear model\n",
165-
"# in fact the ground truth data y=f(x) originates from a non-linear function f\n",
171+
"# non-linear model in homework task has ER = 5.7457115873891e-5\n",
172+
"# so it might explain the data better than the simple linear model above\n",
173+
"#\n",
174+
"# in fact the ground truth data y=f(x) originates from a non-linear function f,\n",
166175
"# hence a linear model must somehow fail to do a good prediction job"
167176
]
168177
},

index.ipynb

+9-7
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,25 @@
158158
"## Textbook Recommendations\n",
159159
"Machine Learning (ML) using linear / non-linear models is a vivid topic and dozens of textbooks will be released each year.\n",
160160
"The following textbook recommendations are very often referenced in the field and brilliant to learn with. \n",
161+
"- Kevin P. **Murphy**: *Probabilistic Machine Learning: An Introduction*, MIT Press, 1st. ed. [open source book and current draft as free pdf](https://probml.github.io/pml-book/book1.html)\n",
162+
"- J.A. **Fessler**, R.R. **Nadakuditi**: *Linear Algebra for Data Science, Machine Learning, and Signal Processing*, Cambridge University Press, 2024, 1st ed. [online ebook](https://ebookcentral.proquest.com/lib/ubrostock-ebooks/detail.action?docID=31691281)\n",
161163
"- Sebastian **Raschka**, Yuxi Liu, Vahid Mirjalili: *Machine Learning with PyTorch and Scikit-Learn*, Packt, 2022, 1st ed.\n",
162164
"- Gilbert **Strang**: *Linear Algebra and Learning from Data*, Wellesley, 2019, consider to buy your own copy of this brilliant book\n",
163165
"- Gareth **James**, Daniela Witten, Trevor Hastie, Rob Tibshirani: *An Introduction to Statistical Learning* with Applications in R, Springer, 2nd ed., 2021, [free pdf e-book](https://www.statlearning.com/)\n",
164166
"- Trevor **Hastie**, Robert Tibshirani, Jerome Friedman: *The Elements of Statistical Learning: Data Mining, Inference, and Prediction*, Springer, 2nd ed., 2009, [free pdf e-book](https://hastie.su.domains/ElemStatLearn/)\n",
165167
"- Sergios **Theodoridis**: *Machine Learning*, Academic Press, 2nd ed., 2020, check your university library service for free pdf e-book\n",
166-
"- Kevin P. **Murphy**: *Probabilistic Machine Learning: An Introduction*, MIT Press, 1st. ed. [open source book and current draft as free pdf](https://probml.github.io/pml-book/book1.html)\n",
167168
"- Ian **Goodfellow**, Yoshua Bengio, Aaron Courville: *Deep Learning*, MIT Press, 2016\n",
168169
"- Marc Peter **Deisenroth**, A. Aldo Faisal, Cheng Soon Ong: *Mathemathics for Machine Learning*, Cambridge University Press, 2020, [free pdf e-book](https://mml-book.github.io/)\n",
169170
"- Steven L. **Brunton**, J. Nathan Kutz: *Data Driven Science & Engineering - Machine Learning, Dynamical Systems, and Control*, Cambridge University Press, 2020, [free pdf of draft](http://www.databookuw.com/databook.pdf), see also the [video lectures](http://www.databookuw.com/) and [Python tutorials](https://github.com/dylewsky/Data_Driven_Science_Python_Demos)\n",
170171
"- Aurélien **Géron**: *Hands-on machine learning with Scikit-Learn, Keras and TensorFlow*. O’Reilly, 2nd ed., 2019, [Python tutorials](https://github.com/ageron/handson-ml2)\n",
171172
"\n",
172-
"ML deals with stuff that is actually known for decades (at least the linear modeling part of it), so if we are really serious about to learn ML deeply, we should think over concepts on statistical signal processing, maximum-likelihood, Bayesian vs. frequentist statistics, generalized linear models, hierarchical models...For these topics we could check these respected textbooks\n",
173-
"- L. **Fahrmeir**, A. Hamerle, and G. Tutz, Multivariate statistische Verfahren, 2nd ed. de Gruyter, 1996.\n",
174-
"- L. **Fahrmeir**, T. Kneib, S. Lang, and B. D. Marx, Regression, 2nd ed. Springer, 2021.\n",
175-
"- A. J. **Dobson** and A. G. Barnett, An Introduction to Generalized Linear Models, 4th ed. CRC Press, 2018.\n",
176-
"- H. **Madsen**, P. Thyregod, Introduction to General and Generalized Linear Models, CRC Press, 2011.\n",
177-
"- A. **Agresti**, Foundations of Linear and Generalized Models, Wiley, 2015"
173+
"ML deals with stuff that is actually known for decades (at least the linear modeling part of it), so if we are really serious about to learn ML deeply, we should elaborate concepts on statistical signal processing, maximum-likelihood, Bayesian vs. frequentist statistics, generalized linear models, hierarchical models...For these topics we could check these respected textbooks\n",
174+
"- L. **Fahrmeir**, A. Hamerle, and G. Tutz, *Multivariate statistische Verfahren*, 2nd ed. de Gruyter, 1996.\n",
175+
"- L. **Fahrmeir**, T. Kneib, S. Lang, and B. D. Marx, *Regression*, 2nd ed. Springer, 2021.\n",
176+
"- A. J. **Dobson** and A. G. Barnett, *An Introduction to Generalized Linear Models*, 4th ed. CRC Press, 2018.\n",
177+
"- J. F. **Monahan**, *A Primer on Linear Models*, CRC Press, 2008.\n",
178+
"- H. **Madsen**, P. Thyregod, *Introduction to General and Generalized Linear Models*, CRC Press, 2011.\n",
179+
"- A. **Agresti**, *Foundations of Linear and Generalized Models*, Wiley, 2015"
178180
]
179181
},
180182
{

line_fit_linear_regression.ipynb

+37-11
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@
4747
"import matplotlib.pyplot as plt\n",
4848
"import numpy as np\n",
4949
"from scipy.linalg import svd, diagsvd, inv, pinv, norm\n",
50-
"from numpy.linalg import matrix_rank"
50+
"from numpy.linalg import matrix_rank\n",
51+
"\n",
52+
"np.set_printoptions(precision=3,\n",
53+
" floatmode='maxprec',\n",
54+
" suppress=True)"
5155
]
5256
},
5357
{
@@ -193,7 +197,7 @@
193197
"metadata": {},
194198
"outputs": [],
195199
"source": [
196-
"e = y - y_hat # e == y_lns\n",
200+
"e = y - y_hat # e == y_left_null\n",
197201
"e, e.T @ e"
198202
]
199203
},
@@ -203,7 +207,10 @@
203207
"metadata": {},
204208
"outputs": [],
205209
"source": [
206-
"y_col.T @ e # column space is ortho to left null space"
210+
"# recap: y_hat = y_col, e = y_left_null\n",
211+
"# y = y_col + y_lef_null = y_hat + e\n",
212+
"# hence\n",
213+
"y_hat.T @ e # column space is ortho to left null space"
207214
]
208215
},
209216
{
@@ -212,8 +219,7 @@
212219
"metadata": {},
213220
"outputs": [],
214221
"source": [
215-
"# projection matrices\n",
216-
"\n",
222+
"# projection matrices:\n",
217223
"P_col = X @ Xli\n",
218224
"P_col, P_col @ y, y_col"
219225
]
@@ -224,8 +230,8 @@
224230
"metadata": {},
225231
"outputs": [],
226232
"source": [
227-
"# check projection in terms of SVD\n",
228-
"S @ Sli, np.allclose(U @ S @ Sli @ U.T, P_col)"
233+
"# check P_col projection in terms of SVD\n",
234+
"S @ Sli, np.allclose(U @ (S @ Sli) @ U.T, P_col)"
229235
]
230236
},
231237
{
@@ -238,6 +244,16 @@
238244
"P_left_null, P_left_null @ y, e"
239245
]
240246
},
247+
{
248+
"cell_type": "code",
249+
"execution_count": null,
250+
"metadata": {},
251+
"outputs": [],
252+
"source": [
253+
"# check P_left_null projection in terms of SVD\n",
254+
"np.eye(M) - S @ Sli, np.allclose(U @ (np.eye(M) - S @ Sli) @ U.T, P_left_null)"
255+
]
256+
},
241257
{
242258
"cell_type": "code",
243259
"execution_count": null,
@@ -254,8 +270,8 @@
254270
"metadata": {},
255271
"outputs": [],
256272
"source": [
257-
"# check projection in terms of SVD\n",
258-
"Sli @ S, np.allclose(V @ Sli @ S @ V.T, P_row)"
273+
"# check P_row projection in terms of SVD\n",
274+
"Sli @ S, np.allclose(V @ (Sli @ S) @ V.T, P_row)"
259275
]
260276
},
261277
{
@@ -268,6 +284,16 @@
268284
"P_null # null space is spanned only by zero vector"
269285
]
270286
},
287+
{
288+
"cell_type": "code",
289+
"execution_count": null,
290+
"metadata": {},
291+
"outputs": [],
292+
"source": [
293+
"# check P_null projection in terms of SVD\n",
294+
"np.allclose(V @ (np.eye(N) - Sli @ S) @ V.T, P_null)"
295+
]
296+
},
271297
{
272298
"cell_type": "code",
273299
"execution_count": null,
@@ -285,9 +311,9 @@
285311
" ms=10, mew=3,\n",
286312
" label='data')\n",
287313
"# fitted line\n",
288-
"plt.plot(X[:,1], theta_hat[0] * X[:,0] + theta_hat[1] * X[:,1], 'k', label='LS fit (interpolation)')\n",
314+
"plt.plot(X[:,1], theta_hat[0] * X[:,0] + theta_hat[1] * X[:,1], 'k', label='least squares fit (interpolation)')\n",
289315
"x = np.linspace(0, 1, 10)\n",
290-
"plt.plot(x, theta_hat[0] + theta_hat[1] * x, 'C7:', label='LS fit (extrapolation)')\n",
316+
"plt.plot(x, theta_hat[0] + theta_hat[1] * x, 'C7:', label='least squares fit (extrapolation)')\n",
291317
"x = np.linspace(4, 5, 10)\n",
292318
"plt.plot(x, theta_hat[0] + theta_hat[1] * x, 'C7:')\n",
293319
"\n",

0 commit comments

Comments
 (0)