{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Advanced Plotting With Partial Dependence\nThe :class:`~sklearn.inspection.PartialDependenceDisplay` object can be used\nfor plotting without needing to recalculate the partial dependence. In this\nexample, we show how to plot partial dependence plots and how to quickly\ncustomize the plot with the visualization API.\n\n

Note

See also `sphx_glr_auto_examples_miscellaneous_plot_roc_curve_visualization_api.py`

\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause\n\nimport matplotlib.pyplot as plt\nimport pandas as pd\n\nfrom sklearn.datasets import load_diabetes\nfrom sklearn.inspection import PartialDependenceDisplay\nfrom sklearn.neural_network import MLPRegressor\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.tree import DecisionTreeRegressor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Train models on the diabetes dataset\n\nFirst, we train a decision tree and a multi-layer perceptron on the diabetes\ndataset.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "diabetes = load_diabetes()\nX = pd.DataFrame(diabetes.data, columns=diabetes.feature_names)\ny = diabetes.target\n\ntree = DecisionTreeRegressor()\nmlp = make_pipeline(\n StandardScaler(),\n MLPRegressor(hidden_layer_sizes=(100, 100), tol=1e-2, max_iter=500, random_state=0),\n)\ntree.fit(X, y)\nmlp.fit(X, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting partial dependence for two features\n\nWe plot partial dependence curves for features \"age\" and \"bmi\" (body mass\nindex) for the decision tree. With two features,\n:func:`~sklearn.inspection.PartialDependenceDisplay.from_estimator` expects to plot\ntwo curves. Here the plot function place a grid of two plots using the space\ndefined by `ax` .\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(12, 6))\nax.set_title(\"Decision Tree\")\ntree_disp = PartialDependenceDisplay.from_estimator(tree, X, [\"age\", \"bmi\"], ax=ax)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The partial dependence curves can be plotted for the multi-layer perceptron.\nIn this case, `line_kw` is passed to\n:func:`~sklearn.inspection.PartialDependenceDisplay.from_estimator` to change the\ncolor of the curve.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(12, 6))\nax.set_title(\"Multi-layer Perceptron\")\nmlp_disp = PartialDependenceDisplay.from_estimator(\n mlp, X, [\"age\", \"bmi\"], ax=ax, line_kw={\"color\": \"red\"}\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting partial dependence of the two models together\n\nThe `tree_disp` and `mlp_disp`\n:class:`~sklearn.inspection.PartialDependenceDisplay` objects contain all the\ncomputed information needed to recreate the partial dependence curves. This\nmeans we can easily create additional plots without needing to recompute the\ncurves.\n\nOne way to plot the curves is to place them in the same figure, with the\ncurves of each model on each row. First, we create a figure with two axes\nwithin two rows and one column. The two axes are passed to the\n:func:`~sklearn.inspection.PartialDependenceDisplay.plot` functions of\n`tree_disp` and `mlp_disp`. The given axes will be used by the plotting\nfunction to draw the partial dependence. The resulting plot places the\ndecision tree partial dependence curves in the first row of the\nmulti-layer perceptron in the second row.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 10))\ntree_disp.plot(ax=ax1)\nax1.set_title(\"Decision Tree\")\nmlp_disp.plot(ax=ax2, line_kw={\"color\": \"red\"})\nax2.set_title(\"Multi-layer Perceptron\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another way to compare the curves is to plot them on top of each other. Here,\nwe create a figure with one row and two columns. The axes are passed into the\n:func:`~sklearn.inspection.PartialDependenceDisplay.plot` function as a list,\nwhich will plot the partial dependence curves of each model on the same axes.\nThe length of the axes list must be equal to the number of plots drawn.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6))\ntree_disp.plot(ax=[ax1, ax2], line_kw={\"label\": \"Decision Tree\"})\nmlp_disp.plot(\n ax=[ax1, ax2], line_kw={\"label\": \"Multi-layer Perceptron\", \"color\": \"red\"}\n)\nax1.legend()\nax2.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`tree_disp.axes_` is a numpy array container the axes used to draw the\npartial dependence plots. This can be passed to `mlp_disp` to have the same\naffect of drawing the plots on top of each other. Furthermore, the\n`mlp_disp.figure_` stores the figure, which allows for resizing the figure\nafter calling `plot`. In this case `tree_disp.axes_` has two dimensions, thus\n`plot` will only show the y label and y ticks on the left most plot.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "tree_disp.plot(line_kw={\"label\": \"Decision Tree\"})\nmlp_disp.plot(\n line_kw={\"label\": \"Multi-layer Perceptron\", \"color\": \"red\"}, ax=tree_disp.axes_\n)\ntree_disp.figure_.set_size_inches(10, 6)\ntree_disp.axes_[0, 0].legend()\ntree_disp.axes_[0, 1].legend()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting partial dependence for one feature\n\nHere, we plot the partial dependence curves for a single feature, \"age\", on\nthe same axes. In this case, `tree_disp.axes_` is passed into the second\nplot function.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "tree_disp = PartialDependenceDisplay.from_estimator(tree, X, [\"age\"])\nmlp_disp = PartialDependenceDisplay.from_estimator(\n mlp, X, [\"age\"], ax=tree_disp.axes_, line_kw={\"color\": \"red\"}\n)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.21" } }, "nbformat": 4, "nbformat_minor": 0 }