{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Comparing random forests and the multi-output meta estimator\n\n\nAn example to compare multi-output regression with random forest and\nthe `multioutput.MultiOutputRegressor <multiclass>` meta-estimator.\n\nThis example illustrates the use of the\n`multioutput.MultiOutputRegressor <multiclass>` meta-estimator\nto perform multi-output regression. A random forest regressor is used,\nwhich supports multi-output regression natively, so the results can be\ncompared.\n\nThe random forest regressor will only ever predict values within the\nrange of observations or closer to zero for each of the targets. As a\nresult the predictions are biased towards the centre of the circle.\n\nUsing a single underlying feature the model learns both the\nx and y coordinate as output.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\n# Author: Tim Head <betatim@gmail.com>\n#\n# License: BSD 3 clause\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.multioutput import MultiOutputRegressor\n\n\n# Create a random dataset\nrng = np.random.RandomState(1)\nX = np.sort(200 * rng.rand(600, 1) - 100, axis=0)\ny = np.array([np.pi * np.sin(X).ravel(), np.pi * np.cos(X).ravel()]).T\ny += (0.5 - rng.rand(*y.shape))\n\nX_train, X_test, y_train, y_test = train_test_split(\n    X, y, train_size=400, test_size=200, random_state=4)\n\nmax_depth = 30\nregr_multirf = MultiOutputRegressor(RandomForestRegressor(n_estimators=100,\n                                                          max_depth=max_depth,\n                                                          random_state=0))\nregr_multirf.fit(X_train, y_train)\n\nregr_rf = RandomForestRegressor(n_estimators=100, max_depth=max_depth,\n                                random_state=2)\nregr_rf.fit(X_train, y_train)\n\n# Predict on new data\ny_multirf = regr_multirf.predict(X_test)\ny_rf = regr_rf.predict(X_test)\n\n# Plot the results\nplt.figure()\ns = 50\na = 0.4\nplt.scatter(y_test[:, 0], y_test[:, 1], edgecolor='k',\n            c=\"navy\", s=s, marker=\"s\", alpha=a, label=\"Data\")\nplt.scatter(y_multirf[:, 0], y_multirf[:, 1], edgecolor='k',\n            c=\"cornflowerblue\", s=s, alpha=a,\n            label=\"Multi RF score=%.2f\" % regr_multirf.score(X_test, y_test))\nplt.scatter(y_rf[:, 0], y_rf[:, 1], edgecolor='k',\n            c=\"c\", s=s, marker=\"^\", alpha=a,\n            label=\"RF score=%.2f\" % regr_rf.score(X_test, y_test))\nplt.xlim([-6, 6])\nplt.ylim([-6, 6])\nplt.xlabel(\"target 1\")\nplt.ylabel(\"target 2\")\nplt.title(\"Comparing random forests and the multi-output meta estimator\")\nplt.legend()\nplt.show()"
      ]
    }
  ],
  "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
}