# Feature Construction Using Persistence Landscapes for Clustering Noisy IoT Time Series
## 5 Implementation  
This section describes optimal feature construction from persistence landscapes corresponding to time series. Specifically, our research describes an algorithm for selecting the _smallest order of persistence landscapes_ to be employed as features for clustering/classifying a large number of time series, in order to speed up the computing without sacrificing accuracy. Depending on the nature of the time series, the selected number could be one or a small integer--say, two or three. Our algorithm takes advantage of the monotonicity and non-negativity of persistence landscapes, to set up a scoring function that accumulates over the different orders. This is similar to the behavior of the total within-cluster sum of squares, as the number of clusters increases in unsupervised learning, where the _elbow method_ is used to select the appropriate number of clusters [30]. For \(n=1,\ldots,N\), let \(\tilde{\Gamma}^{(n)}(\omega_{j}),j=1,\ldots,T/2\) be the smoothed spectrum, \(\tilde{\Omega}^{(n)}=\{(\lambda_{k,1}^{(n)},\lambda_{k,2}^{(n)}),k=1,2,\ldots, \tilde{K}\}\) be the persistence diagram, and \(\texttt{PL}_{\
u}^{(n)}(\ell),\ \ell=1,\ldots,L\) be the \(\mathsf{v}\) order persistence landscapes. Algorithm 1 describes an approach to selecting the optimal (smallest) order \(\
u=\
u^{\mathsf{opt}}\), in the context of clustering \(N\) time series, each of length \(T\): specifically, it chooses the farthest point \((\
u^{\mathsf{opt}},S_{\
u^{\mathsf{opt}}})\) from the function formed by using points \((1,S_{1})\) and \((\
u,S_{\
u})\). The features \(\texttt{PL}_{\
u}^{(n)}(\ell),\
u=1,2,\ldots,\
u^{\mathsf{opt}};\ell=M_{1}, \ldots,M_{2}\) obtained from Algorithm 1 for each time series represent the important topological features of the data. Using these features within a K-means algorithm produces effective clustering of the \(N\) time series in less computing time than if all the persistence landscapes were employed as features. Let  
\[D(n_{1},n_{2})=\sqrt{\sum_{\
u=1}^{\mathsf{opt}}\sum_{\ell=M_{1}}^{M_{2}}( \texttt{PL}_{\
u}^{(n_{1})}(\ell)-\texttt{PL}_{\
u}^{(n_{2})}(\ell))^{2}}\]  
denote the Euclidean distance between the persistence landscapes of the \(n_{1}\)-th and \(n_{2}\)-th time series. Smaller (larger) Euclidean distance implies that the two series have similar (dissimilar) topological features. ```
Input: Set of time series \(\{\chi_{t}^{(n)},t=1,\ldots,T,\ n=1,2,\ldots,N\}\); \(N\) is the number of time series, each of length \(T\). for\(n=1\) to \(N\)do  Compute \(l^{(n)}(\omega_{j}),j=1,\ldots,T/2\) by using the R function spec.pgram() with a modified Daniel kernel. Compute the persistence diagram \(\tilde{\Omega}^{(n)}=\{(\lambda_{k,1}^{(n)},\lambda_{k,2}^{(n)}),k=1,2,\ldots, \tilde{K}\}\) from \(l^{(n)}(\omega_{j})\) by using the R function gridDiag() in \(R\)-\(\mathit{TDA}\) package. endfor  Compute \(M_{1}=\min_{n,k}\lambda_{k,1}^{(n)}\) and \(M_{2}=\max_{n,k}\lambda_{k,2}^{(n)}\). Initialize PL order \(\
u=1,\quad\texttt{flag}=\textbf{true},\
u^{\mathsf{opt}}=1\) when \(\
u=1\). while flag do for\(n=1\) to \(N\)do  Compute the \(v\)-th order persistence landscapes \(\texttt{PL}_{\
u}^{(n)}(\ell),\ \ell=1,\ldots,L\). endfor  Compute \(S_{\
u}=\sum_{\ell=M_{1}}^{M_{2}}\sum_{n=1}^{N}\texttt{PL}_{\
u}^{(n)}(\ell)\). if\(\
u>1\)then  Fit linear function \(y=ax+b\) of \((x,y)\) on points \((1,S_{1})\) and \((\
u,S_{\
u})\), so that \(\hat{a}=\frac{S_{1}-S_{1}}{\
u-1},\hat{b}=S_{1}-\hat{a}\). for\(\
u^{+}=1\) to \(\
u\)do  Compute \(D(\
u^{+})=\frac{|h^{+}+\hat{b}-S_{+}|}{\sqrt{2}+1}\) as Euclidean distances of points \((\
u^{+},S_{\
u^{+}})\) to the fitted linear function above. endfor  Calculate \(\
u^{\mathsf{opt}}=\arg\max_{\
u^{+}\leq\
u}D(\
u^{+})\) (let \(\
u^{\mathsf{opt}}=1\) when \(\
u=2\) since \(D(1)=D(2)=0\) when \(\
u=2\)).