|
331 | 331 | }, |
332 | 332 | { |
333 | 333 | "cell_type": "code", |
334 | | - "execution_count": null, |
| 334 | + "execution_count": 64, |
335 | 335 | "id": "7d969206", |
336 | 336 | "metadata": {}, |
337 | 337 | "outputs": [], |
|
343 | 343 | }, |
344 | 344 | { |
345 | 345 | "cell_type": "code", |
346 | | - "execution_count": null, |
| 346 | + "execution_count": 65, |
347 | 347 | "id": "f18dc6d5", |
348 | 348 | "metadata": {}, |
349 | 349 | "outputs": [], |
|
391 | 391 | }, |
392 | 392 | { |
393 | 393 | "cell_type": "code", |
394 | | - "execution_count": null, |
| 394 | + "execution_count": 66, |
395 | 395 | "id": "5e9a45d9", |
396 | 396 | "metadata": {}, |
397 | 397 | "outputs": [], |
|
418 | 418 | "source": [ |
419 | 419 | "# generate 'true' data with the design matrix of 'true' model\n", |
420 | 420 | "y = X @ beta\n", |
421 | | - "plt.figure(figsize=(5, 3))\n", |
| 421 | + "plt.figure(figsize=(6, 3))\n", |
422 | 422 | "plt.plot(y, \"k-\")\n", |
423 | 423 | "plt.xlabel(\"independent features' input variable x\")\n", |
424 | 424 | "plt.ylabel((\"dependent variable y, true data\"))\n", |
425 | 425 | "plt.title(\"true model data as linear model (x -> 4 features + intercept)\")\n", |
426 | 426 | "plt.xlim(0, M)\n", |
427 | 427 | "plt.ylim(-2, 8)\n", |
428 | 428 | "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)" |
430 | 432 | ] |
431 | 433 | }, |
432 | 434 | { |
|
583 | 585 | "X = np.copy(x)[:, None]\n", |
584 | 586 | "fig, axs = plt.subplots(2, 2, figsize=(10, 5))\n", |
585 | 587 | "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)" |
587 | 591 | ] |
588 | 592 | }, |
589 | 593 | { |
|
618 | 622 | "# note that intercept is only added in function bias_variance_of_model(X)\n", |
619 | 623 | "fig, axs = plt.subplots(2, 2, figsize=(10, 5))\n", |
620 | 624 | "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)" |
622 | 628 | ] |
623 | 629 | }, |
624 | 630 | { |
|
652 | 658 | "bias_variance_of_model(X) # lowest possible bias^2+variance, because we\n", |
653 | 659 | "# know the true model (again: which in practice likely never will occur)\n", |
654 | 660 | "# 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)" |
656 | 664 | ] |
657 | 665 | }, |
658 | 666 | { |
|
684 | 692 | "# note that intercept is only added in function bias_variance_of_model(X)\n", |
685 | 693 | "fig, axs = plt.subplots(2, 2, figsize=(10, 5))\n", |
686 | 694 | "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)" |
688 | 698 | ] |
689 | 699 | }, |
690 | 700 | { |
|
721 | 731 | "kernelspec": { |
722 | 732 | "display_name": "myddasp", |
723 | 733 | "language": "python", |
724 | | - "name": "myddasp" |
| 734 | + "name": "python3" |
725 | 735 | }, |
726 | 736 | "language_info": { |
727 | 737 | "codemirror_mode": { |
|
733 | 743 | "name": "python", |
734 | 744 | "nbconvert_exporter": "python", |
735 | 745 | "pygments_lexer": "ipython3", |
736 | | - "version": "3.10.6" |
| 746 | + "version": "3.12.3" |
737 | 747 | } |
738 | 748 | }, |
739 | 749 | "nbformat": 4, |
|
0 commit comments