# An Interpretable Deep Learning Approach for Detecting Marine Heatwaves Patterns
## 2 Methods
### Clustering of Marine Heatwaves by Feature Importance  
The feature importance analysis described in the previous section was applied to pre-identified marine heatwave events. Training the model at each station would result in an equal number of \(\phi_{i}^{EG}\) sequences, each containing two vectors representing the temporal feature importance scores of mean sea-level pressure and 10 m wind speed. We trained on seven different split datasets, obtaining seven feature importance sequences for every heatwave occurrence. The sequences for each variable were averaged into a single sequence to mitigate the effect of the randomness introduced during LSTM training. The average sequences corresponding to each peak flow were normalized within the \([0,1]\) range and further clustered into several groups with similar patterns.  
The experiments made use of the K-Medoids clustering algorithm and Dynamic Time Warping Barycenter Averaging (DBA), as proposed by [PERSON] et al. (2017). KMedoids appeared superior visually in the clustering of centers compared to KMeans, but the silhouette coefficient was less so. However, in the case of just Euclidean distance, the silhouette coefficient of KMedoids was slightly better than that of KMeans.  
DBA is a method of centroid averaging based on Dynamic Time Warping (DTW) ([PERSON] et al., 2017), designed to iteratively optimize an initial sequence so that its squared DTW distance to other sequences is minimized. Because the experiment requires comparing not a single time series but a collection, the time series are compressed to make the comparison. DTW is a distance measurement method that can perform similarity detection at different temporal scales. The key process is stretching and shrinking one time series along the time axis, essentially distorting one series to align with the other in temporal scale, before calculating the desired path between the two series to achieve similarities discrimination at different temporal scales.  
Consequently, the computation of the Dynamic Time Warping (DTW) distance between two temporal sequences, \([\varphi_{1}^{(1)},\varphi_{1}^{(2)},\ldots,\varphi_{1}^{(n)}]\) and \([\varphi_{2}^{(1)},\varphi_{2}^{(2)},\ldots,\varphi_{2}^{(m)}]\), is characterized as an optimization problem which can be formulated as:  
\[\text{DTW}=\min_{\pi}\sqrt{\sum_{(i,j)\in\pi}\left(\varphi_{1}^{(i)}-\varphi_{ 2}^{(j)}\right)^{2}} \tag{5}\]  
Here, the path \(\pi=[\pi_{0},\pi_{1},\ldots,\pi_{K}]\) entails a sequence of index tuples \(\pi_{k}=(i_{k},j_{k})\) that adhere to the constraints \(0\leq i_{k}\leq n\) and \(0\leq j_{k}\leq m\), in which the notation \(\varphi^{(i)}\) signifies the observational value at the specific time index \(i\).  
The experiment computes a normalization sequence for the trained \(\phi_{i}^{EC}\). The number of clusters for all obtained feature sequences is then determined using the elbow method, as reviewed in [39]. The formula for the elbow method can be represented as follows:  
\[D_{k}=\sum_{i=1}^{K}\sum dist(x,c_{i})^{2} \tag{6}\]  
where, \(K\) is the number of clusters, and \(dist(\cdot)\) is the Euclidean distance between each data point and the center of the cluster. These feature sequences are measured with DBA (Dynamic Time Warping Barycenter Averaging), and the K-Medoids algorithm is applied to group these sequences into clusters by minimizing the square sum of the DTW (Dynamic Time Warping) distance between the centroid in the class and all sequences in the class. The optimal number of clusters is determined by evaluating the silhouette scores of different numbers of clusters, where a higher value generally indicates a better choice of the number of clusters [40]. Each cluster contains feature importance sequences with similar patterns, which may further be associated with specific marine heatwave generation mechanisms.