.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_digit_classification.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_auto_examples_plot_digit_classification.py: Digit Classification with SomClassifier ======================================== Classifies handwritten digits from the sklearn digits dataset using ``SomClassifier`` wrapped in a scikit-learn ``Pipeline`` with ``StandardScaler``. The dataset contains 1 797 grayscale images (8×8 pixels, 64 features) of digits 0–9. .. GENERATED FROM PYTHON SOURCE LINES 11-16 Build and Train Pipeline ------------------------ ``SomClassifier`` is a drop-in replacement for any scikit-learn classifier. Wrapping it in a ``Pipeline`` applies ``StandardScaler`` automatically during both ``fit`` and ``predict``. .. GENERATED FROM PYTHON SOURCE LINES 16-36 .. code-block:: Python from sklearn.datasets import load_digits from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from dbgsom.SomClassifier import SomClassifier digits_X, digits_y = load_digits(return_X_y=True) som = SomClassifier( spreading_factor=0.6, n_iter=500, sigma_end=0.5, random_state=42, tau_2=0.01, ) pipe = Pipeline(steps=[("scaler", StandardScaler()), ("som", som)]) pipe.fit(digits_X, digits_y) .. raw:: html
Pipeline(steps=[('scaler', StandardScaler()),
                    ('som',
                     SomClassifier(random_state=42, sigma_end=0.5,
                                   spreading_factor=0.6, tau_2=0.01))])
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 37-40 Evaluation ---------- Print accuracy and topographic quality metrics. .. GENERATED FROM PYTHON SOURCE LINES 40-45 .. code-block:: Python print(f"Accuracy: {pipe.score(digits_X, digits_y):.4f}") print(f"Topographic error: {som.topographic_error_:.4f}") print(f"Quantization error: {som.quantization_error_:.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none Accuracy: 0.8753 Topographic error: 0.1809 Quantization error: 5.0955 .. GENERATED FROM PYTHON SOURCE LINES 46-50 Grid Layout – Neurons by Dominant Class ---------------------------------------- Each neuron colored by its dominant digit class. Spatial clustering shows how the SOM organizes the digit space. .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: Python som.plot(layout="grid", color="label", palette="Set1").show() .. image-sg:: /auto_examples/images/sphx_glr_plot_digit_classification_001.png :alt: plot digit classification :srcset: /auto_examples/images/sphx_glr_plot_digit_classification_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-58 PCA Layout ---------- Same coloring but neurons arranged by PCA of their weight vectors. Reveals the structure of the learned representation in weight space. .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: Python som.plot(layout="pca", color="label", palette="Set1", X=digits_X).show() .. image-sg:: /auto_examples/images/sphx_glr_plot_digit_classification_002.png :alt: plot digit classification :srcset: /auto_examples/images/sphx_glr_plot_digit_classification_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.974 seconds) .. _sphx_glr_download_auto_examples_plot_digit_classification.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_digit_classification.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_digit_classification.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_digit_classification.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_