|
107 | 107 | "import numpy as np\n",
|
108 | 108 | "import matplotlib.pyplot as plt"
|
109 | 109 | ],
|
110 |
| - "execution_count": null, |
| 110 | + "execution_count": 18, |
111 | 111 | "outputs": []
|
112 | 112 | },
|
113 | 113 | {
|
|
215 | 215 | "Let's use the contrived values again to show how MSE is calculated. As covered in [*Calculus II*](https://github.com/jonkrohn/ML-foundations/blob/master/notebooks/4-calculus-ii.ipynb), the individual-instance variant of MSE is *squared error* or *quadratic cost*, which -- as with absolute error -- dictates that the cost for each instance $i$ must be $\\geq 0$: \n",
|
216 | 216 | "\n",
|
217 | 217 | "* $(\\hat{y}_1 - y_1)^2 = (7 - 2)^2 = 5^2 = 25$\n",
|
218 |
| - "* $(\\hat{y}_2 - y_2)^2 = (3 - 8)^2 = 5^2 = 25$\n", |
| 218 | + "* $(\\hat{y}_2 - y_2)^2 = (3 - 8)^2 = (-5)^2 = 25$\n", |
219 | 219 | "\n",
|
220 | 220 | "As suggested by its name, MSE is the average: $$C = \\frac{1}{n} \\sum_{i=1}^n (\\hat{y_i}-y_i)^2 = \\frac{1}{2} (25 + 25) = \\frac{50}{2} = 25 $$\n",
|
221 | 221 | "\n",
|
|
233 | 233 | "source": [
|
234 | 234 | "delta_y = np.linspace(-5, 5, 1000)"
|
235 | 235 | ],
|
236 |
| - "execution_count": null, |
| 236 | + "execution_count": 19, |
237 | 237 | "outputs": []
|
238 | 238 | },
|
239 | 239 | {
|
|
244 | 244 | "source": [
|
245 | 245 | "abs_error = np.abs(delta_y)"
|
246 | 246 | ],
|
247 |
| - "execution_count": null, |
| 247 | + "execution_count": 20, |
248 | 248 | "outputs": []
|
249 | 249 | },
|
250 | 250 | {
|
|
255 | 255 | "source": [
|
256 | 256 | "sq_error = delta_y**2"
|
257 | 257 | ],
|
258 |
| - "execution_count": null, |
| 258 | + "execution_count": 21, |
259 | 259 | "outputs": []
|
260 | 260 | },
|
261 | 261 | {
|
|
266 | 266 | "base_uri": "https://localhost:8080/",
|
267 | 267 | "height": 279
|
268 | 268 | },
|
269 |
| - "outputId": "09fd0964-4718-4243-8966-45f47e8028c0" |
| 269 | + "outputId": "ca63427c-ca25-4fff-9445-a77d6e172e58" |
270 | 270 | },
|
271 | 271 | "source": [
|
272 | 272 | "fig, ax = plt.subplots()\n",
|
|
282 | 282 | "ax.set_ylim(-1, 17)\n",
|
283 | 283 | "_ = ax.legend(['Absolute', 'Squared'])"
|
284 | 284 | ],
|
285 |
| - "execution_count": null, |
| 285 | + "execution_count": 22, |
286 | 286 | "outputs": [
|
287 | 287 | {
|
288 | 288 | "output_type": "display_data",
|
|
361 | 361 | "source": [
|
362 | 362 | "x = np.linspace(-10, 10, 1000)"
|
363 | 363 | ],
|
364 |
| - "execution_count": null, |
| 364 | + "execution_count": 23, |
365 | 365 | "outputs": []
|
366 | 366 | },
|
367 | 367 | {
|
|
381 | 381 | "source": [
|
382 | 382 | "y_min = x**2 + 3*x + 4"
|
383 | 383 | ],
|
384 |
| - "execution_count": null, |
| 384 | + "execution_count": 24, |
385 | 385 | "outputs": []
|
386 | 386 | },
|
387 | 387 | {
|
|
411 | 411 | "base_uri": "https://localhost:8080/",
|
412 | 412 | "height": 265
|
413 | 413 | },
|
414 |
| - "outputId": "9b8a6556-37e6-41f8-bac7-e010948a5b66" |
| 414 | + "outputId": "b5d76c63-d12c-4237-92b9-53509f1d9711" |
415 | 415 | },
|
416 | 416 | "source": [
|
417 | 417 | "fig, ax = plt.subplots()\n",
|
|
427 | 427 | "\n",
|
428 | 428 | "_ = ax.plot(x, y_min)"
|
429 | 429 | ],
|
430 |
| - "execution_count": null, |
| 430 | + "execution_count": 25, |
431 | 431 | "outputs": [
|
432 | 432 | {
|
433 | 433 | "output_type": "display_data",
|
|
470 | 470 | "source": [
|
471 | 471 | "y_max = -x**2 + 3*x + 4"
|
472 | 472 | ],
|
473 |
| - "execution_count": null, |
| 473 | + "execution_count": 26, |
474 | 474 | "outputs": []
|
475 | 475 | },
|
476 | 476 | {
|
|
500 | 500 | "base_uri": "https://localhost:8080/",
|
501 | 501 | "height": 269
|
502 | 502 | },
|
503 |
| - "outputId": "58e3dd67-6466-497f-a2b9-d1d8f4184146" |
| 503 | + "outputId": "5c77fa62-fa1d-4bfc-ac39-74eabc9781fa" |
504 | 504 | },
|
505 | 505 | "source": [
|
506 | 506 | "fig, ax = plt.subplots()\n",
|
|
516 | 516 | "\n",
|
517 | 517 | "_ = ax.plot(x, y_max)"
|
518 | 518 | ],
|
519 |
| - "execution_count": null, |
| 519 | + "execution_count": 27, |
520 | 520 | "outputs": [
|
521 | 521 | {
|
522 | 522 | "output_type": "display_data",
|
|
559 | 559 | "source": [
|
560 | 560 | "y_sp = x**3 + 6"
|
561 | 561 | ],
|
562 |
| - "execution_count": null, |
| 562 | + "execution_count": 28, |
563 | 563 | "outputs": []
|
564 | 564 | },
|
565 | 565 | {
|
|
589 | 589 | "base_uri": "https://localhost:8080/",
|
590 | 590 | "height": 269
|
591 | 591 | },
|
592 |
| - "outputId": "ace66e80-8afc-463e-c3bf-971f1e56dc33" |
| 592 | + "outputId": "7680e12d-4d8a-4f8b-a371-d285c6b2b704" |
593 | 593 | },
|
594 | 594 | "source": [
|
595 | 595 | "fig, ax = plt.subplots()\n",
|
|
605 | 605 | "\n",
|
606 | 606 | "_ = ax.plot(x, y_sp)"
|
607 | 607 | ],
|
608 |
| - "execution_count": null, |
| 608 | + "execution_count": 29, |
609 | 609 | "outputs": [
|
610 | 610 | {
|
611 | 611 | "output_type": "display_data",
|
|
0 commit comments