# Machine Learning and Bagging to Predict Midterm Electricity Consumption in Saudi Arabia
## 3 Materials and Methods  
This section presents the description of the used dataset, the methodology, and the applied machine learning techniques.  
The dataset for electricity consumption was collected from the Saudi Electrical Company (SEC). The collected dataset covers the monthly electricity consumption for eighteen locations. The dataset contains the electricity consumption in Saudiia Arabia from January 2010 to September 2020 and it has six attributes and includes 2298 instances. A summary of the electricity consumption dataset is shown in Table 2.  
The methodology of this study can be summarized as follows: First, in the preprocessing phase, the values for the input features, as well as the target value, were normalized within a 0-1 range based on Equation (1).  
\[X_{norm}=\frac{X-X_{min}}{X_{max}-X_{min}} \tag{1}\]  
The reason for the normalization was due to the different ranges throughout the dataset features. Then, since the nature of this study is heavily based on temporal qualities, the month feature was split into sine and cosine facets. In other words, as the month attribute is considered a cyclic attribute where month 1 (January) is closer to month 12 (December) in temporal characteristics, it was converted into 2 dimensions using the sine and cosine facets of each month based on Equations (2) and (3). Thus, they adequately represent the actual temporal similarities between months.  
\[x_{sin}=\sin\biggl{(}\frac{2\times\pi\times x}{x_{max}}\biggr{)} \tag{2}\]  
\[X_{cos}=\cos\biggl{(}\frac{2\times\pi\times x}{x_{max}}\biggr{)} \tag{3}\]  
In this research, the dataset followed a 70-30 split so that 70% of the dataset is used in the training phase, and 30% is reserved for testing the model.  
After splitting the dataset into their separate training and testing datasets, the K\({}^{*}\) method was optimized through tuning the parameters. A 10-fold cross-validation technique was used in the training phase to ensure the model was not overfitted to the test set. The K\({}^{*}\) model only had one parameter to tune: the global blend parameter.  
Moreover, the bagging ensemble method was applied to the dataset. It was optimized similarly to what was performed with the standalone K\({}^{*}\) model by means of tuning their parameters.  
Following the parameter tuning, the models were applied to the test data to evaluate their capabilities based on various evaluation metrics, namely correlation coefficient (CC), mean absolute percentage error (MAPE), mean absolute error (MAE), root mean squared percentage error (RMPSE), and root mean squared error (RMSE). Equations (4)-(8) show their mathematical representations and Figure 1 shows the framework for the proposed methodology.  
\[CC=\frac{n(\sum xy)-(\sum x)(\sum y)}{\sqrt{\left[n\sum x^{2}-(\sum x)^{2} \right]\left[n\sum y^{2}-(\sum y)^{2}\right]}} \tag{4}\]  
where \(x\) and \(y\) are a random pair of variables.  
\[MAPE=\frac{1}{n}\sum_{l=1}^{n}\left|\frac{A_{t}-F_{t}}{A_{t}}\right| \tag{5}\]  
\[RMSPE=\sqrt{\frac{1}{n}\cdot\sum_{i=1}^{n}\left(\frac{A_{i}}{F_{i}}\right)^{ 2}}\cdot 100\% \tag{6}\]  
\begin{table}
\begin{tabular}{l l} \hline \hline
**Attribute** & **Description** \\ \hline City & City name. \\ Year & The numeric year in which the consumption data were gathered. \\ Month (Sine) & The sine value of the numeric month. \\ Month (Cosine) & The cosine value of the numeric month. \\ Electricity consumption of current month & Monthly electricity consumption in Kilowats. \\ (Target value) Electricity consumption of next month & Electricity consumption of the next month in Kilowats. \\ \hline \hline \end{tabular}
\end{table}
Table 2: Dataset description.  
\[MAE=\frac{1}{n}\sum_{t=1}^{n}\left|A_{t}-F_{t}\right| \tag{7}\] \[RMSE=\sqrt{\frac{1}{n}\cdot\sum_{i=1}^{n}\left(F_{i}-A_{i}\right)^ {2}} \tag{8}\]