.. 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 JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_tree_plot_tree_regression.py: ======================== انحدار شجرة القرار ======================== في هذا المثال، نوضح تأثير تغيير العمق الأقصى لشجرة القرار على كيفية ملاءمتها للبيانات. نقوم بذلك مرة على مهمة انحدار 1D ومرة على مهمة انحدار متعددة المخرجات. .. GENERATED FROM PYTHON SOURCE LINES 7-11 .. code-block:: Python # المؤلفون: مطوري سكايلرن # معرف الترخيص: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 12-26 شجرة القرار على مهمة انحدار 1D ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ هنا نقوم بضبط شجرة على مهمة انحدار 1D. تستخدم :ref:`شجرة القرار ` لضبط منحنى جيب التمام مع إضافة ملاحظة عشوائية. ونتيجة لذلك، تتعلم انحدارات خطية محلية تقريب منحنى جيب التمام. يمكننا أن نرى أنه إذا تم تعيين العمق الأقصى للشجرة (الذي يتحكم فيه بمعلمة `max_depth`) مرتفعًا جدًا، فإن شجرة القرار تتعلم تفاصيل دقيقة لبيانات التدريب وتتعلم من الضوضاء، أي أنها تبالغ في التعميم. إنشاء مجموعة بيانات عشوائية 1D -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 26-33 .. code-block:: Python import numpy as np 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)) .. GENERATED FROM PYTHON SOURCE LINES 34-37 ضبط نموذج الانحدار -------------------- هنا نقوم بضبط نموذجين بعمقين أقصى مختلفين .. GENERATED FROM PYTHON SOURCE LINES 37-44 .. code-block:: Python from sklearn.tree import DecisionTreeRegressor regr_1 = DecisionTreeRegressor(max_depth=2) regr_2 = DecisionTreeRegressor(max_depth=5) regr_1.fit(X, y) regr_2.fit(X, y) .. raw:: html
DecisionTreeRegressor(max_depth=5)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 45-48 التنبؤ ------- الحصول على تنبؤات على مجموعة الاختبار .. GENERATED FROM PYTHON SOURCE LINES 48-52 .. code-block:: Python 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) .. GENERATED FROM PYTHON SOURCE LINES 53-55 رسم النتائج ---------------- .. GENERATED FROM PYTHON SOURCE LINES 55-67 .. code-block:: Python import matplotlib.pyplot as plt 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() .. 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 .. GENERATED FROM PYTHON SOURCE LINES 68-73 كما ترى، فإن النموذج بعمق 5 (أصفر) يتعلم تفاصيل بيانات التدريب إلى الحد الذي يبالغ فيه في التعميم على الضوضاء. من ناحية أخرى، النموذج بعمق 2 (أزرق) يتعلم الاتجاهات الرئيسية في البيانات جيدًا ولا يبالغ في التعميم. في حالات الاستخدام الفعلية، تحتاج إلى التأكد من أن الشجرة لا تبالغ في التعميم على بيانات التدريب، والتي يمكن القيام بها باستخدام تقسيم البيانات. .. GENERATED FROM PYTHON SOURCE LINES 75-85 انحدار شجرة القرار مع أهداف متعددة المخرجات ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ هنا تستخدم :ref:`شجرة القرار ` للتنبؤ في نفس الوقت بالملاحظات العشوائية 'x' و 'y' لدائرة مع ميزة أساسية واحدة. ونتيجة لذلك، تتعلم انحدارات خطية محلية تقريب الدائرة. يمكننا أن نرى أنه إذا تم تعيين العمق الأقصى للشجرة (الذي يتحكم فيه بمعلمة `max_depth`) مرتفعًا جدًا، فإن شجرة القرار تتعلم تفاصيل دقيقة لبيانات التدريب وتتعلم من الضوضاء، أي أنها تبالغ في التعميم. .. GENERATED FROM PYTHON SOURCE LINES 87-89 إنشاء مجموعة بيانات عشوائية ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 89-94 .. code-block:: Python rng = np.random.RandomState(1) X = np.sort(200 * rng.rand(100, 1) - 100, axis=0) y = np.array([np.pi * np.sin(X).ravel(), np.pi * np.cos(X).ravel()]).T y[::5, :] += 0.5 - rng.rand(20, 2) .. GENERATED FROM PYTHON SOURCE LINES 95-97 ضبط نموذج الانحدار -------------------- .. GENERATED FROM PYTHON SOURCE LINES 97-104 .. code-block:: Python regr_1 = DecisionTreeRegressor(max_depth=2) regr_2 = DecisionTreeRegressor(max_depth=5) regr_3 = DecisionTreeRegressor(max_depth=8) regr_1.fit(X, y) regr_2.fit(X, y) regr_3.fit(X, y) .. raw:: html
DecisionTreeRegressor(max_depth=8)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 105-108 التنبؤ ------- الحصول على تنبؤات على مجموعة الاختبار .. GENERATED FROM PYTHON SOURCE LINES 108-113 .. code-block:: Python X_test = np.arange(-100.0, 100.0, 0.01)[:, np.newaxis] y_1 = regr_1.predict(X_test) y_2 = regr_2.predict(X_test) y_3 = regr_3.predict(X_test) .. GENERATED FROM PYTHON SOURCE LINES 114-116 رسم النتائج ---------------- .. GENERATED FROM PYTHON SOURCE LINES 116-137 .. code-block:: Python plt.figure() s = 25 plt.scatter(y[:, 0], y[:, 1], c="yellow", s=s, edgecolor="black", label="data") plt.scatter( y_1[:, 0], y_1[:, 1], c="cornflowerblue", s=s, edgecolor="black", label="max_depth=2", ) plt.scatter(y_2[:, 0], y_2[:, 1], c="red", s=s, edgecolor="black", label="max_depth=5") plt.scatter(y_3[:, 0], y_3[:, 1], c="blue", s=s, edgecolor="black", label="max_depth=8") plt.xlim([-6, 6]) plt.ylim([-6, 6]) plt.xlabel("target 1") plt.ylabel("target 2") plt.title("Multi-output Decision Tree Regression") plt.legend(loc="best") plt.show() .. image-sg:: /auto_examples/tree/images/sphx_glr_plot_tree_regression_002.png :alt: Multi-output Decision Tree Regression :srcset: /auto_examples/tree/images/sphx_glr_plot_tree_regression_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 138-140 كما ترى، كلما زادت قيمة `max_depth`، كلما زادت تفاصيل البيانات التي يلتقطها النموذج. ومع ذلك، فإن النموذج يبالغ أيضًا في التعميم على البيانات ويتأثر بالضوضاء. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.431 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:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/tree/plot_tree_regression.ipynb :alt: Launch JupyterLite :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 `_