Skip to content

Instantly share code, notes, and snippets.

@CatChenal
Last active October 22, 2021 19:51
Show Gist options
  • Save CatChenal/1f68b3870ab2396685aa15291bd6e152 to your computer and use it in GitHub Desktop.
Save CatChenal/1f68b3870ab2396685aa15291bd6e152 to your computer and use it in GitHub Desktop.
Installing SOMPY on Colab

Importing SOMPY from github in Colab

Need: SOMPY is not packaged on pipy, and conda is not installed on the colab platform (as of 9/21):

Run this in a colab cell:

!pip install git+https://github.com/sevamoo/SOMPY.git

Then import and test:

import sompy
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors

# Generate sample data & plot
# Modified from sklearn example: auto_examples/cluster/plot_kmeans_plusplus.html

n_samples = 4000
n_components = 4

X, y_true = make_blobs(n_samples=n_samples,
                       centers=n_components,
                       cluster_std=0.60,
                       random_state=0)

## Plot sample data
colors = list(mcolors.TABLEAU_COLORS)

plt.figure()
plt.title("Starting data")
for i in range(n_components):
    cluster_data = y_true == i
    plt.scatter(X[cluster_data, 0], X[cluster_data, 1],
                c=colors[i], marker='.', s=10)
plt.xticks([])
plt.yticks([])

plt.show();
d = int(n_samples * .01)
mapsize = [d, d]

som = sompy.SOMFactory.build(X, mapsize, mapshape='planar', lattice='rect',
                             normalization='var', initialization='pca',
                             neighborhood='gaussian',
                             training='batch',
                             name='somfac')

som.train(n_job=1, verbose='info')   # verbose: [None, 'info', 'debug']

Show codebook (default):

som_map = sompy.mapview.View2DPacked(50, 50, 'Codebook', text_size=14, col_size=6)
# changing col_sz has no effect??

som_map.show(som)

Problem with sizing cluster map (figure is huge):

som_map.show(som, what='cluster');

Build & show umatrix:

umv = sompy.umatrix.UMatrixView(50, 50, 'umatrix', show_axis=True, text_size=14, show_text=True)

umv.build_u_matrix(som, distance=1, row_normalized=False);

# Next, show fn returns warning:
"""
/usr/local/lib/python3.7/dist-packages/sompy/visualization/umatrix.py:123: MatplotlibDeprecationWarning: 
Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.
In a future version, a new instance will always be created and returned.
Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
  ax = self._fig.add_subplot(111)
"""
umv.show(som, distance=1, row_normalized=False, show_data=True, contour=True, blob=False);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment