# Software Integration of Power System Measurement Devices with AI Capabilities
## 5 Real-Time Application: Machine Learning-Based Load Identification
### Data Collection and Models Training
In this way, optimized hyperparameters were established with a configuration of two internal layers of 5 and 10 neurons, respectively, as well as through the use of a stochastic gradient descent (SGD) optimizer, with an adaptive learning rate and an activation function for the hidden layer of the hyperbolic tan type:  
\[f(x)=\frac{e^{x}-e^{-x}}{e^{x}+e^{-x}} \tag{2}\]  
As a result of training this model, a predictor with 100% accuracy was obtained in both the training and validation subsets. Figure 18 shows the evolution of the loss function during training and the confusion matrix obtained with the test data subset. In general, classifiers based on neural networks tend to exhibit strong generalization capabilities. However, sometimes, their black-box nature makes it challenging to extract information about the criteria used to classify an input value into specific categories. In addition, the computation time can be significant if a large number of hidden layers and neurons are used. In such situations, it may be appropriate to use a non-parametric classifier such as a Decision Tree. Figure 17: Number of neurons in the two hidden layers vs. accuracy with different activation functions. The goal of the DT is to construct a binary tree structure that efficiently classifies input values through a progressive features evaluation process until reaching a terminal node (leaf) that corresponds to the category associated to the input value. In this case, the input value would be the recorded harmonics. The conditions are established based on the values of the harmonics, and the result should be a leaf corresponding to each of the four categories. DT training consists of optimizing the decision rules at each node, based on input data, to achieve better divisions. This process often utilizes metrics such as the Gini Index to guide the optimization:  
\[Gini=1-\sum_{i=1}^{n}P_{i}^{2} \tag{3}\]  
where \(P_{i}\) is the probability that an element is classified in a given category. Thus, the Gini Index, which ranges from 0 to 1, determines the impurity of a set, indicating how mixed or heterogeneous the set is. Therefore, using the Scikit-learn library, a DT was trained to select the best split at each node, using the Gini Index, and with a maximum depth of 3 to reduce computational time during evaluation. The DT obtained achieved 100% accuracy for both the training and test subsets. Figure 19 shows the structure of the Decision Tree and the confusion matrix when evaluating the test dataset. As can be seen in Figure 19a the tree managed to establish four leaves with purity (\(Gini=0\)) corresponding to each of the four categories using a classification method based on a progressive binary division of the scaled values of harmonics H29, H5, and H27. The effectiveness of this classification approach becomes evident when examining the distribution of these harmonic values within the available dataset, as shown in Figure 20.  
This results in a classifier that enables rapid classification, though it may be more sensitive to the values of specific harmonics and thus potentially less robust compared to the NN approach. To mitigate this issue, it is possible to employ a classifier that utilizes a set of DTs, known as Random Forest, which combines the results from all of these trees. Although it is possible to use a large number of trees, in this case, keeping in mind the real-time implementation, only four trees were calculated. Each of them was trained on a subset of the training data to facilitate the search of diverse classifications. As a result, an RF with an accuracy of 100% on the training and test subsets was obtained, which was composed of the four DTs shown in Figure 21. As can be observed, the decisions were made based on the values of nine harmonics (H3, H5, H9, H16, H23, H27, H33, H34, and H36), thereby increasing robustness with respect to the DT. Figure 14:0 Multi-Layer Perceptron training and test results. The performance of these three classifiers applied in real time will be shown in the next section.