.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_2d_clustering.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_2d_clustering.py: 2D Clustering with SomVQ ======================== Demonstrates unsupervised clustering on synthetic 2D data using ``SomVQ``. Because the data is 2-dimensional, both the input points and the learned neuron positions can be visualized in the same space. .. GENERATED FROM PYTHON SOURCE LINES 10-18 Train SomVQ ----------- ``SomVQ`` is the unsupervised variant of DBGSOM — no class labels needed. Key hyperparameters: - ``spreading_factor=0.9``: controls lateral network growth (lower = more compact) - ``max_neurons=200``: upper bound on neuron count - ``sigma_end=0.9``: neighborhood radius at end of training .. GENERATED FROM PYTHON SOURCE LINES 18-39 .. code-block:: Python from pathlib import Path import matplotlib.pyplot as plt import numpy as np import seaborn as sns from sklearn.preprocessing import scale from dbgsom.SomVQ import SomVQ data = scale(np.load(Path("data") / "clusterable_data.npy")) som = SomVQ( n_iter=500, spreading_factor=0.9, sigma_end=0.9, random_state=32, max_neurons=200, ) som.fit(data) .. raw:: html
SomVQ(max_neurons=200, random_state=32, sigma_end=0.9, spreading_factor=0.9)
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 40-43 Network Visualization --------------------- Input data colored by cluster assignment; gray lines show neuron connections. .. GENERATED FROM PYTHON SOURCE LINES 43-86 .. code-block:: Python edges = list(som.som_.edges) weights = som.weights_ fig, ax = plt.subplots(figsize=(5, 5)) for edge in edges: ax.plot( [ som.som_.nodes().data()[edge[0]]["weight"][0], som.som_.nodes().data()[edge[1]]["weight"][0], ], [ som.som_.nodes().data()[edge[0]]["weight"][1], som.som_.nodes().data()[edge[1]]["weight"][1], ], color="gray", linewidth=0.5, ) sns.scatterplot( ax=ax, x=data[:, 0], y=data[:, 1], s=4, alpha=0.5, hue=som.predict(data), palette="Set1", legend=False, ) sns.scatterplot( ax=ax, x=weights[:, 0], y=weights[:, 1], hue=[1] * len(som.neurons_), palette="Set1", s=10, legend=False, ) ax.set_title("SOM Network – Neurons and Cluster Assignments") ax.set_xlabel("Feature 1") ax.set_ylabel("Feature 2") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_2d_clustering_001.png :alt: SOM Network – Neurons and Cluster Assignments :srcset: /auto_examples/images/sphx_glr_plot_2d_clustering_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 87-91 Quantization Error per Neuron ----------------------------- Each neuron colored by mean quantization error — higher error (darker) indicates regions where data density is not well represented. .. GENERATED FROM PYTHON SOURCE LINES 91-94 .. code-block:: Python som.plot(color="error").show() .. image-sg:: /auto_examples/images/sphx_glr_plot_2d_clustering_002.png :alt: plot 2d clustering :srcset: /auto_examples/images/sphx_glr_plot_2d_clustering_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 95-99 Topographic Function -------------------- Topographic error as a function of distance threshold. Lower values indicate better topology preservation. .. GENERATED FROM PYTHON SOURCE LINES 99-108 .. code-block:: Python te = som.topographic_function(data) fig, ax = plt.subplots() ax.plot(te[1], te[0]) ax.set_xlabel("Distance threshold") ax.set_ylabel("Topographic error") ax.set_title("Topographic Function") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_2d_clustering_003.png :alt: Topographic Function :srcset: /auto_examples/images/sphx_glr_plot_2d_clustering_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.587 seconds) .. _sphx_glr_download_auto_examples_plot_2d_clustering.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_2d_clustering.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_2d_clustering.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_2d_clustering.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_