pythonsom

mvpa2: restore SOM from Kohonen layer


I'm trying to implement self-organising maps in Python using pymvpa2 SOM mapper.
It works just fine, but how do I save the resulting SOM?

h5save didn't work for me and I started wondering, if I can create an empty SOM instance and then attach a saved Kohonen layer to it? However, when I try to feed the Kohonen matrix to an empty mapper I get this error:

...
size = (15,15)
som_new = SimpleSOMMapper(size, 1900, learning_rate=0.05)
som_new.K = som_trained.K
>> AttributeError: can't set attribute



Is there a way to override this?


Solution

  • You can't access som.K but if you look into som.py from the package you'll see that the script uses som._K to modify Kohonen layers.

    Use this approach:

    som_new.is_trained = True
    som_new._K = som_trained.K
    

    And then you'll have your map transferred to an empty mapper. But be careful as som.py doesn't check what you add to the Kohonen layer this way: it can be a string, an int or whatever.