# An Unsupervised Anomaly Detection Based on Self-Organizing Map for the Oil and Gas Sector
## 3 The Research Approach
### Quality of Self-Organizing Map  
Several methods in the literature are used to validate the quality of the SOM. [PERSON] [47] validates the SOM according to two parameters concerning the quality of the network learning (Quantization Error, QE) and the quality of the projection of the source data onto the output map (Topographic Error, TE). At the end of the learning process, each input data x is assigned to a weight on the output map that best represents it \(\mathrm{w}^{\mathrm{i}}_{\mathrm{i}\mathrm{p}}\)[48]. The difference \(\left\|\mathrm{x}(\mathrm{k})-\mathrm{w}^{\mathrm{i}}_{\mathrm{i}\mathrm{p} }(\mathrm{k})\right\|\) between the input and its associated weight expresses the quantization error. Through Equation (18), it is possible to calculate the average quantization error, which numerically represents how similar the final map is to the initial dataset. \[\mathrm{QE}=\frac{\sum_{k=1}^{K}\left\|\mathrm{x}(\mathrm{k})-\mathrm{w}^{ \mathrm{i}}_{\mathrm{i}\mathrm{p}}(\mathrm{k})\right\|}{K} \tag{18}\]  
One way to reduce the value of QE is to increase the number of output nodes to have the samples distributed more sparsely over the map, but in this way, the direct correlation with TE would be lost. The quality of the data projection on the output map is determined by considering the Topographic Error (Equation (19)). This parameter defines the percentage of vectors for which the first and second BMUs are not adjacent. Equation (20) expresses how this value is calculated. \[\mathrm{TE}=\frac{1}{D}\sum_{k=1}^{D}\mathrm{err}(\mathrm{x}(\mathrm{k})) \tag{19}\]  
\[\mathrm{err}(\mathrm{x}(\mathrm{k}))= \begin{cases}1,&\mathrm{if}\ \mathrm{p}^{*}_{ \mathrm{i}}(\mathrm{k}),2\mathrm{p}^{*}_{\mathrm{i}}(\mathrm{k})\in\mathrm{N} ^{*}_{\mathrm{n}}\\ 0,&\mathrm{otherwise}\end{cases}  \tag{20}\]  
After verifying the method and training the network, the input data are projected onto the output map. The nodes that make up the output grid accommodate only one class type, but sometimes this does not happen. Therefore, an analysis of cluster purity is conducted to uniquely assign a single class to each cell in the map [49]. Purity is a metric for how much a cluster contains a single class (Equation (21)). First, this parameter is calculated: count the number of data points from each cluster's most common class type. Then, divide the total data points by the sum of all clusters. Formally, given a collection of clusters M and a set of classes C, both splitting N data points, purity may be defined as:  
\[\frac{1}{\mathrm{N}}\sum_{\mathrm{m}\in\mathrm{M}}\max_{\mathrm{c}\in\mathrm{ C}}\left|\mathrm{m}\cap\mathrm{c}\right| \tag{21}\]  
The confusion matrix is another tool to validate the results of the SOM Network [50]. A confusion matrix is a C \(\times\) C matrix used to assess the effectiveness of a classification model, where C represents the number of target classes [51]. The matrix compares the actual goal values to the machine learning model's predictions (Figure 6). Thus, this method evaluates a classification model's performance by computing measures such as accuracy, precision, recall, and F1-score [34]. The parameters that make up the matrix are:  
* True positives (TP): the actual value is positive, and the predicted is also positive. * True negatives (TN): the actual value is negative, and the prediction is also negative. * False positives (FP): the actual is negative, but the prediction is positive. * False negatives (FN): the actual is positive, but the prediction is negative. The following parameters can be exploited to analyse the properties of the confusion matrix:  
* **Accuracy** (Equation (22))--is the percentage of samples in the test set that were categorized correctly. \[\text{Accuracy}=\frac{\text{TP}+\text{TN}}{\text{TP}+\text{FP}+\text{TN}+\text{ FN}}\] (22)
* **Precision** (Equation (23))--out of all the samples, how many belonged to the positive class compared to how many the model projected would.