|
39 | 39 | "metadata": {},
|
40 | 40 | "outputs": [],
|
41 | 41 | "source": [
|
42 |
| - "from matplotlib import pyplot as plt\n", |
| 42 | + "from matplotlib import pyplot\n", |
43 | 43 | "from autograd import grad\n",
|
44 |
| - "import autograd.numpy as np" |
| 44 | + "from autograd import numpy" |
45 | 45 | ]
|
46 | 46 | },
|
47 | 47 | {
|
|
63 | 63 | }
|
64 | 64 | ],
|
65 | 65 | "source": [
|
66 |
| - "np.random.seed(0) # fix seed for reproducibility\n", |
67 |
| - "x = np.linspace(-3, 3, 20)\n", |
68 |
| - "y = x**4 + x**3 - 4*x**2 + 8*np.random.normal(size=len(x))\n", |
69 |
| - "plt.scatter(x, y);" |
| 66 | + "numpy.random.seed(0) # fix seed for reproducibility\n", |
| 67 | + "x = numpy.linspace(-3, 3, 20)\n", |
| 68 | + "y = x**4 + x**3 - 4*x**2 + 8*numpy.random.normal(size=len(x))\n", |
| 69 | + "pyplot.scatter(x, y);" |
70 | 70 | ]
|
71 | 71 | },
|
72 | 72 | {
|
|
111 | 111 | "def polynomial_features(x, degree):\n",
|
112 | 112 | " \"\"\" Generate polynomial features for x.\"\"\"\n",
|
113 | 113 | " \n",
|
114 |
| - " X = np.empty((len(x), degree+1))\n", |
| 114 | + " X = numpy.empty((len(x), degree+1))\n", |
115 | 115 | " for i in range(degree+1):\n",
|
116 | 116 | " X[:,i] = x**i\n",
|
117 | 117 | " return X\n",
|
|
192 | 192 | " Returns:\n",
|
193 | 193 | " 1D array of predicted values\n",
|
194 | 194 | " '''\n",
|
195 |
| - " return np.dot(X, params)\n", |
| 195 | + " return numpy.dot(X, params)\n", |
196 | 196 | "\n",
|
197 | 197 | "def mse_loss(params, model, X, y):\n",
|
198 | 198 | " '''\n",
|
|
206 | 206 | " float, mean squared error\n",
|
207 | 207 | " '''\n",
|
208 | 208 | " y_pred = model(params, X)\n",
|
209 |
| - " return np.mean( np.sum((y-y_pred)**2) )\n", |
| 209 | + " return numpy.mean( numpy.sum((y-y_pred)**2) )\n", |
210 | 210 | "\n",
|
211 | 211 | "gradient = grad(mse_loss)"
|
212 | 212 | ]
|
|
265 | 265 | "source": [
|
266 | 266 | "max_iter = 3000\n",
|
267 | 267 | "alpha = 0.01\n",
|
268 |
| - "params = np.zeros(X_scaled.shape[1])\n", |
269 |
| - "descent = np.ones(X_scaled.shape[1])\n", |
| 268 | + "params = numpy.zeros(X_scaled.shape[1])\n", |
| 269 | + "descent = numpy.ones(X_scaled.shape[1])\n", |
270 | 270 | "i = 0\n",
|
271 | 271 | "\n",
|
272 | 272 | "from sklearn.metrics import mean_absolute_error\n",
|
273 | 273 | "\n",
|
274 |
| - "while np.linalg.norm(descent) > 0.01 and i < max_iter:\n", |
| 274 | + "while numpy.linalg.norm(descent) > 0.01 and i < max_iter:\n", |
275 | 275 | " descent = gradient(params, linear_regression, X_scaled, y)\n",
|
276 | 276 | " params = params - descent * alpha\n",
|
277 | 277 | " loss = mse_loss(params, linear_regression, X_scaled, y)\n",
|
|
341 | 341 | }
|
342 | 342 | ],
|
343 | 343 | "source": [
|
344 |
| - "xgrid = np.linspace(x.min(), x.max(), 30)\n", |
| 344 | + "xgrid = numpy.linspace(x.min(), x.max(), 30)\n", |
345 | 345 | "Xgrid_poly_feat = polynomial_features(xgrid, degree)\n",
|
346 | 346 | "Xgrid_scaled = min_max_scaler.transform(Xgrid_poly_feat)\n",
|
347 | 347 | "Xgrid_scaled[:,0] = 1 \n",
|
348 |
| - "plt.scatter(x, y, c='r', label='true')\n", |
349 |
| - "plt.plot(xgrid, Xgrid_scaled@params, label='predicted')\n", |
350 |
| - "plt.legend();" |
| 348 | + "pyplot.scatter(x, y, c='r', label='true')\n", |
| 349 | + "pyplot.plot(xgrid, Xgrid_scaled@params, label='predicted')\n", |
| 350 | + "pyplot.legend();" |
351 | 351 | ]
|
352 | 352 | },
|
353 | 353 | {
|
|
489 | 489 | " float, regularized mean squared error\n",
|
490 | 490 | " '''\n",
|
491 | 491 | " y_pred = model(params, X)\n",
|
492 |
| - " return np.mean( np.sum((y-y_pred)**2) ) + _lambda * np.sum( params[1:]**2 )\n", |
| 492 | + " return numpy.mean( numpy.sum((y-y_pred)**2) ) + _lambda * numpy.sum( params[1:]**2 )\n", |
493 | 493 | "\n",
|
494 | 494 | "gradient = grad(regularized_loss) "
|
495 | 495 | ]
|
|
529 | 529 | "source": [
|
530 | 530 | "max_iter = 3000\n",
|
531 | 531 | "alpha = 0.01\n",
|
532 |
| - "params = np.zeros(X_scaled.shape[1])\n", |
533 |
| - "descent = np.ones(X_scaled.shape[1])\n", |
| 532 | + "params = numpy.zeros(X_scaled.shape[1])\n", |
| 533 | + "descent = numpy.ones(X_scaled.shape[1])\n", |
534 | 534 | "i = 0\n",
|
535 | 535 | "\n",
|
536 | 536 | "from sklearn.metrics import mean_absolute_error\n",
|
537 | 537 | "\n",
|
538 |
| - "while np.linalg.norm(descent) > 0.01 and i < max_iter:\n", |
| 538 | + "while numpy.linalg.norm(descent) > 0.01 and i < max_iter:\n", |
539 | 539 | " descent = gradient(params, linear_regression, X_scaled, y)\n",
|
540 | 540 | " params = params - descent * alpha\n",
|
541 | 541 | " loss = mse_loss(params, linear_regression, X_scaled, y)\n",
|
|
586 | 586 | "print(\"weights with regularization\")\n",
|
587 | 587 | "print(params)\n",
|
588 | 588 | "\n",
|
589 |
| - "plt.scatter(x, y, c='r', label='true')\n", |
590 |
| - "plt.plot(xgrid, Xgrid_scaled@no_regularization_params, label='w/o regularization')\n", |
591 |
| - "plt.plot(xgrid, Xgrid_scaled@params, label='with regularization')\n", |
592 |
| - "plt.legend();" |
| 589 | + "pyplot.scatter(x, y, c='r', label='true')\n", |
| 590 | + "pyplot.plot(xgrid, Xgrid_scaled@no_regularization_params, label='w/o regularization')\n", |
| 591 | + "pyplot.plot(xgrid, Xgrid_scaled@params, label='with regularization')\n", |
| 592 | + "pyplot.legend();" |
593 | 593 | ]
|
594 | 594 | },
|
595 | 595 | {
|
|
676 | 676 | "print(model.coef_)\n",
|
677 | 677 | "print(model.intercept_)\n",
|
678 | 678 | "\n",
|
679 |
| - "plt.scatter(x, y, c='r', label='true')\n", |
680 |
| - "plt.plot(xgrid, y_pred_sklearn, label='sklearn ridge regression')\n", |
681 |
| - "plt.legend();\n" |
| 679 | + "pyplot.scatter(x, y, c='r', label='true')\n", |
| 680 | + "pyplot.plot(xgrid, y_pred_sklearn, label='sklearn ridge regression')\n", |
| 681 | + "pyplot.legend();\n" |
682 | 682 | ]
|
683 | 683 | },
|
684 | 684 | {
|
|
0 commit comments