|
69 | 69 | },
|
70 | 70 | "outputs": [],
|
71 | 71 | "source": [
|
72 |
| - "import matplotlib.pyplot as plt\nfrom sklearn.inspection import partial_dependence\nfrom sklearn.inspection import PartialDependenceDisplay\n\nprint(\"Computing partial dependence plots...\")\ntic = time()\nfeatures = [\"MedInc\", \"AveOccup\", \"HouseAge\", \"AveRooms\"]\ndisplay = PartialDependenceDisplay.from_estimator(\n est,\n X_train,\n features,\n kind=\"both\",\n subsample=50,\n n_jobs=3,\n grid_resolution=20,\n random_state=0,\n ice_lines_kw={\"color\": \"tab:blue\", \"alpha\": 0.2, \"linewidth\": 0.5},\n pd_line_kw={\"color\": \"tab:orange\", \"linestyle\": \"--\"},\n)\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n \"Partial dependence of house value on non-location features\\n\"\n \"for the California housing dataset, with MLPRegressor\"\n)\ndisplay.figure_.subplots_adjust(hspace=0.3)" |
| 72 | + "from sklearn.inspection import PartialDependenceDisplay\n\ncommon_params = {\"subsample\": 50, \"n_jobs\": 2, \"grid_resolution\": 20, \"random_state\": 0}\n\nprint(\"Computing partial dependence plots...\")\ntic = time()\ndisplay = PartialDependenceDisplay.from_estimator(\n est,\n X_train,\n features=[\"MedInc\", \"AveOccup\", \"HouseAge\", \"AveRooms\"],\n kind=\"both\",\n **common_params,\n)\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n \"Partial dependence of house value on non-location features\\n\"\n \"for the California housing dataset, with MLPRegressor\"\n)\ndisplay.figure_.subplots_adjust(hspace=0.3)" |
73 | 73 | ]
|
74 | 74 | },
|
75 | 75 | {
|
|
105 | 105 | },
|
106 | 106 | "outputs": [],
|
107 | 107 | "source": [
|
108 |
| - "print(\"Computing partial dependence plots...\")\ntic = time()\ndisplay = PartialDependenceDisplay.from_estimator(\n est,\n X_train,\n features,\n kind=\"both\",\n subsample=50,\n n_jobs=3,\n grid_resolution=20,\n random_state=0,\n ice_lines_kw={\"color\": \"tab:blue\", \"alpha\": 0.2, \"linewidth\": 0.5},\n pd_line_kw={\"color\": \"tab:orange\", \"linestyle\": \"--\"},\n)\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n \"Partial dependence of house value on non-location features\\n\"\n \"for the California housing dataset, with Gradient Boosting\"\n)\ndisplay.figure_.subplots_adjust(wspace=0.4, hspace=0.3)" |
| 108 | + "print(\"Computing partial dependence plots...\")\ntic = time()\ndisplay = PartialDependenceDisplay.from_estimator(\n est,\n X_train,\n features=[\"MedInc\", \"AveOccup\", \"HouseAge\", \"AveRooms\"],\n kind=\"both\",\n **common_params,\n)\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n \"Partial dependence of house value on non-location features\\n\"\n \"for the California housing dataset, with Gradient Boosting\"\n)\ndisplay.figure_.subplots_adjust(wspace=0.4, hspace=0.3)" |
109 | 109 | ]
|
110 | 110 | },
|
111 | 111 | {
|
|
130 | 130 | },
|
131 | 131 | "outputs": [],
|
132 | 132 | "source": [
|
133 |
| - "features = [\"AveOccup\", \"HouseAge\", (\"AveOccup\", \"HouseAge\")]\nprint(\"Computing partial dependence plots...\")\ntic = time()\n_, ax = plt.subplots(ncols=3, figsize=(9, 4))\ndisplay = PartialDependenceDisplay.from_estimator(\n est,\n X_train,\n features,\n kind=\"average\",\n n_jobs=2,\n grid_resolution=10,\n ax=ax,\n)\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n \"Partial dependence of house value on non-location features\\n\"\n \"for the California housing dataset, with Gradient Boosting\"\n)\ndisplay.figure_.subplots_adjust(wspace=0.4, hspace=0.3)" |
| 133 | + "import matplotlib.pyplot as plt\n\nprint(\"Computing partial dependence plots...\")\ntic = time()\n_, ax = plt.subplots(ncols=3, figsize=(9, 4))\n\n# Note that we could have called the method `from_estimator` three times and\n# provide one feature, one kind of plot, and one axis for each call.\ndisplay = PartialDependenceDisplay.from_estimator(\n est,\n X_train,\n features=[\"AveOccup\", \"HouseAge\", (\"AveOccup\", \"HouseAge\")],\n kind=[\"both\", \"both\", \"average\"],\n ax=ax,\n **common_params,\n)\n\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n \"Partial dependence of house value on non-location features\\n\"\n \"for the California housing dataset, with Gradient Boosting\"\n)\ndisplay.figure_.subplots_adjust(wspace=0.4, hspace=0.3)" |
134 | 134 | ]
|
135 | 135 | },
|
136 | 136 | {
|
|
148 | 148 | },
|
149 | 149 | "outputs": [],
|
150 | 150 | "source": [
|
151 |
| - "import numpy as np\nfrom mpl_toolkits.mplot3d import Axes3D\n\nfig = plt.figure()\n\nfeatures = (\"AveOccup\", \"HouseAge\")\npdp = partial_dependence(\n est, X_train, features=features, kind=\"average\", grid_resolution=10\n)\nXX, YY = np.meshgrid(pdp[\"values\"][0], pdp[\"values\"][1])\nZ = pdp.average[0].T\nax = Axes3D(fig)\nfig.add_axes(ax)\n\nsurf = ax.plot_surface(XX, YY, Z, rstride=1, cstride=1, cmap=plt.cm.BuPu, edgecolor=\"k\")\nax.set_xlabel(features[0])\nax.set_ylabel(features[1])\nax.set_zlabel(\"Partial dependence\")\n# pretty init view\nax.view_init(elev=22, azim=122)\nplt.colorbar(surf)\nplt.suptitle(\n \"Partial dependence of house value on median\\n\"\n \"age and average occupancy, with Gradient Boosting\"\n)\nplt.subplots_adjust(top=0.9)\nplt.show()" |
| 151 | + "import numpy as np\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom sklearn.inspection import partial_dependence\n\nfig = plt.figure()\n\nfeatures = (\"AveOccup\", \"HouseAge\")\npdp = partial_dependence(\n est, X_train, features=features, kind=\"average\", grid_resolution=10\n)\nXX, YY = np.meshgrid(pdp[\"values\"][0], pdp[\"values\"][1])\nZ = pdp.average[0].T\nax = Axes3D(fig)\nfig.add_axes(ax)\n\nsurf = ax.plot_surface(XX, YY, Z, rstride=1, cstride=1, cmap=plt.cm.BuPu, edgecolor=\"k\")\nax.set_xlabel(features[0])\nax.set_ylabel(features[1])\nax.set_zlabel(\"Partial dependence\")\n# pretty init view\nax.view_init(elev=22, azim=122)\nplt.colorbar(surf)\nplt.suptitle(\n \"Partial dependence of house value on median\\n\"\n \"age and average occupancy, with Gradient Boosting\"\n)\nplt.subplots_adjust(top=0.9)\nplt.show()" |
152 | 152 | ]
|
153 | 153 | }
|
154 | 154 | ],
|
|
0 commit comments