Skip to content

Commit dc0c035

Browse files
committed
bias/var plots
1 parent b952e9f commit dc0c035

8 files changed

+35
-22
lines changed

bias_variance_linear_regression.ipynb

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@
331331
},
332332
{
333333
"cell_type": "code",
334-
"execution_count": null,
334+
"execution_count": 64,
335335
"id": "7d969206",
336336
"metadata": {},
337337
"outputs": [],
@@ -343,7 +343,7 @@
343343
},
344344
{
345345
"cell_type": "code",
346-
"execution_count": null,
346+
"execution_count": 65,
347347
"id": "f18dc6d5",
348348
"metadata": {},
349349
"outputs": [],
@@ -391,7 +391,7 @@
391391
},
392392
{
393393
"cell_type": "code",
394-
"execution_count": null,
394+
"execution_count": 66,
395395
"id": "5e9a45d9",
396396
"metadata": {},
397397
"outputs": [],
@@ -418,15 +418,17 @@
418418
"source": [
419419
"# generate 'true' data with the design matrix of 'true' model\n",
420420
"y = X @ beta\n",
421-
"plt.figure(figsize=(5, 3))\n",
421+
"plt.figure(figsize=(6, 3))\n",
422422
"plt.plot(y, \"k-\")\n",
423423
"plt.xlabel(\"independent features' input variable x\")\n",
424424
"plt.ylabel((\"dependent variable y, true data\"))\n",
425425
"plt.title(\"true model data as linear model (x -> 4 features + intercept)\")\n",
426426
"plt.xlim(0, M)\n",
427427
"plt.ylim(-2, 8)\n",
428428
"plt.grid(True)\n",
429-
"print(X.shape, y.shape)"
429+
"print(X.shape, y.shape)\n",
430+
"plt.tight_layout()\n",
431+
"plt.savefig('bias_variance_plots/true_data.png', dpi=300)"
430432
]
431433
},
432434
{
@@ -583,7 +585,9 @@
583585
"X = np.copy(x)[:, None]\n",
584586
"fig, axs = plt.subplots(2, 2, figsize=(10, 5))\n",
585587
"bias_variance_of_model(X)\n",
586-
"axs[0, 0].set_title(\"underfit, too low model complexity, high bias, low var\");"
588+
"axs[0, 0].set_title(\"underfit, too low model complexity, high bias, low var\");\n",
589+
"plt.tight_layout()\n",
590+
"plt.savefig('bias_variance_plots/too_simple_model.png', dpi=300)"
587591
]
588592
},
589593
{
@@ -618,7 +622,9 @@
618622
"# note that intercept is only added in function bias_variance_of_model(X)\n",
619623
"fig, axs = plt.subplots(2, 2, figsize=(10, 5))\n",
620624
"bias_variance_of_model(X)\n",
621-
"axs[0, 0].set_title(\"overfit, too high model complexity, low bias, high var\");"
625+
"axs[0, 0].set_title(\"overfit, too high model complexity, low bias, high var\");\n",
626+
"plt.tight_layout()\n",
627+
"plt.savefig('bias_variance_plots/too_complex_model.png', dpi=300)"
622628
]
623629
},
624630
{
@@ -652,7 +658,9 @@
652658
"bias_variance_of_model(X) # lowest possible bias^2+variance, because we\n",
653659
"# know the true model (again: which in practice likely never will occur)\n",
654660
"# the remaining variance is from the added noise\n",
655-
"axs[0, 0].set_title(\"true model features, lowest bias, lowest var\");"
661+
"axs[0, 0].set_title(\"true model features, lowest bias, lowest var\");\n",
662+
"plt.tight_layout()\n",
663+
"plt.savefig('bias_variance_plots/true_model.png', dpi=300)"
656664
]
657665
},
658666
{
@@ -684,7 +692,9 @@
684692
"# note that intercept is only added in function bias_variance_of_model(X)\n",
685693
"fig, axs = plt.subplots(2, 2, figsize=(10, 5))\n",
686694
"bias_variance_of_model(X)\n",
687-
"axs[0, 0].set_title(\"reasonable bias/var trade-off if true model is unknown\");"
695+
"axs[0, 0].set_title(\"reasonable bias/var trade-off if true model is unknown\");\n",
696+
"plt.tight_layout()\n",
697+
"plt.savefig('bias_variance_plots/robust_model.png', dpi=300)"
688698
]
689699
},
690700
{
@@ -721,7 +731,7 @@
721731
"kernelspec": {
722732
"display_name": "myddasp",
723733
"language": "python",
724-
"name": "myddasp"
734+
"name": "python3"
725735
},
726736
"language_info": {
727737
"codemirror_mode": {
@@ -733,7 +743,7 @@
733743
"name": "python",
734744
"nbconvert_exporter": "python",
735745
"pygments_lexer": "ipython3",
736-
"version": "3.10.6"
746+
"version": "3.12.3"
737747
}
738748
},
739749
"nbformat": 4,
Loading

bias_variance_plots/robust_model.png

430 KB
Loading
577 KB
Loading
404 KB
Loading

bias_variance_plots/true_data.png

101 KB
Loading

bias_variance_plots/true_model.png

423 KB
Loading

bias_variance_ridge_regression.ipynb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
{
2626
"cell_type": "code",
27-
"execution_count": null,
27+
"execution_count": 13,
2828
"id": "c86e15f6",
2929
"metadata": {},
3030
"outputs": [],
@@ -174,7 +174,7 @@
174174
},
175175
{
176176
"cell_type": "code",
177-
"execution_count": null,
177+
"execution_count": 15,
178178
"id": "6764d6da",
179179
"metadata": {},
180180
"outputs": [],
@@ -185,7 +185,7 @@
185185
},
186186
{
187187
"cell_type": "code",
188-
"execution_count": null,
188+
"execution_count": 16,
189189
"id": "d8ef032a",
190190
"metadata": {},
191191
"outputs": [],
@@ -263,7 +263,7 @@
263263
},
264264
{
265265
"cell_type": "code",
266-
"execution_count": null,
266+
"execution_count": 17,
267267
"id": "e21f304f",
268268
"metadata": {},
269269
"outputs": [],
@@ -295,7 +295,7 @@
295295
},
296296
{
297297
"cell_type": "code",
298-
"execution_count": null,
298+
"execution_count": 18,
299299
"id": "9afb4067",
300300
"metadata": {},
301301
"outputs": [],
@@ -337,7 +337,7 @@
337337
},
338338
{
339339
"cell_type": "code",
340-
"execution_count": null,
340+
"execution_count": 19,
341341
"id": "9c3ccf25",
342342
"metadata": {},
343343
"outputs": [],
@@ -371,7 +371,7 @@
371371
"outputs": [],
372372
"source": [
373373
"fig, axs = plt.subplots(1, 1, figsize=(8, 4))\n",
374-
"axs.plot(alpha2_vec, bias_squared, \"C0\", label=r\"bias$^2$\", lw=2)\n",
374+
"axs.plot(alpha2_vec, bias_squared, \"C0\", label=r\"bias$^2$\", lw=3)\n",
375375
"axs.plot(alpha2_vec, variance, \"C1\", label=r\"var\")\n",
376376
"axs.plot(alpha2_vec, bias_squared + variance, \"C2\", label=r\"bias$^2$+var\")\n",
377377
"\n",
@@ -381,12 +381,15 @@
381381
"\n",
382382
"axs.set_xscale(\"log\")\n",
383383
"axs.set_yscale(\"log\")\n",
384-
"axs.set_xlabel(r\"regularization value $\\alpha^2$\")\n",
384+
"axs.set_xlabel(r\"underfit region regularization value $\\alpha^2$ overfit region\")\n",
385385
"axs.set_title(r\"$\\alpha^2_\\mathrm{opt}$=\" + \"{:4.3f}\".format(alpha2_vec[idx]))\n",
386386
"axs.legend()\n",
387387
"axs.set_xlim(10**alpha2_min, 10**alpha2_max)\n",
388388
"axs.set_ylim(1e-2, 1e1)\n",
389-
"axs.grid(True)"
389+
"axs.grid(True)\n",
390+
"axs.xaxis.set_inverted(True)\n",
391+
"plt.tight_layout()\n",
392+
"plt.savefig('bias_variance_plots/bias_var_l2_regularisation.png', dpi=600)"
390393
]
391394
},
392395
{
@@ -668,7 +671,7 @@
668671
"kernelspec": {
669672
"display_name": "myddasp",
670673
"language": "python",
671-
"name": "myddasp"
674+
"name": "python3"
672675
},
673676
"language_info": {
674677
"codemirror_mode": {
@@ -680,7 +683,7 @@
680683
"name": "python",
681684
"nbconvert_exporter": "python",
682685
"pygments_lexer": "ipython3",
683-
"version": "3.10.6"
686+
"version": "3.12.3"
684687
}
685688
},
686689
"nbformat": 4,

0 commit comments

Comments
 (0)