# Software Integration of Power System Measurement Devices with AI Capabilities
## 5 Real-Time Application: Machine Learning-Based Load Identification
### Data Collection and Models Training  
The first step to be taken was the acquisition of data in order to proceed with the training of the different models. For this purpose, the tool presented in Section 3.2 was used, as shown in Figure 14, so that, using a PQube3 quality analyzer, the operating data of each of the LED lamps were recorded for 35 min. Values were also recorded with no load connected so that the status of the line could also be identified. Note that although several quality measurements were collected in the database, only voltage harmonics were used for identification. As there were three different lamp models, if each of them were connected to the mains, this would have distorted the waveform and generated different harmonics, as can be seen in the data collected (see Figure 15). Thus, the THD of each lamp must be calculated as  
\[THD=\sqrt{\sum_{h=2}^{40}\left(\frac{u_{h}}{u_{1}}\right)^{2}}\times 100\% \tag{1}\]  
where \(u_{h}\) is the amplitude of the harmonic \(h\), and \(u_{1}\) is the fundamental harmonic voltage, having been calculated as 0.538%, 0.594%, and 0.446%, respectively. Therefore, a dataset was created with the harmonics of each of the LED lamps, in addition to those of the unloaded line, which were recorded for approximately 35 min and collected at 1-second intervals, with a total of 2042 data recordings for each category. This amount allows supervised learning to classify the measured harmonics into one of  
Figure 14: Experimental setup and software tools. four categories according to the LED lamp connected to the line at any given time: \"None\", \"Lamp1\", \"Lamp2\", or \"Lamp3\". As is usual in this type of technique, the data must be subjected to preprocessing before being used. Specifically, a feature selection was performed, initially excluding harmonics that were too low and going from 39 to 21 harmonics. In addition, a scaling of the type MaxAbsScaler was applied so that each feature would be scaled according to its maximum absolute value, and the value of the feature for training would be in the range \([0.0,1.0]\). Figure 16, for illustrative purposes, shows the average normalized values of the 21 harmonics considered. The data were then appropriately divided into a training subset and a test subset using the train_test_split function from the Scikit_learn library, which randomly selects values to ensure a balanced and correct distribution of data from each of the categories in both subsets. Proportions of 80% of training data and 20% for validation were used. This resulted in a subset of scaled training data, with approximately 1600 entries for each category, and a test subset, with approximately 400 values for each class. The first classifier implemented was a Neural Network. Although there is a wide variety of advanced neural networks suitable for these tasks (including RBF, CNN, BRNN,  
Figure 16: Normalized values of the considered average voltage harmonics on each of the three lamps. Figure 15: Average voltage harmonics (2 nd to 40 th) on each of the three lamps. KNN, DNN, GMDH, etc.) [17], for the sake of implementation and comparison with the other two methods, the classic Multi-layer Perceptron classifier structure available in Scikit-learn has been used, which allows for supervised learning by optimizing the log loss function. In this case, the input layer will be formed by 21 nodes and the output layer by 4 nodes (one for each of the considered categories). Configuring the NN requires optimizing the cross-tuning of a significant number of hyperparameters, which influence the accuracy of the network, as can be seen in Figure 17. To assist in this task, Scikit-learn implements the GridSearchCV function, which performs an exhaustive search over specified parameter values optimized by cross-validated grid search over a parameter grid.