.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/tree/plot_tree_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_tree_plot_tree_regression.py: =================================================================== 决策树回归 =================================================================== 使用决策树进行一维回归。 使用 :ref:`决策树 ` 拟合带有噪声观测的正弦曲线。结果是,它学习了近似正弦曲线的局部线性回归。 我们可以看到,如果树的最大深度(由 `max_depth` 参数控制)设置得太高,决策树会学习训练数据的过细细节并从噪声中学习,即它们会过拟合。 .. GENERATED FROM PYTHON SOURCE LINES 12-46 .. image-sg:: /auto_examples/tree/images/sphx_glr_plot_tree_regression_001.png :alt: Decision Tree Regression :srcset: /auto_examples/tree/images/sphx_glr_plot_tree_regression_001.png :class: sphx-glr-single-img .. code-block:: Python # 导入必要的模块和库 import matplotlib.pyplot as plt import numpy as np from sklearn.tree import DecisionTreeRegressor # 创建一个随机数据集 rng = np.random.RandomState(1) X = np.sort(5 * rng.rand(80, 1), axis=0) y = np.sin(X).ravel() y[::5] += 3 * (0.5 - rng.rand(16)) # 拟合回归模型 regr_1 = DecisionTreeRegressor(max_depth=2) regr_2 = DecisionTreeRegressor(max_depth=5) regr_1.fit(X, y) regr_2.fit(X, y) # Predict X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis] y_1 = regr_1.predict(X_test) y_2 = regr_2.predict(X_test) # Plot the results plt.figure() plt.scatter(X, y, s=20, edgecolor="black", c="darkorange", label="data") plt.plot(X_test, y_1, color="cornflowerblue", label="max_depth=2", linewidth=2) plt.plot(X_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=2) plt.xlabel("data") plt.ylabel("target") plt.title("Decision Tree Regression") plt.legend() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.039 seconds) .. _sphx_glr_download_auto_examples_tree_plot_tree_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/tree/plot_tree_regression.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_tree_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_tree_regression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_tree_regression.zip ` .. include:: plot_tree_regression.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_