.. 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 ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_ensemble_plot_adaboost_regression.py: ====================================== AdaBoost 决策树回归 ====================================== 在带有少量高斯噪声的一维正弦数据集上,使用 AdaBoost.R2 [1]_ 算法对决策树进行提升。 将 299 次提升(300 棵决策树)与单个决策树回归器进行比较。随着提升次数的增加,回归器可以拟合更多细节。 请参阅 :ref:`sphx_glr_auto_examples_ensemble_plot_hgbt_regression.py` 以了解使用更高效回归模型(如 :class:`~ensemble.HistGradientBoostingRegressor` )的好处。 .. [1] `H. Drucker, "Improving Regressors using Boosting Techniques", 1997. `_ .. GENERATED FROM PYTHON SOURCE LINES 17-20 准备数据 -------- 首先,我们准备具有正弦关系和一些高斯噪声的虚拟数据。 .. GENERATED FROM PYTHON SOURCE LINES 20-31 .. code-block:: Python # 作者:scikit-learn 开发者 # SPDX许可证标识符: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 32-39 训练和预测使用决策树和AdaBoost回归器 ---------------------------------------- 现在,我们定义分类器并将它们拟合到数据上。 然后我们在相同的数据上进行预测,以查看它们的拟合效果。 第一个回归器是一个 `max_depth=4` 的 `DecisionTreeRegressor` 。 第二个回归器是一个以 `max_depth=4` 的 `DecisionTreeRegressor` 为基础学习器的 `AdaBoostRegressor` , 并将使用 `n_estimators=300` 个这样的基础学习器进行构建。 .. GENERATED FROM PYTHON SOURCE LINES 39-56 .. code-block:: Python 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 57-60 绘制结果 -------- 最后,我们绘制了两个回归器(单一决策树回归器和AdaBoost回归器)拟合数据的效果。 .. GENERATED FROM PYTHON SOURCE LINES 60-75 .. code-block:: Python 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.197 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/main?urlpath=lab/tree/notebooks/auto_examples/ensemble/plot_adaboost_regression.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_adaboost_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_adaboost_regression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_adaboost_regression.zip ` .. include:: plot_adaboost_regression.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_