# Adaptive Solar Power Forecasting based on Machine Learning Methods
## 3 The Algorithms to Realize ASPF
### Improved k-means Clustering  
Figure 5 shows a typical daily solar power profile in different weather conditions, i.e., sunny, cloudy and rainy. _sunny_ 1 and _sunny_ 2 represent the sunny day in winter and summer, respectively. _cloudy_ and _rainy day_'s solar power are different from the sunny days'. Therefore, considering the differences between these different weather types, we first cluster the data into several specific groups. Figure 4: The principle of _Updating judgment_. _k-means_ is an effective, robust and widely used clustering technique [28]. It aims to cluster the data \(\mathbb{N}=[1,2,\cdots,N]\) observations into \(K\) clusters, in which each observation belongs to the cluster with the nearest mean, and different clusters have low similarity with each other. It is implicitly assumed that features within a cluster are _\"homogeneous\"_ and that different clusters are _\"heterogeneous\"_[26; 29]. Here, we adopt _k-means_ as a basis for clustering our data. Given a dataset \(\mathbb{S}=\{x_{n}\}_{n=1}^{N}\) of \(N\) points and the number \(K\) of desired clusters, for each group, the corresponding generalized centroids are usually denoted as \(\mathbb{C}=\{C_{1},C_{2},\cdots,C_{K}\}\), and \(C_{k}\in R^{q\times 1}\). The standard _k-means_ finds the optimal cluster centers \(\{C_{1}^{*},C_{2}^{*},\cdots,C_{K}^{*}\}\) such that the sum of the 2-norm distance between each point \(x_{n}\), and the nearest cluster center \(C_{k}\) is minimized [20; 21]:  
\[\min:\;\sum_{k=1}^{K}\left(\sum_{n=1}^{N}\eta_{n,k}\parallel x_{n}-C_{k} \parallel^{2}\right) \tag{1}\]  
where \(\eta_{n,k}\in[0,1]\), is the coefficient which represents the degree of how the _n_th sample belongs to the _k_th cluster. For simplicity, \(\eta_{n,k}=1/2\) in this paper. \(C_{k}\) is changed from \(C_{k}^{\mathit{giv}}\) to \(C_{K}^{*}\) during the calculation, and \(C_{k}^{\mathit{giv}}\) denotes the initial given clustering center for the _k_th group. As mentioned in Section 1, the traditional _k-means_ is sensitive to the initial center \(C_{k}^{\mathit{giv}}\), and different initializations can lead to different final results. In recirculation iteration calculation, the _k-means_ often converges to one or more clusters which contains very few or even no data point if the cluster center is given randomly. To overcome this problem, we employ a simplified method which combines potential function and density function to get the initial cluster centers [21]. Firstly, for a given \(K\), the density function of any sample \(x_{n}\) is:  
\[D_{n}^{(0)}=\sum_{j=1}^{N}\left(1+f_{r_{d}}\parallel x_{n}-x_{j}\parallel^{2} \right)^{-1} \tag{2}\]  
where \(f_{r_{d}}\) is the function of effective radius of the neighborhood density, and the more data around sample \(x_{n}\), the greater \(D_{n}^{(0)}\) is. \(f_{r_{d}}=(\alpha N(N-1))/(\sum_{n=1}^{N}\sum_{j=1}^{N}\parallel x_{n}-x_{j} \parallel^{2})\), and \(\alpha\) is one constant. For a known sample set, the value of \(f_{r_{d}}\) is easy to get. After the density of each data point has been computed, the data point \(x_{1}^{*}\) with the highest density will be selected as the first cluster center \(C_{1}^{\mathit{giv}}=x_{1}^{*}\), and \(D_{1}^{*}=\max\{D_{n}^{(0)},n\in\mathbb{N}\}\) is the density value. Then, the density function in the subsequent cluster becomes:  
\[D_{n}^{(k)}=D_{n}^{(k-1)}-D_{k}^{*}\left(1+f_{r_{d}}\parallel x_{n}-x_{k}^{* }\parallel^{2}\right)^{-1} \tag{3}\]  
Figure 5: The solar power for different weather types. where \(k=1,2,\cdots,K-1\), and \(D_{k}^{*}=\max\{D_{n}^{(k-1)},n\in\mathbb{N}\}\), so the remaining cluster centers \(C_{k}^{giv}=x_{k}^{*}\) can be identified as \(x_{k}^{*}\) according \(D_{k}^{*}\). Based on this result, the function Equation (1) will be convergent through few iterations, and no initial clustering will be empty. _k-means_ consists of two phases: one for determining the initial centroids and the other for assigning data points to the nearest clusters and then recalculating the cluster means.