SuSi: Supervised Self-Organizing Maps for Regression and Classification in Python
Felix M. Riese Sina Keller














	1 Institute of Photogrammetry and Remote Sensing,
	
 Karlsruhe Institute of Technology, Karlsruhe, Germany 
	
(felix.riese, sina.keller)@kit.edu











In many research fields, the sizes of the existing datasets vary widely.
Hence, there is a need for machine learning techniques which are well-suited for these different datasets.
One possible technique is the self-organizing map (SOM), a type of artificial neural network which is, so far, weakly represented in the field of machine learning.
The SOM's unique characteristic is the neighborhood relationship of the output neurons.
This relationship improves the ability of generalization on small datasets.
SOMs are mostly applied in unsupervised learning and few studies focus on using SOMs as supervised learning approach.
Furthermore, no appropriate SOM package is available with respect to machine learning standards and in the widely used programming language Python.
In this paper, we introduce the freely available Supervised Self-organizing maps (SuSi) Python package which performs supervised regression and classification.
The implementation of SuSi is described with respect to the underlying mathematics.
Then, we present first evaluations of the SOM for regression and classification datasets from two different domains of geospatial image analysis.
Despite the early stage of its development, the SuSi framework performs well and is characterized by only small performance differences between the training and the test datasets.
A comparison of the SuSi framework with existing Python and  packages demonstrates the importance of the SuSi framework.
In future work, the SuSi framework will be extended, optimized and upgraded e.g. with tools to better understand and visualize the input data as well as the handling of missing and incomplete data.




SuSi: Supervised Self-Organizing Maps for Regression and Classification in Python
    
Felix M. Riese1, Sina Keller1
    December 30, 2023
=================================================================================







§ INTRODUCTION





With increasing computing power and increasing amount of data, artificial neural networks (ANN) have become a standard tool for regression and classification tasks.
Feed-forward neural networks and convolutional neural networks (CNN) are the most common types of ANN in current research.
According to the no free lunch theorem by <cit.>, a variety of possible tools is necessary to be able to adapt to new tasks.

One underrepresented type of ANNs is the self-organizing map (SOM).
The SOM was introduced by <cit.>.
It is a shallow ANN architecture consisting of an input layer and a 2-dimensional (2D) grid as output layer.
The latter is fully connected to the input layer.
Besides, the neurons on the output grid are interconnected to each other through a neighborhood relationship.
Changes on the weights of one output neuron also affect the neurons in its neighborhood.
This unique characteristic decreases overfitting of the training datasets.
Further, the 2D output grid visualizes the results of the SOM comprehensibly.
This plain visualization does not exist in the majority of ANNs.



In the following, we give a brief overview of the various SOM applications in different fields of research.
Most SOMs are applied in unsupervised learning like clustering, visualization and dimensionality reduction.
A good overview of SOMs as unsupervised learners and their applications in the research field of water resources is presented by <cit.>.
SOMs are also applied to the scientific field of maritime environment research <cit.>.
One major application of SOMs is clustering data <cit.> and cluster-wise regression <cit.>.


SOMs can be combined with other machine learning techniques.
For example, the output of the unsupervised SOM is used by <cit.> as input for a support vector regressor to forecast stock prices.
<cit.> present the combination of SOMs with linear regression in hydrology.
SOMs can also be used for data fusion, e.g. for plant disease detection <cit.>.
<cit.> add majority voting to SOMs for the application as supervised classifier.
The combination of SOMs and nearest-neighbor classification is shown by <cit.> for the classification of land use.
Additional combinations of unsupervised SOMs and supervised algorithms used for classification are presented by <cit.>.
One example for the application of SOMs to solve non-linear regression tasks is presented by <cit.> in the field of robotics.


Two of the most popular programming languages for machine learning applications are Python and .
Programming frameworks like scikit-learn <cit.> in Python have simplified the application of existing machine learning techniques considerably.
While in the programming language  the kohonen package <cit.> provides a standardized framework for SOMs, in Python there exists no such standard SOM package, yet.


In this paper, we introduce the Python package Supervised Self- organizing maps (SuSi) framework for regression and classification.
It is the first Python package that provides unsupervised and supervised SOM algorithms for easy usage.
The SuSi framework is available freely on GitHub <cit.>.
This ensures the maintainability and the opportunity for future upgrades by the authors as well as any member of the community.
The implementation was briefly introduced in <cit.> with respect to the regression of soil-moisture <cit.> and the estimation of water quality parameters <cit.>.
The main contributions of this paper are:

	
  * the implementation of the SuSi framework including the combination of an unsupervised SOM and a supervised SOM for regression and classification tasks that is able to perform on small as well as on large datasets without significant overfitting,
	
  * the mathematical description of all implemented processes of the SuSi framework with a consistent variable naming convention for the unsupervised SOM in <Ref> and the supervised SOM in <Ref>,
	
  * a first evaluation of the regression and classification capabilities of the SuSi framework in <Ref>,
	
  * a detailed comparison of the SuSi framework with existing Python and  packages based on a list of requirements in <Ref> and
	
  * an outlook into the future of the SuSi framework in <Ref>.
















§ SUSI PART 1: UNSUPERVISED LEARNING




In the following, we describe the architecture and mathematics of the unsupervised part of the SuSi framework.
The grid of a SOM, the map, can be implemented in different topologies.
In this paper, we use the simplest topology: the 2D rectangular grid consisting of n_row× n_column nodes.
This grid is fully connected to the input layer as each node is connected to all n input features via n weights.
The variable naming conventions of the SuSi framework are given in <Ref>.
The training process of the unsupervised SOM is illustrated in <Ref> and consists of the following steps:

	
  *  Initialize the SOM.
	
  *  Get random input datapoint.
	
  *  Find best matching unit (BMU) (cf. <Ref>).
	
  *  Calculate learning rate (cf. <Ref>) and neighborhood function (cf. <Ref>).
	
  *  Calculate neighborhood distance weight matrix (cf. <Ref>).
	
  *  Modify SOM weight matrix (cf. <Ref>).
	
  * Repeat from step <ref> until the maximum number of iterations is reached.








The initialization approach of a SOM mainly affects the speed of its training phase.
The SOM weights of the SuSi framework are initialized randomly at this stage of development.
<cit.> and <cit.>, for example, propose more sophisticated initialization approaches like applying a principal component analysis.
In the following subsections, the training of an unsupervised SOM is described in detail.





 §.§ Finding the best matching unit




During the search for the best matching unit (BMU), the input datapoint is compared to all weights on the SOM grid.
The SOM node that is the closest one to the input node according to the chosen distance metric is the BMU.
Several distance metrics can be applied.
The most common distance metric is the Euclidean distance defined as

    d(x, x') = √(∑_i=1^n (x_i-x_i')^2),

with a dimension n of the vectors x, x'.
Another possible choice is the Manhattan distance which is defined as the sum of the absolute distances per element:

    d(x, x') = ∑_i=1^n |x_i-x_i'|.

The Tanimoto distance as third option is defined as the distance or dissimilarity between two boolean (binary: T, F) vectors x, x':

    d(x, x') = R/c_TT + c_FF+R    with  R=2(c_TF + c_FT),

with c_ij as the number of occurrences of x_k=i and x_k'=j for all elements k as defined in <cit.>.
The Mahalanobis distance between two 1-dimensional vectors x, x' is defined as

    d(x, x') = √((x-x')V^-1(x-x')^T),

with the covariance matrix V of the two vectors.
The default distance metric of the SuSi framework is the Euclidean distance defined in <Ref>.






 §.§ Learning rate




For a faster convergence and to prevent oscillations, decreasing learning rates are often implemented in ANNs.
The learning rate α of the SOM training is a function that decreases from a value α_0 with increasing number of iterations.
In general, there is an infinite number of possible functions for the learning rate.
In the following, we present several functions implemented into the SuSi framework.
In <cit.>, different learning rates for SOMs are introduced:


    α(t)    = α_0·1/t,
    α(t)    = α_0·( 1 - t/t_max),
    α(t)    = α_0^t/t_max    with α_0 = 0.005.


In <cit.>, the following learning rate was applied:

    α(t) = α_0·exp(-t/t_max).

The implementation of <cit.> includes not only a start value for the learning rate but also an end value α_end:

    α(t) = α_0·(α_end/α_0)^t/t_max.

In <Ref>, some examples for the behaviour of the functions are plotted.
The default learning rate function of the SuSi framework is set according to <Ref>.







 §.§ Neighborhood function




Similar to the learning rate, the neighborhood function is monotonically decreasing.
We include the following three widely-used functions in the SuSi framework.
Equivalent to <Ref>, the neighborhood function in <cit.> is defined as

    σ(t) = σ_0·(1-t/t_max),

with σ_0 as initial value of the neighborhood function.
In <cit.>, the neighborhood function is implemented as

    σ(t) = σ_0·exp(-t/t_max),

equivalent to <Ref>.
In <cit.>, the neighborhood function is defined similarly to <Ref> as

    σ(t) = σ_0·(σ_end/σ_0)^t/t_max.

The default neighborhood function of the SuSi framework is set according to <Ref>.




 §.§ Neighborhood distance weight




The neighborhood distance weight is a function of the number of iterations and the distance d(c, i) between the BMU c and every other node i on the SOM grid.
The distance d(c, i) between the BMU c and node i is defined as the Euclidean distance (cf. <Ref>) on the map grid.
In this paper, we give two examples for neighborhood distance weights.
<cit.> use a Pseudo-Gaussian neighborhood distance weight.
The weight between the BMU c and the node i on the SOM grid is defined as

    h_c,i(t) = exp( -d^2/2·σ(t)^2),

with the neighborhood function from <Ref> and the Euclidean distance d(c, i) on the SOM grid.
This definition of a neighborhood distance weight is the default setting of the SuSi framework.
Another possible neighborhood distance weight is the Mexican Hat <cit.> defined as

    h_c,i(t) = ( 1 - d^2/σ^2)exp( -d^2/2·σ(t)^2),

again with neighborhood function of <Ref> and Euclidean distance d(c, i) on the SOM grid.
The implications of the chosen neighborhood distance weight definitions on the SOM are investigated in e.g. <cit.> and <cit.>.





 §.§ Adapting weights




The two most commonly used approaches to adapt the SOM weights are the online and the batch mode.
The weights of the SOM are adapted based on the learning rate and the neighborhood distance weight.
The online mode is described in detail in <cit.>.
After each iteration, all weights of the SOM are adapted to the current datapoint x(t) as follows:

    w_i(t+1) = w_i(t) + α(t)· h_c,i(t)·( x(t) - w_i(t)),

with neighborhood function h_c,i(t), learning rate α(t), weight vector w_i(t) of node i at iteration t.
In the batch mode <cit.>, the whole dataset consisting of N datapoints is used in every iteration.
Each weight w_i is adapted as follows:

    w_i(t+1) = ∑_j=1^N h_c,i(t) ·x_j/∑_j=1^N h_c,i(t) ,

with the neighborhood function h_c, i(t) from <Ref> and the weight vector w_i(t+1) of node i at iteration t+1.
In this first stage of development, the SuSi package provides only the online algorithm.




 §.§ Trained unsupervised SOM




After reaching the maximum number of iterations t_max, the unsupervised SOM is fully trained.
New datapoints can be allocated to their respective BMU which will be used in <Ref>.
Note that not every node on the SOM grid has to be linked to a datapoint from the training dataset, since there can be more SOM nodes than datapoints.














§ SUSI PART 2: SUPERVISED LEARNING




To apply the SuSi framework for solving supervised regression or classification tasks, we attach a second SOM to the unsupervised SOM.
The flowchart of the second, supervised SOM is illustrated in <Ref>.
The two SOMs differ with respect to the dimension of the weights and their estimation algorithm.
The weights of the unsupervised SOM have the same dimension as the input data.
Thus, adapting these weights often changes the BMU for each input datapoint. 
In contrast, the weights of the supervised SOM have the same dimension as the target variable of the respective task.
One has to distinguish between two cases: regression and classification.
In the regression case, the weights are one-dimensional and contain a continuous number.
In the classification case, the weights contain a class.
By combining  the unsupervised and the supervised SOM, the former is used to select the BMU for each datapoint while the latter links the selected BMU to a specific estimation.
In the following, we describe the different implementations for regression and classification tasks.






 §.§ Regression




The implementation of the regression SOM is described in <cit.> using the example of the soil-moisture regression based on hyperspectral data.
The training of the regression SOM proceeds analogous to the unsupervised SOM: first, the SOM is initialized randomly.
Again, it iterates randomly through the dataset (cf. Step <ref>).
In each iteration, the BMU is found for the current datapoint based on the trained unsupervised SOM (cf. Steps <ref>, <ref>).
The BMUs do not change for the datapoints during the training since the unsupervised SOM is fully trained.
Then, the neighborhood function, the learning rate and the neighborhood distance weight matrix are calculated similarly to the algorithm of the unsupervised SOM (cf. Steps <ref>, <ref>).
Finally, the weights are adapted to the label y(t) of the input datapoint x(t) (cf. Step <ref>).




In the case of the regression SOM, the label is a continuous value and the weights of the regression SOM can be modified similarly to the process described in <Ref>.
After the training (and in the case of a 1-dimensional target variable), the regression SOM consists of a map with a continuous distribution of the regression target variable.
To apply the trained regression SOM to a new dataset, the BMUs needs to be found by the unsupervised SOM.
For each datapoint in the new dataset, the estimated output value of the SuSi framework is the weight of the found BMU on the regression SOM.
The regression SOM is illustrated in <Ref>.





 §.§ Classification




In the case of a classification task, the labels are discrete.
In contrast to the commonly used majority voting approach <cit.>, we have implemented a training process similar to the adaptation approach of the unsupervised SOM (cf. <Ref>):

	
  *  Initialize the classification SOM.
	
  *  Get random input datapoint with label.
	
  * Find BMU based on trained unsupervised SOM.
	
  * Calculate learning rate and neighborhood function.
	
  *  Calculate neighborhood distance weight.
	
  *  Calculate class-change probability matrix.
	
  *  Modify classification SOM weight matrix.
	
  * Repeat from step <ref> until the maximum number of iterations is reached.


The classification SOM is illustrated in <Ref>.
The initialization in step <ref> contains a simple majority vote: each node is assigned to the class representing the majority of datapoints allocated to the respective node.
The steps <ref> to <ref> are implemented similarly to the regression SOM in <Ref>.
To modify the discrete weights of the classification SOM, we introduce the class-change probability P_c, i(t) in step <ref>.
In the regression SOM, the SOM nodes around the BMU are adapted to the current datapoint with a certain probability depending on the learning rate and the neighborhood distance weight.
Since the labels y are discrete in a classification task, this process needs to be adapted.
In the following, we explain our proposed adaptation.

For datasets with imbalanced class distributions, meaning datasets with significantly different number of datapoints per class, we provide the possibility to re-weight the dataset.
The optional class weight is defined as 

    w_class(j) = 
    		N / (n_classes· N_j)    if class weighting
    
    		1    otherwise,

with the number of datapoints N, the number of datapoints N_j of class j and the number of classes n_classes.
Similar to <Ref>, we define a term that affects the modifying of the SOM weights.
Since the modifications need to be discrete, we work with probabilities.
The probability for a class change of node i with BMU c(x) of the datapoint x(t) with label y(t) is defined as

    P_c, i(t) = w_y(t)·α(t) · h_c, i(t),

with the class weight w_y(t) (cf. <Ref>), the learning rate α(t) (cf. <Ref>) and the neighborhood distance weight h_c, i(t) (cf. <Ref>).
To decide if a node changes its assigned class, a binary decision rule is created based on this probability.
A simple threshold of e.g. 0.5 would lead to a static SOM after a certain number of iterations.
Therefore, we include randomization into the decision process.
For every node in every iteration, a random number u_i(t) is generated which is uniformly distributed between 0 and 1.
The modification of the weights is then implemented based on the class change probability P_c, i(t) defined in <Ref> as follows:

    w_i(t+1) = 
    	
    		y(t)      if  u_i(t) < P_c, i(t)
    
    		w_i(t)      otherwise,

with the label y(t) linked the datapoint x(t) of the current iteration t.
After the maximum number of iterations is reached, the classification SOM is fully trained.
Then, every node on the SOM grid is assigned to one class of the dataset.
To apply the classification SOM on new data, the BMU needs to be found for each datapoint with the unsupervised SOM.
This process is similar to the one in the trained regression SOM.
The estimation of the classification SOM for this datapoint is equivalent to the weight of the neuron in the classification SOM at the position of the selected BMU.













§ EVALUATION




For a first evaluation of the regression and classification capabilities of the introduced SuSi framework, we rely on two datasets from different domains of geospatial image analysis.
The regression is evaluated in <Ref> with a hyperspectral dataset on the target variable soil moisture.
The evaluation of the classification SOM in <Ref> is performed based on the freely available Salinas valley dataset for land cover classification from hyperspectral data.
The results of the two different SOM applications are compared against a random forest (RF) estimator <cit.>.
Finally, the SuSi package is compared to existing SOM packages in the programming languages Python and  in <Ref>.





 §.§ Regression of soil moisture





The performance of the regression SOM is evaluated based on the soil-moisture dataset measured during a field campaign and published in <cit.>.
A similar evaluation is published in <cit.> with a preceding version of the SOM algorithm and code.
The dataset consists of 679 datapoints collected by a Cubert UHD 285 camera.
One datapoint consists of 125 bands between 450950.
A soil moisture sensor measured the reference values in a range of 2542 soil moisture.
For the validation of the estimation performance and the generalization capabilities of the SOM, the dataset is randomly divided into a training and a test subset in the ratio 1:1.
The training of the estimator is performed on the training subset and the evaluation is performed on the test subset.


The regression SOM is set up with default parameters with the exception of the grid size and the number of iterations.
The grid size of the SOM is 35× 35.
The unsupervised and the supervised SOMs are trained with 2500 iterations each.
These hyperparameters can be further optimized depending on the applied dataset.
The RF regressor is set up with 1000 estimators and the scikit-learn default hyperparameters <cit.>.
For the evaluation, we choose the coefficient of determination R^2.


The regression SOM achieves R^2 = 90.2 on the test subset.
This score implies that the regression SOM is able to generalize on this dataset.
Interestingly, the result for the training subset is only marginally better with  R^2_train = 90.7.
In comparison, the RF regressor results in R^2 = 93.6 and R^2_train = 99.2 on the dataset.
To conclude, the SOM seems to be robust regarding overfitting.
In this case, the R^2_train score could function as out-of-bag estimate for the dataset similar to <cit.>.
When dealing with small datasets, the SOM provides the advantage of not necessitating on the split of the dataset.

In <Ref>, the distribution of the BMUs of the soil-moisture dataset is shown.
No clear maximum exists, rather a random and uniform distribution is recognizable.
<Ref> illustrates further that despite the fact that the dataset is smaller than the number of SOM nodes, the training takes advantage of the whole SOM grid.
The spread over the whole SOM grid makes generalization possible.
The continuous regression output for each SOM node is presented in <Ref>.
Although the SOM nodes outnumber the input datapoints, each SOM node is linked to a soil moisture value.









 §.§ Classification of land cover





The Salinas valley dataset[<http://www.ehu.eus/ccwintco/index.php/Hyperspectral_Remote_Sensing_Scenes>.] is a freely available land cover dataset consisting of 512× 217 pixels collected by the 224-band AVIRIS sensor in California.
The spatial resolution of this dataset is 3.7.
Of the 224 bands in the range of 4002500, the 20 water absorption bands are discarded, namely bands 108-112, 154-167, 224.
The dataset consists of 54129 datapoints with reference data of 16 classes including vegetation classes and bare soil.
Compared to the dataset used in the evaluation of the regression SOM in <Ref>, this dataset is considered as large dataset.
We apply a 5-fold cross-validation on this dataset for the evaluation of the classification SOM.
The evaluation results are the average results over all five cross-validation combinations.


Similar to <Ref>, the default SOM hyperparameters are used except for the grid size and the number of iterations.
The grid size of the SOM is 40× 20.
The unsupervised SOM is trained with 5000 iterations and the supervised SOM is trained with 20000 iterations.
The hyperparameters of the classification SOM can be further optimized.
The RF classifier is set up with 100 estimators and the scikit-learn default hyperparameters <cit.>.
For the evaluation, we choose the metrics overall accuracy (OA), average accuracy (AA) and Cohen's kappa coefficient κ.
The OA is defined as the ratio between the number of correctly classified datapoints and the size of the dataset.
The AA is the sum of the recall of each class divided by the number of classes with the recall of a class being the number of correctly classified instances (datapoints) of that class, divided by the total number of instances of that class.
Cohen's kappa coefficient κ is defined as

    κ = OA - θ/1 - θ,

with the hypothetical probability of chance agreement θ.


The classification results of the complete dataset are shown in <Ref>.
The SOM achieves a test OA of 71.6, AA =72.8 and  κ = 68.4.
The training OA is 72.5, the training AA is 73.7 and the κ score on the training subsets is 69.4.
In contrast, the RF classifier achieves a test OA of 90.0, AA =93.8 and κ = 88.9 while the RF training metrics are all at 100.
The RF classifier performs significantly better than the classification SOM which has not been fully optimized.
Analog to the regression (cf. <Ref>), the results for the classification SOM with respect to the training and test subsets are similar while the RF classifier shows overfitting on the training subset.

In <Ref>, the distribution of the BMUs of the dataset is illustrated.
Although the dataset is much larger compared to <Ref>, not all nodes are linked to a datapoint while some nodes are linked to a significant number of datapoints.
The distribution of the classification output of the SOM is shown in <Ref>.
Nodes assigned to the same classes are closer together on the SOM grid due to the inclusion of the neighborhood during the training process.









 §.§ Comparison of SuSi and other packages




In the following section, we compare the SuSi framework with existing Software packages.
We compare it with the Python packages SOMPY <cit.>, SimpSOM <cit.>, MiniSom <cit.>, TensorFlow SOM <cit.> and the  kohonen package <cit.>.
All entitled packages are freely available, regularly maintained (in the last year) and include unsupervised clustering.
<Ref> illustrates this comparison.
So far, no supervised SOM package for Python is available that matches the defined requirements requirements (cf. <Ref>).
The fact that the unsupervised SOM packages are all maintained regularly implies a significant interest in Python SOM packages.
Overall, the SuSi package is perfectly suited for an easy use and a variety of applications.















§ CONCLUSION AND OUTLOOK




SOMs are applied in a variety of research areas.
In this paper, we introduce the Supervised Self-organizing maps (SuSi) package in Python.
It provides unsupervised and supervised SOM algorithms for free and easy usage.
The mathematical description of the package is presented in <Ref>.
We demonstrate first regression and classification results in <Ref>.
Overall, the performance of the SuSi package is satisfactory, taking into account that a further optimization is possible.
The regression is performed on a small dataset with 679 datapoints while the classification SOM is applied on a large dataset with 54129 datapoints.
The application to these two datasets illustrates the ability of the SuSi framework to perform on differently sized datasets.
Although, the RF regressor and classifier perform better in the given tasks, the SOM performance metrics of the training and the test subset differ only slightly.
This shows the robustness of the SuSi framework.
Further, the performance metric based on the training dataset could function as out-of-bag estimate for the dataset.
This implies that a dataset does not have to be split which improves training especially on small datasets.
Finally, we compare the SuSi framework against different existing SOM frameworks in Python and  with respect to e.g. features, documentation and availability.
We conclude that there is a significant interest in a standardized SOM package in Python which is perfectly covered by the SuSi framework.

In the future, the SuSi package will be extended, optimized and upgraded.
The handling of missing and incomplete data, as described in <cit.>, is one example for a possible new extension.
In addition, the 2D SOM grid visualizes the results of the SOM and therefore ensures to better understand the underlying dataset.
This ability to learn from underlying datasets can be extended as described e.g. by <cit.>.
Furthermore, we will present applications on new datasets as well as share best practices to make the SuSi framework as valuable as possible for its users.




0.9		
	
	



