.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/miscellaneous/plot_display_object_visualization.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_miscellaneous_plot_display_object_visualization.py: =================================== التصورات باستخدام كائنات العرض =================================== .. currentmodule:: sklearn.metrics في هذا المثال، سنقوم بإنشاء كائنات عرض، :class:`ConfusionMatrixDisplay`، :class:`RocCurveDisplay`، و :class:`PrecisionRecallDisplay` مباشرة من مقاييسها الخاصة. هذا بديل لاستخدام وظائف الرسم الخاصة بها عندما تكون تنبؤات النموذج محسوبة بالفعل أو مكلفة في الحساب. لاحظ أن هذا استخدام متقدم، ونحن نوصي عمومًا باستخدام وظائف الرسم الخاصة بها. .. GENERATED FROM PYTHON SOURCE LINES 16-20 .. code-block:: Python # المؤلفون: مطوري scikit-learn # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 21-28 تحميل البيانات وتدريب النموذج ------------------------- في هذا المثال، نقوم بتحميل مجموعة بيانات مركز خدمة نقل الدم من `OpenML `_. هذه مشكلة تصنيف ثنائي حيث الهدف هو ما إذا كان الفرد قد تبرع بالدم. ثم يتم تقسيم البيانات إلى مجموعة بيانات تدريب واختبار ويتم تثبيت الانحدار اللوجستي باستخدام مجموعة بيانات التدريب. .. GENERATED FROM PYTHON SOURCE LINES 28-40 .. code-block:: Python from sklearn.datasets import fetch_openml from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler X, y = fetch_openml(data_id=1464, return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y) clf = make_pipeline(StandardScaler(), LogisticRegression(random_state=0)) clf.fit(X_train, y_train) .. raw:: html
Pipeline(steps=[('standardscaler', StandardScaler()),
                    ('logisticregression', LogisticRegression(random_state=0))])
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 41-46 إنشاء :class:`ConfusionMatrixDisplay` ###################################### باستخدام النموذج المدرب، نقوم بحساب تنبؤات النموذج على مجموعة الاختبار. يتم استخدام هذه التنبؤات لحساب مصفوفة الارتباك التي يتم رسمها باستخدام :class:`ConfusionMatrixDisplay` .. GENERATED FROM PYTHON SOURCE LINES 46-54 .. code-block:: Python from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix y_pred = clf.predict(X_test) cm = confusion_matrix(y_test, y_pred) cm_display = ConfusionMatrixDisplay(cm).plot() .. image-sg:: /auto_examples/miscellaneous/images/sphx_glr_plot_display_object_visualization_001.png :alt: plot display object visualization :srcset: /auto_examples/miscellaneous/images/sphx_glr_plot_display_object_visualization_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 55-60 إنشاء :class:`RocCurveDisplay` ############################### يتطلب منحنى ROC إما الاحتمالات أو قيم القرار غير المحددة من المقدر. نظرًا لأن الانحدار اللوجستي يوفر دالة قرار، فسنستخدمها لرسم منحنى ROC: .. GENERATED FROM PYTHON SOURCE LINES 60-67 .. code-block:: Python from sklearn.metrics import RocCurveDisplay, roc_curve y_score = clf.decision_function(X_test) fpr, tpr, _ = roc_curve(y_test, y_score, pos_label=clf.classes_[1]) roc_display = RocCurveDisplay(fpr=fpr, tpr=tpr).plot() .. image-sg:: /auto_examples/miscellaneous/images/sphx_glr_plot_display_object_visualization_002.png :alt: plot display object visualization :srcset: /auto_examples/miscellaneous/images/sphx_glr_plot_display_object_visualization_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /project/workspace/sklearn/metrics/_plot/roc_curve.py:179: UserWarning: No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument. .. GENERATED FROM PYTHON SOURCE LINES 68-72 إنشاء :class:`PrecisionRecallDisplay` ###################################### وبالمثل، يمكن رسم منحنى الدقة والاستدعاء باستخدام `y_score` من أقسام التقدير السابقة. .. GENERATED FROM PYTHON SOURCE LINES 72-77 .. code-block:: Python from sklearn.metrics import PrecisionRecallDisplay, precision_recall_curve prec, recall, _ = precision_recall_curve(y_test, y_score, pos_label=clf.classes_[1]) pr_display = PrecisionRecallDisplay(precision=prec, recall=recall).plot() .. image-sg:: /auto_examples/miscellaneous/images/sphx_glr_plot_display_object_visualization_003.png :alt: plot display object visualization :srcset: /auto_examples/miscellaneous/images/sphx_glr_plot_display_object_visualization_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 78-84 دمج كائنات العرض في رسم واحد ################################################ تقوم كائنات العرض بتخزين القيم المحسوبة التي تم تمريرها كحجج. يسمح هذا بدمج التصورات بسهولة باستخدام واجهة برمجة التطبيقات الخاصة بـ Matplotlib. في المثال التالي، نقوم بوضع العروض بجانب بعضها البعض في صف. .. GENERATED FROM PYTHON SOURCE LINES 84-91 .. code-block:: Python import matplotlib.pyplot as plt fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 8)) roc_display.plot(ax=ax1) pr_display.plot(ax=ax2) plt.show() .. image-sg:: /auto_examples/miscellaneous/images/sphx_glr_plot_display_object_visualization_004.png :alt: plot display object visualization :srcset: /auto_examples/miscellaneous/images/sphx_glr_plot_display_object_visualization_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /project/workspace/sklearn/metrics/_plot/roc_curve.py:179: UserWarning: No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.748 seconds) .. _sphx_glr_download_auto_examples_miscellaneous_plot_display_object_visualization.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/miscellaneous/plot_display_object_visualization.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/miscellaneous/plot_display_object_visualization.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_display_object_visualization.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_display_object_visualization.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_display_object_visualization.zip ` .. include:: plot_display_object_visualization.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_