# Anomaly Detection of Metallurgical Energy Data Based on iForest-AE
## 3 Experiment and Result Analysis
### Experimental Procedure and Result Analysis  
The iForest algorithm was selected to calculate the anomaly score for 7200 metallurgical energy data. According to the characteristics of electric energy data of 1# workshop, the best parameters were selected by manual experience [29], and the results are shown in Table 1.  
Figure 2 shows the result of calculating the anomaly score of partial data through the iForest algorithm. Among the 7200 training data, 684 data with an anomaly score higher than 0.60 were uncertain data. This part of the data was excluded, and the remaining part of the data was selected for model training. The autoencoder was used to detect abnormality in metallurgical energy data. The random search algorithm [30] was used to find the best parameters, and the group of parameters with the minimum training error was taken as the final parameters. The results are shown in Table 2. \begin{table}
\begin{tabular}{c c} \hline
**Parameter** & **Value** \\ \hline n\_estimators & 100 \\ max\_features & 1 \\ contamination & 0.1 \\ max\_samples & auto \\ n\_jobs & None \\ \hline \end{tabular}
\end{table} 
Table 1: Parameter settings for the iForest algorithm. \begin{table}
\begin{tabular}{c c} \hline
**Parameter** & **Values** \\ \hline Batch\_size & 128 \\ Epoch & 80 \\ Loss & Mean square error \\ Optimizer & Adam \\ \hline \end{tabular}
\end{table} 
Table 2: Parameter settings for the autoencoder algorithm. Figure 2: Anomaly scores of data. The training and validation sets were fed into the autoencoder network. Their loss leveled off at \(1.976\times 10^{-4}\) and \(2.684\times 10^{-4}\) after 80 training iterations, as shown in Figure 3, indicating that the trained model could effectively reconstruct normal data. To evaluate the effect of anomaly detection, recall, precision, accuracy, and F1 score were used to evaluate the model. The F1 score was used for it is a comprehensive metrics. Since the reconstruction error was used as the anomaly score, the selection of the threshold had a great influence on the detection result, which was related to the model detection accuracy. We selected several values as the threshold. For each value, five experiments were carried out and the average value of the F1 score of the five experiments was calculated. The threshold corresponding to the maximum F1 score was selected as the final threshold. The results of the experiments are shown in Figure 4.  
It can be seen from the above experiments' results that when the threshold was 0.01, the F1 score obtained the maximum value of 0.981, and the anomaly detection effect was best at this time. When the threshold was too small, the detection effect was poor. The reason is that the threshold was too small, which caused the model to regard the normal reconstruction error fluctuation generated by the normal energy data as abnormal, so some  
Figure 4: Relationship between threshold selection and detection effect. Figure 3: Training error of training set and validation set. normal data were judged as abnormal data. When the threshold was too large, the detection effect of the model also decreased. The reason is that the threshold selection was too large, which relaxed the requirements for the reconstruction error of normal data. Therefore, abnormal data were determined as normal data. In the test data, the electricity consumption data of metallurgical enterprise 1# workshop from 26 June 2021 to 12:00 on 28 June 2021 were taken as an example. After data normalization, the electricity consumption data were shown in Figure 5. The normal data were screened out by the iForest algorithm for model training. The model encoded and decoded those data by the autoencoder, and the reconstructed data are shown in Figure 6.  
Normal electricity consumption data fluctuated in a low range. However, due to sensor failures and interruptions in field communication transmissions, the collected data did conform to the normal data distribution pattern, and the abnormal data had a large error after passing through the autoencoder trained by the normal data.