.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "packages/scikit-learn/auto_examples/plot_digits_simple_classif.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_packages_scikit-learn_auto_examples_plot_digits_simple_classif.py: Simple visualization and classification of the digits dataset ============================================================= Plot the first few samples of the digits dataset and a 2D representation built using PCA, then do a simple classification .. GENERATED FROM PYTHON SOURCE LINES 8-13 .. code-block:: Python from sklearn.datasets import load_digits digits = load_digits() .. GENERATED FROM PYTHON SOURCE LINES 14-18 Plot the data: images of digits ------------------------------- Each data in a 8x8 image .. GENERATED FROM PYTHON SOURCE LINES 18-30 .. code-block:: Python import matplotlib.pyplot as plt fig = plt.figure(figsize=(6, 6)) # figure size in inches fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05) for i in range(64): ax = fig.add_subplot(8, 8, i + 1, xticks=[], yticks=[]) ax.imshow(digits.images[i], cmap="binary", interpolation="nearest") # label the image with the target value ax.text(0, 7, str(digits.target[i])) .. image-sg:: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_digits_simple_classif_001.png :alt: plot digits simple classif :srcset: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_digits_simple_classif_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 31-33 Plot a projection on the 2 first principal axis ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 33-44 .. code-block:: Python plt.figure() from sklearn.decomposition import PCA pca = PCA(n_components=2) proj = pca.fit_transform(digits.data) plt.scatter(proj[:, 0], proj[:, 1], c=digits.target, cmap="Paired") plt.colorbar() .. image-sg:: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_digits_simple_classif_002.png :alt: plot digits simple classif :srcset: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_digits_simple_classif_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 45-47 Classify with Gaussian naive Bayes ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 47-78 .. code-block:: Python from sklearn.naive_bayes import GaussianNB from sklearn.model_selection import train_test_split # split the data into training and validation sets X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target) # train the model clf = GaussianNB() clf.fit(X_train, y_train) # use the model to predict the labels of the test data predicted = clf.predict(X_test) expected = y_test # Plot the prediction fig = plt.figure(figsize=(6, 6)) # figure size in inches fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05) # plot the digits: each image is 8x8 pixels for i in range(64): ax = fig.add_subplot(8, 8, i + 1, xticks=[], yticks=[]) ax.imshow(X_test.reshape(-1, 8, 8)[i], cmap="binary", interpolation="nearest") # label the image with the target value if predicted[i] == expected[i]: ax.text(0, 7, str(predicted[i]), color="green") else: ax.text(0, 7, str(predicted[i]), color="red") .. image-sg:: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_digits_simple_classif_003.png :alt: plot digits simple classif :srcset: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_digits_simple_classif_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-83 Quantify the performance ------------------------ First print the number of correct matches .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: Python matches = predicted == expected print(matches.sum()) .. rst-class:: sphx-glr-script-out .. code-block:: none 395 .. GENERATED FROM PYTHON SOURCE LINES 86-87 The total number of data points .. GENERATED FROM PYTHON SOURCE LINES 87-88 .. code-block:: Python print(len(matches)) .. rst-class:: sphx-glr-script-out .. code-block:: none 450 .. GENERATED FROM PYTHON SOURCE LINES 89-90 And now, the ration of correct predictions .. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: Python matches.sum() / float(len(matches)) .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(0.8777777777777778) .. GENERATED FROM PYTHON SOURCE LINES 93-94 Print the classification report .. GENERATED FROM PYTHON SOURCE LINES 94-98 .. code-block:: Python from sklearn import metrics print(metrics.classification_report(expected, predicted)) .. rst-class:: sphx-glr-script-out .. code-block:: none precision recall f1-score support 0 0.97 0.95 0.96 37 1 0.83 0.85 0.84 41 2 0.89 0.84 0.86 49 3 0.93 0.83 0.88 47 4 0.93 0.90 0.92 42 5 0.89 0.95 0.92 42 6 0.98 0.97 0.97 60 7 0.81 0.98 0.88 47 8 0.65 0.87 0.75 39 9 0.97 0.63 0.76 46 accuracy 0.88 450 macro avg 0.89 0.88 0.87 450 weighted avg 0.89 0.88 0.88 450 .. GENERATED FROM PYTHON SOURCE LINES 99-100 Print the confusion matrix .. GENERATED FROM PYTHON SOURCE LINES 100-103 .. code-block:: Python print(metrics.confusion_matrix(expected, predicted)) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none [[35 0 0 0 1 0 0 1 0 0] [ 0 35 0 0 0 0 1 1 4 0] [ 0 1 41 0 0 0 0 0 7 0] [ 0 0 2 39 0 1 0 2 2 1] [ 0 1 0 0 38 0 0 2 1 0] [ 0 0 0 0 1 40 0 1 0 0] [ 0 0 1 0 1 0 58 0 0 0] [ 0 0 0 0 0 1 0 46 0 0] [ 0 2 0 1 0 1 0 1 34 0] [ 1 3 2 2 0 2 0 3 4 29]] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.124 seconds) .. _sphx_glr_download_packages_scikit-learn_auto_examples_plot_digits_simple_classif.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_digits_simple_classif.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_digits_simple_classif.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_digits_simple_classif.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_