.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/ensemble/plot_adaboost_regression.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_ensemble_plot_adaboost_regression.py>`
        to download the full example code or to run this example in your browser via JupyterLite or Binder

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_ensemble_plot_adaboost_regression.py:


======================================
Decision Tree Regression with AdaBoost
======================================

A decision tree is boosted using the AdaBoost.R2 [1]_ algorithm on a 1D
sinusoidal dataset with a small amount of Gaussian noise.
299 boosts (300 decision trees) is compared with a single decision tree
regressor. As the number of boosts is increased the regressor can fit more
detail.

.. [1] `H. Drucker, "Improving Regressors using Boosting Techniques", 1997.
        <https://citeseerx.ist.psu.edu/doc_view/pid/8d49e2dedb817f2c3330e74b63c5fc86d2399ce3>`_

.. GENERATED FROM PYTHON SOURCE LINES 18-21

Preparing the data
------------------
First, we prepare dummy data with a sinusoidal relationship and some gaussian noise.

.. GENERATED FROM PYTHON SOURCE LINES 21-32

.. code-block:: default


    # Author: Noel Dawe <noel.dawe@gmail.com>
    #
    # License: BSD 3 clause

    import numpy as np

    rng = np.random.RandomState(1)
    X = np.linspace(0, 6, 100)[:, np.newaxis]
    y = np.sin(X).ravel() + np.sin(6 * X).ravel() + rng.normal(0, 0.1, X.shape[0])








.. GENERATED FROM PYTHON SOURCE LINES 33-41

Training and prediction with DecisionTree and AdaBoost Regressors
-----------------------------------------------------------------
Now, we define the classifiers and fit them to the data.
Then we predict on that same data to see how well they could fit it.
The first regressor is a `DecisionTreeRegressor` with `max_depth=4`.
The second regressor is an `AdaBoostRegressor` with a `DecisionTreeRegressor`
of `max_depth=4` as base learner and will be built with `n_estimators=300`
of those base learners.

.. GENERATED FROM PYTHON SOURCE LINES 41-57

.. code-block:: default


    from sklearn.ensemble import AdaBoostRegressor
    from sklearn.tree import DecisionTreeRegressor

    regr_1 = DecisionTreeRegressor(max_depth=4)

    regr_2 = AdaBoostRegressor(
        DecisionTreeRegressor(max_depth=4), n_estimators=300, random_state=rng
    )

    regr_1.fit(X, y)
    regr_2.fit(X, y)

    y_1 = regr_1.predict(X)
    y_2 = regr_2.predict(X)








.. GENERATED FROM PYTHON SOURCE LINES 58-62

Plotting the results
--------------------
Finally, we plot how well our two regressors,
single decision tree regressor and AdaBoost regressor, could fit the data.

.. GENERATED FROM PYTHON SOURCE LINES 62-77

.. code-block:: default


    import matplotlib.pyplot as plt
    import seaborn as sns

    colors = sns.color_palette("colorblind")

    plt.figure()
    plt.scatter(X, y, color=colors[0], label="training samples")
    plt.plot(X, y_1, color=colors[1], label="n_estimators=1", linewidth=2)
    plt.plot(X, y_2, color=colors[2], label="n_estimators=300", linewidth=2)
    plt.xlabel("data")
    plt.ylabel("target")
    plt.title("Boosted Decision Tree Regression")
    plt.legend()
    plt.show()



.. image-sg:: /auto_examples/ensemble/images/sphx_glr_plot_adaboost_regression_001.png
   :alt: Boosted Decision Tree Regression
   :srcset: /auto_examples/ensemble/images/sphx_glr_plot_adaboost_regression_001.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.436 seconds)


.. _sphx_glr_download_auto_examples_ensemble_plot_adaboost_regression.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example


    .. container:: binder-badge

      .. image:: images/binder_badge_logo.svg
        :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/1.3.X?urlpath=lab/tree/notebooks/auto_examples/ensemble/plot_adaboost_regression.ipynb
        :alt: Launch binder
        :width: 150 px



    .. container:: lite-badge

      .. image:: images/jupyterlite_badge_logo.svg
        :target: ../../lite/lab/?path=auto_examples/ensemble/plot_adaboost_regression.ipynb
        :alt: Launch JupyterLite
        :width: 150 px

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_adaboost_regression.py <plot_adaboost_regression.py>`

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_adaboost_regression.ipynb <plot_adaboost_regression.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_