# Adaptive Solar Power Forecasting based on Machine Learning Methods
## 3 The Algorithms to Realize ASPF
### Improved k-means Clustering
The second phase is necessary for finding the optimal \(k\), and the data within the same type are very similar, while the data from different types are not close. In this paper, the method discussed in [30] is adopted to obtain optimal number of \(k\). Here, an inner distance in the same group is defined as:  
\[D_{w}(n,k)=\frac{1}{N_{k}-1}\sum_{g=1}^{N_{k}}\parallel x_{k,g}-x_{k,n} \parallel^{2} \tag{4}\]  
where \(D_{w}(n,k)\) is the average distance from \(n\)th data in cluster \(k\) to other data in the same group, and \(N_{k}\) denotes the number of data in the \(k\)th cluster, \(n=[1,2,\cdots,N_{k}]\). If \(D_{w}(n,k)\) is small, the data in this cluster is also compact correspondingly. The distance for data between different groups is defined as:  
\[D_{b}(n,k)=\min_{1\leq k\leq K,k\
eq n}\frac{1}{N_{k}}\sum_{g=1}^{N_{k}} \parallel x_{k,g}-x_{k,n}\parallel^{2} \tag{5}\]  
where \(D_{b}(n,k)\) represents the minimal average distance of the \(n\)th data from cluster \(k\) to other clusters. In addition, the larger value of the \(D_{b}(n,k)\) is, the feature of _\"heterogeneous\"_ is more clear. In order to synthesize the two factors, \(D_{b}(n,k)\) and \(D_{w}(n,k)\), the linear combination is used to balance the relationship between them. At the same time, in order to enable the indicator to analyze the validity of the data clustering and to avoid the index from being affected by the dimension, the fractional operation is employed. The index to calculate the optimal \(k\) is defined as:  
\[I_{k}(n,k)=\frac{D_{b}(n,k)-D_{w}(n,k)}{D_{b}(n,k)+D_{w}(n,k)} \tag{6}\]  
where \(I_{k}(n,k)\in[-1,1]\). Since the \(D_{b}(n,k)\) requires at least two clusters, \(k\) starts from 2 to \(K\). From Equation (6), it can be seen that when the cluster number \(k\) is taken from 2 to \(K\), the optimal \(k\) is confirmed if the \(I_{k}(n,k)\) gets the maximal value. Usually, \(K\leq\sqrt{N}\), \(K=Int(\sqrt{N})\)[31]. Eventually, based on the condition of getting a suitable initial cluster centers and the optimal number of \(k\), pseudo-code for the improved _k-means_ clustering is summarized in Algorithm 1. ```
Step 1: For each \(n\in\mathbb{N}\), \(k\in[2,K]\). Step 2: In each cluster \(k\), the initial cluster center \(C_{k}^{giv}\) is obtained by executing Equations (2) and (3), then \(C_{k}\) & \(N_{k}\) can be calculated by Equation (1). Step 3: Find and record \(I_{k}\) based on Equations (4)-(6). Compare \(k\) with \(K\), and if \(k<K\), \(k:=k+1\) and repeat Step 2; otherwise, jump to Step 4. Step 4: Acquire the maximal \(I_{k}\) and the corresponding number \(k^{*}\), recalculate Equations (2) and (3), then the optimal clustering data is obtained through Equation (1), and the optimal centers are \(C_{k}^{*}\), weather types are \(G_{k}\), \(k=1,2,\cdots,k^{*}\). ```  
**Algorithm 1**Improved _k-means_ Algorithm  
In this way, solar power data is divided into several groups by the proposed improved _k-means_ clustering. According to the standardized-residual method and Lagrange Interpolation Polynomial [32], the abnormal data is removed and the remaining data are normalized.