.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_topology/plot_01_neural_gas.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 or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_02_topology_plot_01_neural_gas.py: Neural gas learns the shape of the data ======================================= Neural gas moves *all* representatives towards every drawn sample, with steps that decay exponentially with their distance rank. Early on, when the neighborhood range is large, the codebook contracts towards the data mean; as the range shrinks the representatives spread out and finally cover the data with roughly equal probability mass. The data set combines a noisy cube, a bent line and a circle: shapes of three different intrinsic dimensions. .. GENERATED FROM PYTHON SOURCE LINES 12-38 .. image-sg:: /auto_examples/02_topology/images/sphx_glr_plot_01_neural_gas_001.png :alt: initialization, 100 adaptation steps, 1000 adaptation steps, 10000 adaptation steps, 30000 adaptation steps, 60000 adaptation steps :srcset: /auto_examples/02_topology/images/sphx_glr_plot_01_neural_gas_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none t_max= 0 quantization error 1.925 t_max= 100 quantization error 1.605 t_max= 1000 quantization error 1.388 t_max= 10000 quantization error 0.669 t_max= 30000 quantization error 0.594 t_max= 60000 quantization error 0.574 | .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from graphclust import NeuralGas from graphclust.datasets import load_boxlinecircle X = load_boxlinecircle() rng = np.random.default_rng(0) sub = X[rng.choice(len(X), 2000, replace=False)] fig = plt.figure(figsize=(12, 7.5)) for i, t_max in enumerate([0, 100, 1000, 10000, 30000, 60000]): # [snippet:ng] ng = NeuralGas(n_nodes=300, max_iter=max(t_max, 1), random_state=0).fit(X) # [/snippet:ng] ax = fig.add_subplot(2, 3, i + 1, projection="3d") ax.scatter(*sub.T, s=1, c="#C9D3DD") ax.scatter(*ng.nodes_.T, s=6, c="#0072B2") ax.set_title("initialization" if t_max == 0 else f"{t_max} adaptation steps", fontsize=10) ax.view_init(20, -60) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([]) print(f"t_max={t_max:6d} quantization error {ng.quantization_error(X):.3f}") plt.tight_layout() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.223 seconds) .. _sphx_glr_download_auto_examples_02_topology_plot_01_neural_gas.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/graphclust/main?urlpath=lab/tree/notebooks/auto_examples/02_topology/plot_01_neural_gas.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_neural_gas.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_neural_gas.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_neural_gas.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_