.. _sphx_glr_auto_examples_exercises_plot_cv_digits.py:


=============================================
Cross-validation on Digits Dataset Exercise
=============================================

A tutorial exercise using Cross-validation with an SVM on the Digits dataset.

This exercise is used in the :ref:`cv_generators_tut` part of the
:ref:`model_selection_tut` section of the :ref:`stat_learn_tut_index`.



.. image:: /auto_examples/exercises/images/sphx_glr_plot_cv_digits_001.png
    :align: center





.. code-block:: python

    print(__doc__)


    import numpy as np
    from sklearn.model_selection import cross_val_score
    from sklearn import datasets, svm

    digits = datasets.load_digits()
    X = digits.data
    y = digits.target

    svc = svm.SVC(kernel='linear')
    C_s = np.logspace(-10, 0, 10)

    scores = list()
    scores_std = list()
    for C in C_s:
        svc.C = C
        this_scores = cross_val_score(svc, X, y, n_jobs=1)
        scores.append(np.mean(this_scores))
        scores_std.append(np.std(this_scores))

    # Do the plotting
    import matplotlib.pyplot as plt
    plt.figure(1, figsize=(4, 3))
    plt.clf()
    plt.semilogx(C_s, scores)
    plt.semilogx(C_s, np.array(scores) + np.array(scores_std), 'b--')
    plt.semilogx(C_s, np.array(scores) - np.array(scores_std), 'b--')
    locs, labels = plt.yticks()
    plt.yticks(locs, list(map(lambda x: "%g" % x, locs)))
    plt.ylabel('CV score')
    plt.xlabel('Parameter C')
    plt.ylim(0, 1.1)
    plt.show()

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



.. container:: sphx-glr-download

    **Download Python source code:** :download:`plot_cv_digits.py <plot_cv_digits.py>`


.. container:: sphx-glr-download

    **Download IPython notebook:** :download:`plot_cv_digits.ipynb <plot_cv_digits.ipynb>`