|
29 | 29 | "import numpy as np\n",
|
30 | 30 | "\n",
|
31 | 31 | "# Plotting library\n",
|
32 |
| - "from matplotlib import pyplot\n", |
| 32 | + "from matplotlib import pyplot as plt\n", |
33 | 33 | "\n",
|
34 | 34 | "# Optimization module in scipy\n",
|
35 | 35 | "from scipy import optimize\n",
|
|
114 | 114 | "m = y.size\n",
|
115 | 115 | "\n",
|
116 | 116 | "# Plot training data\n",
|
117 |
| - "pyplot.plot(X, y, 'ro', ms=10, mec='k', mew=1)\n", |
118 |
| - "pyplot.xlabel('Change in water level (x)')\n", |
119 |
| - "pyplot.ylabel('Water flowing out of the dam (y)');" |
| 117 | + "plt.plot(X, y, 'ro', ms=10, mec='k', mew=1)\n", |
| 118 | + "plt.xlabel('Change in water level (x)')\n", |
| 119 | + "plt.ylabel('Water flowing out of the dam (y)');" |
120 | 120 | ]
|
121 | 121 | },
|
122 | 122 | {
|
|
317 | 317 | "theta = utils.trainLinearReg(linearRegCostFunction, X_aug, y, lambda_=0)\n",
|
318 | 318 | "\n",
|
319 | 319 | "# Plot fit over the data\n",
|
320 |
| - "pyplot.plot(X, y, 'ro', ms=10, mec='k', mew=1.5)\n", |
321 |
| - "pyplot.xlabel('Change in water level (x)')\n", |
322 |
| - "pyplot.ylabel('Water flowing out of the dam (y)')\n", |
323 |
| - "pyplot.plot(X, np.dot(X_aug, theta), '--', lw=2);" |
| 320 | + "plt.plot(X, y, 'ro', ms=10, mec='k', mew=1.5)\n", |
| 321 | + "plt.xlabel('Change in water level (x)')\n", |
| 322 | + "plt.ylabel('Water flowing out of the dam (y)')\n", |
| 323 | + "plt.plot(X, np.dot(X_aug, theta), '--', lw=2);" |
324 | 324 | ]
|
325 | 325 | },
|
326 | 326 | {
|
|
464 | 464 | "Xval_aug = np.concatenate([np.ones((yval.size, 1)), Xval], axis=1)\n",
|
465 | 465 | "error_train, error_val = learningCurve(X_aug, y, Xval_aug, yval, lambda_=0)\n",
|
466 | 466 | "\n",
|
467 |
| - "pyplot.plot(np.arange(1, m+1), error_train, np.arange(1, m+1), error_val, lw=2)\n", |
468 |
| - "pyplot.title('Learning curve for linear regression')\n", |
469 |
| - "pyplot.legend(['Train', 'Cross Validation'])\n", |
470 |
| - "pyplot.xlabel('Number of training examples')\n", |
471 |
| - "pyplot.ylabel('Error')\n", |
472 |
| - "pyplot.axis([0, 13, 0, 150])\n", |
| 467 | + "plt.plot(np.arange(1, m+1), error_train, np.arange(1, m+1), error_val, lw=2)\n", |
| 468 | + "plt.title('Learning curve for linear regression')\n", |
| 469 | + "plt.legend(['Train', 'Cross Validation'])\n", |
| 470 | + "plt.xlabel('Number of training examples')\n", |
| 471 | + "plt.ylabel('Error')\n", |
| 472 | + "plt.axis([0, 13, 0, 150])\n", |
473 | 473 | "\n",
|
474 | 474 | "print('# Training Examples\\tTrain Error\\tCross Validation Error')\n",
|
475 | 475 | "for i in range(m):\n",
|
|
654 | 654 | " lambda_=lambda_, maxiter=55)\n",
|
655 | 655 | "\n",
|
656 | 656 | "# Plot training data and fit\n",
|
657 |
| - "pyplot.plot(X, y, 'ro', ms=10, mew=1.5, mec='k')\n", |
| 657 | + "plt.plot(X, y, 'ro', ms=10, mew=1.5, mec='k')\n", |
658 | 658 | "\n",
|
659 | 659 | "utils.plotFit(polyFeatures, np.min(X), np.max(X), mu, sigma, theta, p)\n",
|
660 | 660 | "\n",
|
661 |
| - "pyplot.xlabel('Change in water level (x)')\n", |
662 |
| - "pyplot.ylabel('Water flowing out of the dam (y)')\n", |
663 |
| - "pyplot.title('Polynomial Regression Fit (lambda = %f)' % lambda_)\n", |
664 |
| - "pyplot.ylim([-20, 50])\n", |
| 661 | + "plt.xlabel('Change in water level (x)')\n", |
| 662 | + "plt.ylabel('Water flowing out of the dam (y)')\n", |
| 663 | + "plt.title('Polynomial Regression Fit (lambda = %f)' % lambda_)\n", |
| 664 | + "plt.ylim([-20, 50])\n", |
665 | 665 | "\n",
|
666 |
| - "pyplot.figure()\n", |
| 666 | + "plt.figure()\n", |
667 | 667 | "error_train, error_val = learningCurve(X_poly, y, X_poly_val, yval, lambda_)\n",
|
668 |
| - "pyplot.plot(np.arange(1, 1+m), error_train, np.arange(1, 1+m), error_val)\n", |
| 668 | + "plt.plot(np.arange(1, 1+m), error_train, np.arange(1, 1+m), error_val)\n", |
669 | 669 | "\n",
|
670 |
| - "pyplot.title('Polynomial Regression Learning Curve (lambda = %f)' % lambda_)\n", |
671 |
| - "pyplot.xlabel('Number of training examples')\n", |
672 |
| - "pyplot.ylabel('Error')\n", |
673 |
| - "pyplot.axis([0, 13, 0, 100])\n", |
674 |
| - "pyplot.legend(['Train', 'Cross Validation'])\n", |
| 670 | + "plt.title('Polynomial Regression Learning Curve (lambda = %f)' % lambda_)\n", |
| 671 | + "plt.xlabel('Number of training examples')\n", |
| 672 | + "plt.ylabel('Error')\n", |
| 673 | + "plt.axis([0, 13, 0, 100])\n", |
| 674 | + "plt.legend(['Train', 'Cross Validation'])\n", |
675 | 675 | "\n",
|
676 | 676 | "print('Polynomial Regression (lambda = %f)\\n' % lambda_)\n",
|
677 | 677 | "print('# Training Examples\\tTrain Error\\tCross Validation Error')\n",
|
|
821 | 821 | "source": [
|
822 | 822 | "lambda_vec, error_train, error_val = validationCurve(X_poly, y, X_poly_val, yval)\n",
|
823 | 823 | "\n",
|
824 |
| - "pyplot.plot(lambda_vec, error_train, '-o', lambda_vec, error_val, '-o', lw=2)\n", |
825 |
| - "pyplot.legend(['Train', 'Cross Validation'])\n", |
826 |
| - "pyplot.xlabel('lambda')\n", |
827 |
| - "pyplot.ylabel('Error')\n", |
| 824 | + "plt.plot(lambda_vec, error_train, '-o', lambda_vec, error_val, '-o', lw=2)\n", |
| 825 | + "plt.legend(['Train', 'Cross Validation'])\n", |
| 826 | + "plt.xlabel('lambda')\n", |
| 827 | + "plt.ylabel('Error')\n", |
828 | 828 | "\n",
|
829 | 829 | "print('lambda\\t\\tTrain Error\\tValidation Error')\n",
|
830 | 830 | "for i in range(len(lambda_vec)):\n",
|
|
907 | 907 | "name": "python",
|
908 | 908 | "nbconvert_exporter": "python",
|
909 | 909 | "pygments_lexer": "ipython3",
|
910 |
| - "version": "3.6.4" |
| 910 | + "version": "3.9.1" |
911 | 911 | }
|
912 | 912 | },
|
913 | 913 | "nbformat": 4,
|
|
0 commit comments