.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_classical_maps/plot_02_neighborhood_maps.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_06_classical_maps_plot_02_neighborhood_maps.py: LPP, the self-organizing map and incremental grid growing ========================================================= Neighborhood-based maps preserve who is next to whom. Locality preserving projections find the *linear* projection that keeps the kNN graph's neighbors together. The self-organizing map fits a fixed lattice of prototypes into the data; the U-matrix (mean distance of a node to its lattice neighbors) reveals cluster borders as ridges. Incremental grid growing lets the lattice grow from 2 x 2 nodes where the error is largest and cut lattice links that span gaps in the data. .. GENERATED FROM PYTHON SOURCE LINES 13-42 .. image-sg:: /auto_examples/06_classical_maps/images/sphx_glr_plot_02_neighborhood_maps_001.png :alt: locality preserving projection, SOM: U-matrix and samples, incremental grid growing (majority class) :srcset: /auto_examples/06_classical_maps/images/sphx_glr_plot_02_neighborhood_maps_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from graphclust import IncrementalGridGrowing, LocalityPreservingProjection, SelfOrganizingMap from graphclust.datasets import load_wine from graphclust.plotting import plot_embedding, plot_graph X, y = load_wine() X = (X - X.mean(0)) / X.std(0) # [snippet:som] lpp = LocalityPreservingProjection(k=10).fit(X) som = SelfOrganizingMap(grid=(12, 16), topology="hexagonal", n_epochs=60).fit(X) igg = IncrementalGridGrowing(max_nodes=40, random_state=0).fit(X) # [/snippet:som] fig, axes = plt.subplots(1, 3, figsize=(14, 4)) plot_embedding(lpp.embedding_, y, ax=axes[0], title="locality preserving projection") P = som.grid_positions_ sc = axes[1].scatter(*P.T, c=som.umatrix(), cmap="Greys", s=70, marker="h") jit = np.random.default_rng(0).normal(0, 0.18, (len(X), 2)) plot_embedding(som.embedding_ + jit, y, ax=axes[1], s=10, title="SOM: U-matrix and samples") fig.colorbar(sc, ax=axes[1], shrink=0.7) lab = np.array([np.bincount(y[igg.labels_ == i], minlength=3).argmax() if (igg.labels_ == i).any() else -1 for i in range(len(igg.nodes_))]) plot_graph(igg.grid_positions_, igg.adjacency_, labels=lab, ax=axes[2], node_size=60, title="incremental grid growing (majority class)") axes[2].set_xticks([]) axes[2].set_yticks([]) plt.tight_layout() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.264 seconds) .. _sphx_glr_download_auto_examples_06_classical_maps_plot_02_neighborhood_maps.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/06_classical_maps/plot_02_neighborhood_maps.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_neighborhood_maps.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_neighborhood_maps.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_neighborhood_maps.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_