# Time Series Classification with Shapelet and Canonical Features
## 3 Definition and Background
### Ensemble Classification Model Building  
Algorithm 3 describes the process of building an RSFCF ensemble classification model. Algorithm 2 is first invoked to transform the training dataset (line 3), and then the transformed dataset is used to train the decision tree classifier (line 4). The Shapelets and features used in the dataset transformation are saved together in the corresponding decision tree (lines 5-7) for converting the test dataset when classifying. The above process will be repeated \(r\) times to build a forest containing \(r\) trees.  
The construction of the time series tree follows the recursive strategy of the standard decision tree from top to bottom, which takes the information gain as the criterion, divides the instances of the current node with the best splitting threshold of the best splitting attribute at each node, constructing two subnodes on the left and right, and recursively carries out the process until all instances of the node belong to the same category. Regarding the calculation of the optimal split threshold for numeric attributes, the time series tree adopts a more efficient method, it divides the interval composed of the minimum and maximum values of the attribute into \(\kappa\) equal parts (here we set \(\kappa\) to a fixed value of 20), and the boundary between each cell is tested one by one as the candidate split threshold, and the threshold for obtaining the maximum information gain is the optimal split threshold for the attribute. In addition, for the split threshold to achieve the same information gain, the time series tree uses the method of maximizing the decision boundary to break the draw (to calculate a reasonable boundary value, the various attributes of the dataset need to be Z-score standardized before training the time series tree to avoid the algorithm's preference for attributes of a larger magnitude).  
\[Margin^{\tau}_{j}=\min_{i=1,2,\cdots,n}|Att^{j}_{i}-\tau|, \tag{4}\]  
Equation (4) shows how the decision boundary is calculated for the split threshold \(\tau\) of the \(j\)th attribute, where \(Att^{j}_{i}\) is the value of the \(i\)th instance on the \(j\)th attribute.  
```
Input: Dataset \(\mathbf{D}\), Shapelet minimum length \(min\), Shapelet maximum length \(max\), number of Shapelets \(k\), number of typical time series features a, maximum offset shift, number of decision trees \(r\) Output: RSFCF classification model
1.RSFCFModel\(\leftarrow\)[\(\textit{Tree}_{1}\),\(\textit{Tree}_{2}\),\(\cdots\),\(\textit{Tree}_{r}\)];
2.for\(i\)\(\leftarrow\)\(1\) to \(r\)
3.Shapelets, Locations, Atts, \(\mathbf{D}^{\prime}\)\(\leftarrow\)randShapelet Transform-CF(\(\mathbf{D}\),\(min\),\(max\),\(k\),\(a\),\(\textit{s}\)hift);
4.Tree\({}_{i}\)\(\leftarrow\)buildTimeSeriesTree(\(\mathbf{D}^{\prime}\));
5.Tree;add(Shapelets);
6.Tree;add(Locations);
7.Tree;add(Atts);
8.end for
9. return RSFCFModel;
```  
**Algorithm 3**_buildRSFCF(\(\mathbf{D}\),\(min\),\(max\),\(k\),\(a\),\(shift\),\(r\))_  
Algorithm 4 describes the classification process of the RSFCF ensemble classification model. When classifying, RSFCF aggregates the classification results of all time series trees and gives the final classification by majority vote. It should be noted that before classifying using a time series tree, it is first necessary to transform the time series to be classified using the Shapelets stored in the tree and the attribute indices according to the method described in lines 6 to 13 of Algorithm 2.