.. note::
    :class: sphx-glr-download-link-note

    Click :ref:`here <sphx_glr_download_auto_examples_semi_supervised_plot_label_propagation_digits.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_semi_supervised_plot_label_propagation_digits.py:


===================================================
Label Propagation digits: Demonstrating performance
===================================================

This example demonstrates the power of semisupervised learning by
training a Label Spreading model to classify handwritten digits
with sets of very few labels.

The handwritten digit dataset has 1797 total points. The model will
be trained using all points, but only 30 will be labeled. Results
in the form of a confusion matrix and a series of metrics over each
class will be very good.

At the end, the top 10 most uncertain predictions will be shown.



.. code-block:: python

    print(__doc__)

    # Authors: Clay Woolam <clay@woolam.org>
    # License: BSD

    import numpy as np
    import matplotlib.pyplot as plt

    from scipy import stats

    from sklearn import datasets
    from sklearn.semi_supervised import label_propagation

    from sklearn.metrics import confusion_matrix, classification_report

    digits = datasets.load_digits()
    rng = np.random.RandomState(0)
    indices = np.arange(len(digits.data))
    rng.shuffle(indices)

    X = digits.data[indices[:330]]
    y = digits.target[indices[:330]]
    images = digits.images[indices[:330]]

    n_total_samples = len(y)
    n_labeled_points = 30

    indices = np.arange(n_total_samples)

    unlabeled_set = indices[n_labeled_points:]

    # #############################################################################
    # Shuffle everything around
    y_train = np.copy(y)
    y_train[unlabeled_set] = -1

    # #############################################################################
    # Learn with LabelSpreading
    lp_model = label_propagation.LabelSpreading(gamma=0.25, max_iter=5)
    lp_model.fit(X, y_train)
    predicted_labels = lp_model.transduction_[unlabeled_set]
    true_labels = y[unlabeled_set]

    cm = confusion_matrix(true_labels, predicted_labels, labels=lp_model.classes_)

    print("Label Spreading model: %d labeled & %d unlabeled points (%d total)" %
          (n_labeled_points, n_total_samples - n_labeled_points, n_total_samples))

    print(classification_report(true_labels, predicted_labels))

    print("Confusion matrix")
    print(cm)

    # #############################################################################
    # Calculate uncertainty values for each transduced distribution
    pred_entropies = stats.distributions.entropy(lp_model.label_distributions_.T)

    # #############################################################################
    # Pick the top 10 most uncertain labels
    uncertainty_index = np.argsort(pred_entropies)[-10:]

    # #############################################################################
    # Plot
    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('predict: %i\ntrue: %i' % (
            lp_model.transduction_[image_index], y[image_index]))

    f.suptitle('Learning with small amount of labeled data')
    plt.show()

**Total running time of the script:** ( 0 minutes  0.000 seconds)


.. _sphx_glr_download_auto_examples_semi_supervised_plot_label_propagation_digits.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download

     :download:`Download Python source code: plot_label_propagation_digits.py <plot_label_propagation_digits.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: plot_label_propagation_digits.ipynb <plot_label_propagation_digits.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
