# Road Event Detection and Classification Algorithm Using Vibration and Acceleration Data
## 3 The Proposed Algorithm
### Model Training  
Once the relevant features are extracted from the preprocessed data, the next step is to train the Random Forest model. This step is critical to the overall success of the classification process as it enables the creation of a model capable of accurately predicting the type of event based on the extracted features. Random Forest is an ensemble learning method that combines multiple decision trees to improve the robustness and accuracy of predictions.  
The key strength of Random Forest lies in its ability to reduce overfitting and enhance generalization by leveraging the power of multiple decision trees, each trained on random subsets of the data. By aggregating the predictions of all trees in the forest, Random Forest can produce more reliable and stable results compared to a single decision tree. The final classification is determined by majority voting across all trees in the forest, allowing better handling of outliers and noise in the data.  
The training process can be broken down into several stages, each of which plays a crucial role in building the Random Forest model.  
* Tree Construction: Each decision tree in the Random Forest is trained on a random subset of the training data, with replacement. This is known as bootstrap sampling, and it ensures that each tree learns from a different subset of the data. This randomization reduces overfitting and increases the model's ability to generalize to unseen data.
* Splitting Criteria: During the construction of each decision tree, the data are split at each node according to a criterion that maximizes the gain in information. Popular splitting criteria include:Gini Impurity: Measures the degree of impurity in the dataset at each node. The lower the Gini index, the purer the node.
* Entropy: Measures the amount of uncertainty in the dataset. A lower entropy value indicates that the node is more homogeneous. The choice of criterion for splitting influences the way the tree learns from the data and helps minimize misclassification.
* Majority Voting: Once all trees are trained, new observations are classified by passing them through each decision tree. Each tree makes a prediction based on the characteristic vector of the new observation. The final classification is determined by the majority vote of all trees in the forest, which ensures that the predictions are robust and not biased by individual tree errors.  
The Random Forest algorithm can be formalized as follows:  
\[\hat{y}=\text{Majority Voting}(T_{1}(X),T_{2}(X),\dots,T_{N}(X)) \tag{5}\]  
where  
* \(\hat{y}\) is the predicted event label.
* \(T_{j}(X)\) is the prediction made by the \(j\)th decision tree for the feature vector \(X\).
* \(N\) is the total number of trees in the forest.  
The Random Forest model is built by aggregating the predictions from multiple trees, where each tree is trained on a different subset of the data. This ensemble approach leads to a more accurate and stable model that is less prone to overfitting.  
The steps for training a Random Forest classifier using the extracted features are detailed in Algorithm 3, which includes data partitioning, class balancing, and the construction of a Random Forest model with weighted samples to effectively handle unbalanced datasets.  
```
1:Input: Feature vectors \(F_{k}\), corresponding event labels \(L_{k}\), number of trees \(N_{T}\)
2:Output: Trained Random Forest model \(\mathcal{M}\)
3:Initialize an empty set for the Random Forest model: \(\mathcal{M}=\emptyset\)
4:for each tree \(T_{j}\) for \(j=1,2,\dots,N_{T}\)do
5: Randomly sample a subset \(D_{j}\) from the training dataset \(\{F_{k},L_{k}\}\)
6: Use bootstrap sampling (with replacement) to form the subset \(D_{j}\)
7: Build a decision tree \(T_{j}\) using the subset \(D_{j}\)
8: Choose the splitting criterion (e.g., Gini impurity or entropy)
9: Train the tree \(T_{j}\) on the subset \(D_{j}\)
10: Add the trained tree \(T_{j}\) to the forest \(\mathcal{M}\)
11:endfor
12:return\(\mathcal{M}\)
```  
**Algorithm 3**Random Forest Model Training