`build`

method from SOMFactory creates self organizing map, give it the size of the map and the data. the method takes the size of the map and the data.
#
# `initialization='random'`

is a type of initial node weights, the random values to all weights.
# In[14]:
som = sompy.SOMFactory.build(
data_full,
mapsize,
initialization='random')
som.train(n_job=1, verbose='info')
# For visualizaion used mapview.View2DPacked.
# In[47]:
v = sompy.mapview.View2DPacked(10, 10, 'example', text_size=8)
v.show(som)
# The som could recognize four clusters. Although the scope of the cluster are far from ideal.
#
# The "cluster" method is using [sklearn.Kmeans](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html) for predict clusters on the raw data.
# In[48]:
v = sompy.mapview.View2DPacked(5, 5, 'test',text_size=8)
som.cluster(n_clusters=4)
som.cluster_labels
# In[49]:
v.show(som, what='cluster');
# Let's look at the visualization of clusters on the grid. For this use HitMapView.
# In[50]:
h = sompy.hitmap.HitMapView(8, 8, 'hitmap', text_size=8, show_text=True)
h.show(som);
# The grid of self organizing map have a two types:
# - square grid
# - hexagonal grid
#
# Now we will create a new SOM and add some arguments for best result.
#
# Increasing map size.
# In[51]:
mapsize = [20,20]
# `lattice='rect'`

is a square grid of SOM.
#
# `normalization='var'`

is the type of [normalization](https://en.wikipedia.org/wiki/Normalization_(statistics)) of the input data. 'var' is [t-statistic](https://en.wikipedia.org/wiki/T-statistic).
# $$\frac{X-\bar{X}}{s}$$
# - $X$ is input data.
# - $\bar{X}$ is average of input data.
# - $s$ is [standard deviation](https://en.wikipedia.org/wiki/Standard_deviation).
#
# `initialization='pca'`

is a type of initial node weights, principal component initialization.
#
# `neighborhood='gaussian'`

use the 'gaussian' function for "measure of neighborhood".
# In[64]:
som = sompy.SOMFactory.build(
data_full,
mapsize,
lattice='rect',
normalization='var',
initialization='random',
neighborhood='gaussian')
som.train(n_job=1, verbose='info')
# In[65]:
v = sompy.mapview.View2DPacked(10, 10, 'example', text_size=8)
v.show(som)
# In[62]:
v = sompy.mapview.View2DPacked(5, 5, 'test',text_size=8)
som.cluster(n_clusters=4)
som.cluster_labels
# In[63]:
h = sompy.hitmap.HitMapView(8, 8, 'hitmap', text_size=8, show_text=True)
h.show(som);
# Now let's use the SOM for [the Iris Dataset](https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html)
# In[5]:
from sklearn import datasets
# In[6]:
iris = datasets.load_iris()
# In[7]:
iris.target_names
# In[11]:
mapsize = [20,20]
# In[13]:
iris.target
# In[41]:
get_ipython().run_cell_magic('time', '', "som = sompy.SOMFactory.build(\n iris.data, \n mapsize, \n lattice='rect', \n normalization='var', \n initialization='random', \n neighborhood='gaussian')\nsom.train(n_job=1, verbose=False)")
# In[31]:
v = sompy.mapview.View2DPacked(10, 10, 'iris', text_size=8)
v.show(som, which_dim=[0,1,2])
# The raw data.
# In[33]:
view2D = sompy.mapview.View2D(10,10,"Iris_raw_data",text_size=8)
view2D.show(som, col_sz=4, which_dim="all",desnormalize=True)
# After training, SOM separates four distinct clusters, which is true.
# In[34]:
iris.data.shape
# Visualization of a grid.
# In[35]:
v = sompy.mapview.View2DPacked(5, 5, 'test',text_size=8)
som.cluster(n_clusters=3)
som.cluster_labels
# In[36]:
h = sompy.hitmap.HitMapView(8, 8, 'hitmap_iris', text_size=8, show_text=True)
h.show(som, );
# Also we can build the [U-matrix](https://en.wikipedia.org/wiki/U-matrix). Use umatrix.UMatrixView for visualization.
# In[37]:
u = sompy.umatrix.UMatrixView(20, 20, 'umatrix')
UMAT = u.build_u_matrix(som)
UMAT = u.show(som)
# ### `normalization='var'`

is only one implementation of the normalization.
#
# Kohonen self-organizing maps solve many issues and are a powerful tool for data analysis. In this article, we learned the principle of the SOM, as well as considered small examples of clustering and data visualization. But at the moment, the SOM is losing its popularity in favor of other algorithms.