{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Partial Dependence Plots\n\n\nPartial dependence plots show the dependence between the target function [2]_\nand a set of 'target' features, marginalizing over the\nvalues of all other features (the complement features). Due to the limits\nof human perception the size of the target feature set must be small (usually,\none or two) thus the target features are usually chosen among the most\nimportant features\n(see :attr:`~sklearn.ensemble.GradientBoostingRegressor.feature_importances_`).\n\nThis example shows how to obtain partial dependence plots from a\n:class:`~sklearn.ensemble.GradientBoostingRegressor` trained on the California\nhousing dataset. The example is taken from [1]_.\n\nThe plot shows four one-way and one two-way partial dependence plots.\nThe target variables for the one-way PDP are:\nmedian income (`MedInc`), avg. occupants per household (`AvgOccup`),\nmedian house age (`HouseAge`), and avg. rooms per household (`AveRooms`).\n\nWe can clearly see that the median house price shows a linear relationship\nwith the median income (top left) and that the house price drops when the\navg. occupants per household increases (top middle).\nThe top right plot shows that the house age in a district does not have\na strong influence on the (median) house price; so does the average rooms\nper household.\nThe tick marks on the x-axis represent the deciles of the feature values\nin the training data.\n\nPartial dependence plots with two target features enable us to visualize\ninteractions among them. The two-way partial dependence plot shows the\ndependence of median house price on joint values of house age and avg.\noccupants per household. We can clearly see an interaction between the\ntwo features:\nFor an avg. occupancy greater than two, the house price is nearly independent\nof the house age, whereas for values less than two there is a strong dependence\non age.\n\n.. [1] T. Hastie, R. Tibshirani and J. Friedman,\n    \"Elements of Statistical Learning Ed. 2\", Springer, 2009.\n\n.. [2] For classification you can think of it as the regression score before\n       the link function.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from __future__ import print_function\nprint(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom mpl_toolkits.mplot3d import Axes3D\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import GradientBoostingRegressor\nfrom sklearn.ensemble.partial_dependence import plot_partial_dependence\nfrom sklearn.ensemble.partial_dependence import partial_dependence\nfrom sklearn.datasets.california_housing import fetch_california_housing\n\n\ndef main():\n    cal_housing = fetch_california_housing()\n\n    # split 80/20 train-test\n    X_train, X_test, y_train, y_test = train_test_split(cal_housing.data,\n                                                        cal_housing.target,\n                                                        test_size=0.2,\n                                                        random_state=1)\n    names = cal_housing.feature_names\n\n    print(\"Training GBRT...\")\n    clf = GradientBoostingRegressor(n_estimators=100, max_depth=4,\n                                    learning_rate=0.1, loss='huber',\n                                    random_state=1)\n    clf.fit(X_train, y_train)\n    print(\" done.\")\n\n    print('Convenience plot with ``partial_dependence_plots``')\n\n    features = [0, 5, 1, 2, (5, 1)]\n    fig, axs = plot_partial_dependence(clf, X_train, features,\n                                       feature_names=names,\n                                       n_jobs=3, grid_resolution=50)\n    fig.suptitle('Partial dependence of house value on nonlocation features\\n'\n                 'for the California housing dataset')\n    plt.subplots_adjust(top=0.9)  # tight_layout causes overlap with suptitle\n\n    print('Custom 3d plot via ``partial_dependence``')\n    fig = plt.figure()\n\n    target_feature = (1, 5)\n    pdp, axes = partial_dependence(clf, target_feature,\n                                   X=X_train, grid_resolution=50)\n    XX, YY = np.meshgrid(axes[0], axes[1])\n    Z = pdp[0].reshape(list(map(np.size, axes))).T\n    ax = Axes3D(fig)\n    surf = ax.plot_surface(XX, YY, Z, rstride=1, cstride=1,\n                           cmap=plt.cm.BuPu, edgecolor='k')\n    ax.set_xlabel(names[target_feature[0]])\n    ax.set_ylabel(names[target_feature[1]])\n    ax.set_zlabel('Partial dependence')\n    #  pretty init view\n    ax.view_init(elev=22, azim=122)\n    plt.colorbar(surf)\n    plt.suptitle('Partial dependence of house value on median\\n'\n                 'age and average occupancy')\n    plt.subplots_adjust(top=0.9)\n\n    plt.show()\n\n\n# Needed on Windows because plot_partial_dependence uses multiprocessing\nif __name__ == '__main__':\n    main()"
      ]
    }
  ],
  "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.6.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}