.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/ensemble/plot_monotonic_constraints.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_ensemble_plot_monotonic_constraints.py: ===================== القيود الرتيبة ===================== يوضح هذا المثال تأثير القيود الرتيبة على مقدر التعزيز المتدرج. نقوم ببناء مجموعة بيانات اصطناعية حيث تكون قيمة الهدف بشكل عام مرتبطة بشكل إيجابي بالميزة الأولى (مع بعض الاختلافات العشوائية وغير العشوائية)، ومرتبطة بشكل سلبي بالميزة الثانية بشكل عام. من خلال فرض قيد زيادة رتيبة أو قيد نقصان رتيب، على التوالي، على الميزات أثناء عملية التعلم، يكون المقدر قادرًا على اتباع الاتجاه العام بشكل صحيح بدلاً من أن يخضع للاختلافات. استوحى هذا المثال من `وثائق XGBoost `_. .. GENERATED FROM PYTHON SOURCE LINES 20-24 .. code-block:: Python # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 25-43 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.ensemble import HistGradientBoostingRegressor from sklearn.inspection import PartialDependenceDisplay rng = np.random.RandomState(0) n_samples = 1000 f_0 = rng.rand(n_samples) f_1 = rng.rand(n_samples) X = np.c_[f_0, f_1] noise = rng.normal(loc=0.0, scale=0.01, size=n_samples) # y مرتبط بشكل إيجابي بـ f_0، ومرتبط بشكل سلبي بـ f_1 y = 5 * f_0 + np.sin(10 * np.pi * f_0) - 5 * f_1 - np.cos(10 * np.pi * f_1) + noise .. GENERATED FROM PYTHON SOURCE LINES 44-45 ملاءمة نموذج أول على مجموعة البيانات هذه بدون أي قيود. .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python gbdt_no_cst = HistGradientBoostingRegressor() gbdt_no_cst.fit(X, y) .. raw:: html
HistGradientBoostingRegressor()
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 49-51 ملاءمة نموذج ثانٍ على مجموعة البيانات هذه مع قيود زيادة رتيبة (1) وقيد نقصان رتيب (-1)، على التوالي. .. GENERATED FROM PYTHON SOURCE LINES 51-55 .. code-block:: Python gbdt_with_monotonic_cst = HistGradientBoostingRegressor(monotonic_cst=[1, -1]) gbdt_with_monotonic_cst.fit(X, y) .. raw:: html
HistGradientBoostingRegressor(monotonic_cst=[1, -1])
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 56-57 دعونا نعرض الاعتماد الجزئي للتنبؤات على الميزتين. .. GENERATED FROM PYTHON SOURCE LINES 57-87 .. code-block:: Python fig, ax = plt.subplots() disp = PartialDependenceDisplay.from_estimator( gbdt_no_cst, X, features=[0, 1], feature_names=( "الميزة الأولى", "الميزة الثانية", ), line_kw={"linewidth": 4, "label": "بدون قيود", "color": "tab:blue"}, ax=ax, ) PartialDependenceDisplay.from_estimator( gbdt_with_monotonic_cst, X, features=[0, 1], line_kw={"linewidth": 4, "label": "مقيد", "color": "tab:orange"}, ax=disp.axes_, ) for f_idx in (0, 1): disp.axes_[0, f_idx].plot( X[:, f_idx], y, "o", alpha=0.3, zorder=-1, color="tab:green" ) disp.axes_[0, f_idx].set_ylim(-6, 6) plt.legend() fig.suptitle("تأثير القيود الرتيبة على التبعيات الجزئية") plt.show() .. image-sg:: /auto_examples/ensemble/images/sphx_glr_plot_monotonic_constraints_001.png :alt: تأثير القيود الرتيبة على التبعيات الجزئية :srcset: /auto_examples/ensemble/images/sphx_glr_plot_monotonic_constraints_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-91 يمكننا أن نرى أن تنبؤات النموذج غير المقيد تلتقط تذبذبات البيانات بينما يتبع النموذج المقيد الاتجاه العام ويتجاهل الاختلافات المحلية. .. GENERATED FROM PYTHON SOURCE LINES 93-100 .. _monotonic_cst_features_names: استخدام أسماء الميزات لتحديد القيود الرتيبة ---------------------------------------------------- لاحظ أنه إذا كانت بيانات التدريب تحتوي على أسماء ميزات، فمن الممكن تحديد القيود الرتيبة عن طريق تمرير قاموس: .. GENERATED FROM PYTHON SOURCE LINES 100-113 .. code-block:: Python import pandas as pd X_df = pd.DataFrame(X, columns=["f_0", "f_1"]) gbdt_with_monotonic_cst_df = HistGradientBoostingRegressor( monotonic_cst={"f_0": 1, "f_1": -1} ).fit(X_df, y) np.allclose( gbdt_with_monotonic_cst_df.predict(X_df), gbdt_with_monotonic_cst.predict(X) ) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.832 seconds) .. _sphx_glr_download_auto_examples_ensemble_plot_monotonic_constraints.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_monotonic_constraints.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/ensemble/plot_monotonic_constraints.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_monotonic_constraints.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_monotonic_constraints.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_monotonic_constraints.zip ` .. include:: plot_monotonic_constraints.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_