# EV Charging Behavior Analysis Using Hybrid Intelligence for 5G Smart Grid
## 4 Simulation
### k-NN Classification for EV Driver Behavior  
After clustering the data in the dataset by K-means and HITL methods, we also need to classify the new EV users' behavior to manage and optimize the charging behavior of new electric vehicle users. However, it is unrealistic to re-cluster the entire data set every time new data is added. So, we would like to use the K-Nearest Neighbors (k-NN) method to classify new EV driver behavior according to the results of the K-means clustering algorithm. k-NN is a supervised learning method that classifies new data by measuring the distance between new data points and the remaining marked data points. In the k-NN algorithm, all the selected neighbors have been correctly classified. It is a distance-based algorithm where the category of new data can be determined based on the category of the nearest neighbors, e.g., using the Euclidean method (Equation (1)) to calculate the distance between data points. Then, we can select the training data of the first k distances closest to the new data and assign it the same data category. There is no fixed rule for the selection of \(k\) values. Generally, according to the distribution of samples, a smaller value is best, and a suitable \(k\) value can be determined by cross-validation. The detailed description of the k-NN algorithm is shown in Figure 9.  
Figure 8: The clustering results under HITL based K-means algorithm. Figure 9: The k-nearest neighbors algorithm. We define the training error rate as the ratio of the K-nearest training sample marker to the input marker, and the error rate is expressed as:  
\[\frac{1}{\mathrm{k}\sum_{x_{i}\in N_{k}(x)}I\left(y_{i}\
eq c_{j}\right)}=1-1/ \mathrm{K}\sum\
olimits_{x_{i}\in N_{k}(x)}I\left(y_{i}=c_{j}\right), \tag{7}\]  
and as a result, the proper \(k\) can maximize the coefficient below in the training set. \[1/\mathrm{K}\sum\limits_{x_{i}\in N_{k}(x)}I\left(y_{i}=c_{j}\right) \tag{8}\]  
After that, we use cross-validation to evaluate the performance of the model, which can prevent over-fitting. As mentioned before, there were 223 data in total after screening, which we divided into six groups. As shown in Figure 10, the first five groups were used for cross-validation. In cross-validation, four groups of data were used as a training set and 1 group of data remained as a test set. Each group of data had an opportunity to be test set. The result of cross-validation results showed that when \(k=5\), the training error rate was the smallest, and the correct rate for different groups acting as the test set is shown in Figure 11. Finally, the remaining 48 data were used as the final verification set, and the accuracy was found to be 97.91%. The confusion matrix is used to visualize the performance of an algorithm [34]. In predictive analysis, the confusion matrix is a table made up of False Positives (FP), False Negatives (FN), True Positives (TP), and True Negatives (TN). By using a confusion matrix, it is easy to see if the learning machine will confuse two similar classes. Assuming that the False Positives, False Negatives, True Positives, and True Negatives represent the corresponding number of samples respectively:  
\[\mathrm{TP}+\mathrm{FP}+\mathrm{TN}+\mathrm{FN}=\mathrm{Total}. \tag{9}\]  
Figure 11: The correct rate of cross-validation. Figure 10: Cross-validated data grouping. The classification result's \"confusion matrix\" in this experiment is shown in Figure 12.  
In order to comprehensively consider the generalizable performance of the model, the Receiver Operating Characteristics (ROC) curve can be drawn to see the quality of the model partition type [35]. In the case of uneven class distribution, the ROC curve is usually more efficient. In the ROC curve, the vertical axis is the True Positive Rate (TPR), and the horizontal axis is the False Positive Rate (FPR), which are defined as Equations (10) and (11). Figure 8:0  
Figure 8:1  
Area Under ROC Curve (AUC) is the area under the ROC curve and is an indicator of the performance of the algorithm.