.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/semi_supervised/plot_label_propagation_digits.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_semi_supervised_plot_label_propagation_digits.py: ========================================= نشر العلامات على الأرقام: توضيح الأداء ========================================= يوضح هذا المثال قوة التعلم شبه المُشرف من خلال تدريب نموذج نشر العلامات لتصنيف الأرقام المكتوبة بخط اليد باستخدام مجموعات ذات عدد قليل جدًا من العلامات. يحتوي مجموعة البيانات للأرقام المكتوبة بخط اليد على 1797 نقطة إجمالية. سيتم تدريب النموذج باستخدام جميع النقاط، ولكن سيتم وضع علامات على 30 نقطة فقط. ستكون النتائج على شكل مصفوفة ارتباك وسلسلة من المقاييس عبر كل فئة ستكون جيدة جدًا. في النهاية، سيتم عرض أفضل 10 تنبؤات غير مؤكدة. .. GENERATED FROM PYTHON SOURCE LINES 19-23 .. code-block:: Python # المؤلفون: مطوري سكايلرن # معرف الترخيص: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 24-28 توليد البيانات --------------- نستخدم مجموعة بيانات الأرقام. نستخدم فقط مجموعة فرعية من العينات المحددة عشوائيًا. .. GENERATED FROM PYTHON SOURCE LINES 28-37 .. code-block:: Python import numpy as np from sklearn import datasets digits = datasets.load_digits() rng = np.random.RandomState(2) indices = np.arange(len(digits.data)) rng.shuffle(indices) .. GENERATED FROM PYTHON SOURCE LINES 38-41 قمنا باختيار 340 عينة، منها 40 فقط سترتبط بعلامة معروفة. لذلك، نقوم بتخزين مؤشرات 300 عينة أخرى والتي من المفترض ألا نعرف علاماتها. .. GENERATED FROM PYTHON SOURCE LINES 42-53 .. code-block:: Python X = digits.data[indices[:340]] y = digits.target[indices[:340]] images = digits.images[indices[:340]] n_total_samples = len(y) n_labeled_points = 40 indices = np.arange(n_total_samples) unlabeled_set = indices[n_labeled_points:] .. GENERATED FROM PYTHON SOURCE LINES 54-55 خلط كل شيء .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: Python y_train = np.copy(y) y_train[unlabeled_set] = -1 .. GENERATED FROM PYTHON SOURCE LINES 59-64 التعلم شبه المُشرف ------------------------ نقوم بضبط :class:`~sklearn.semi_supervised.LabelSpreading` واستخدامه للتنبؤ بالعلامات غير المعروفة. .. GENERATED FROM PYTHON SOURCE LINES 64-77 .. code-block:: Python from sklearn.metrics import classification_report from sklearn.semi_supervised import LabelSpreading lp_model = LabelSpreading(gamma=0.25, max_iter=20) lp_model.fit(X, y_train) predicted_labels = lp_model.transduction_[unlabeled_set] true_labels = y[unlabeled_set] print( "نموذج نشر العلامات: %d مع علامات و %d بدون علامات (%d إجمالي)" % (n_labeled_points, n_total_samples - n_labeled_points, n_total_samples) ) .. rst-class:: sphx-glr-script-out .. code-block:: none نموذج نشر العلامات: 40 مع علامات و 300 بدون علامات (340 إجمالي) .. GENERATED FROM PYTHON SOURCE LINES 78-79 تقرير التصنيف .. GENERATED FROM PYTHON SOURCE LINES 79-81 .. code-block:: Python print(classification_report(true_labels, predicted_labels)) .. rst-class:: sphx-glr-script-out .. code-block:: none precision recall f1-score support 0 1.00 1.00 1.00 27 1 0.82 1.00 0.90 37 2 1.00 0.86 0.92 28 3 1.00 0.80 0.89 35 4 0.92 1.00 0.96 24 5 0.74 0.94 0.83 34 6 0.89 0.96 0.92 25 7 0.94 0.89 0.91 35 8 1.00 0.68 0.81 31 9 0.81 0.88 0.84 24 accuracy 0.90 300 macro avg 0.91 0.90 0.90 300 weighted avg 0.91 0.90 0.90 300 .. GENERATED FROM PYTHON SOURCE LINES 82-83 مصفوفة الارتباك .. GENERATED FROM PYTHON SOURCE LINES 83-89 .. code-block:: Python from sklearn.metrics import ConfusionMatrixDisplay ConfusionMatrixDisplay.from_predictions( true_labels, predicted_labels, labels=lp_model.classes_ ) .. image-sg:: /auto_examples/semi_supervised/images/sphx_glr_plot_label_propagation_digits_001.png :alt: plot label propagation digits :srcset: /auto_examples/semi_supervised/images/sphx_glr_plot_label_propagation_digits_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 90-94 رسم التنبؤات الأكثر غموضًا ----------------------------------- هنا، سنختار ونعرض أفضل 10 تنبؤات غير مؤكدة. .. GENERATED FROM PYTHON SOURCE LINES 94-98 .. code-block:: Python from scipy import stats pred_entropies = stats.distributions.entropy(lp_model.label_distributions_.T) .. GENERATED FROM PYTHON SOURCE LINES 99-100 اختيار أفضل 10 علامات غير مؤكدة .. GENERATED FROM PYTHON SOURCE LINES 100-102 .. code-block:: Python uncertainty_index = np.argsort(pred_entropies)[-10:] .. GENERATED FROM PYTHON SOURCE LINES 103-104 رسم .. GENERATED FROM PYTHON SOURCE LINES 104-119 .. code-block:: Python import matplotlib.pyplot as plt f = plt.figure(figsize=(7, 5)) for index, image_index in enumerate(uncertainty_index): image = images[image_index] sub = f.add_subplot(2, 5, index + 1) sub.imshow(image, cmap=plt.cm.gray_r) plt.xticks([]) plt.yticks([]) sub.set_title( "التنبؤ: %i\nالحقيقي: %i" % (lp_model.transduction_[image_index], y[image_index]) ) f.suptitle("التعلم بكمية صغيرة من البيانات المعلمة") plt.show() .. image-sg:: /auto_examples/semi_supervised/images/sphx_glr_plot_label_propagation_digits_002.png :alt: التعلم بكمية صغيرة من البيانات المعلمة, التنبؤ: 1 الحقيقي: 2, التنبؤ: 2 الحقيقي: 2, التنبؤ: 8 الحقيقي: 8, التنبؤ: 1 الحقيقي: 8, التنبؤ: 1 الحقيقي: 8, التنبؤ: 1 الحقيقي: 8, التنبؤ: 3 الحقيقي: 3, التنبؤ: 8 الحقيقي: 8, التنبؤ: 2 الحقيقي: 2, التنبؤ: 7 الحقيقي: 2 :srcset: /auto_examples/semi_supervised/images/sphx_glr_plot_label_propagation_digits_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.455 seconds) .. _sphx_glr_download_auto_examples_semi_supervised_plot_label_propagation_digits.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/semi_supervised/plot_label_propagation_digits.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/semi_supervised/plot_label_propagation_digits.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_label_propagation_digits.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_label_propagation_digits.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_label_propagation_digits.zip ` .. include:: plot_label_propagation_digits.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_