Search code examples
pythonplotsom

Any example/idea of how to plot the U-MATRIX using python (working on SOM)?


Working on SOM implementation using Python ? I want to know how to generate the u-matrix


Solution

  • One implementation can be found in the SUSI package (pip3 install susi). You can use it like that:

    import susi
    import matplotlib.pyplot as plt
    from sklearn.datasets import make_blobs
    
    # get data (replace this part with your data)
    X, y = make_blobs(n_samples=100, n_features=2, centers=3)
    
    # initialize and fit SOM
    som = susi.SOMClustering()
    som.fit(X)
    
    u_matrix = som.get_u_matrix()
    plt.imshow(np.squeeze(u_matrix), cmap="Greys")
    plt.colorbar()
    plt.show()
    

    which results in this plot:

    U-Matrix of SOM

    The code and the plot are taken from susi/SOMClustering.ipynb. You find the implementation of the u-matrix there as well.