Hello! Let's dive into the next step of our journey: training a powerful predictive model.
Introduction
In our last lesson, we tackled the critical issue of class imbalance. You learned how to use SMOTE to create a more balanced training dataset and, most importantly, how to build an imblearn pipeline to ensure this resampling is done correctly without leaking data into your test set. This preparation is the foundation for everything we will do today.
Our learning outcome is to train a binary classifier (e.g., RandomForest, XGBoost) to detect flaky tests from engineered features. We will take the pipeline you learned to build and plug in two of the most effective and widely used classification algorithms in machine learning. By the end of this lesson, you will understand the principles behind these models and have a complete, working code example that trains and compares them on a flaky test detection task.
This step is the heart of the modeling process. It's where the features we engineered and the data we balanced are finally used to teach a model to make intelligent predictions.

Ensemble Methods: The Wisdom of Crowds
Both RandomForest and XGBoost are ensemble methods. The core idea is simple yet powerful: instead of relying on a single complex model, we can combine the predictions of several simpler models to arrive at a more robust and accurate final decision.
There are two main families of ensemble methods:
- Bagging (Bootstrap Aggregating): Trains many models in parallel on different random subsets of the training data. The final prediction is made by averaging or taking a majority vote. This helps reduce variance and makes the model less prone to overfitting. Random Forest is the most famous example.
- Boosting: Trains models sequentially. Each new model focuses on correcting the mistakes made by the previous ones. This typically leads to very high performance but can be more sensitive to noisy data. XGBoost is a leading example of a boosting algorithm.

Let's explore each of these in the context of our flaky test detection problem.
RandomForest: Robust and Reliable
A Random Forest classifier builds a large number of individual decision trees during training. To make a prediction, each tree "votes," and the class that receives the most votes becomes the model's prediction.
The "randomness" comes from two sources:
- Each tree is trained on a different random sample of the training data (this is the "Bootstrap Aggregating" or Bagging part).
- At each split in a tree, only a random subset of features is considered.
This process creates a diverse "forest" of trees, and their collective decision is much more reliable than that of any single tree.
Key Advantages:
- Excellent performance on many types of problems with minimal tuning.
- Highly resistant to overfitting.
- Can provide "feature importances," which help you understand which features were most influential in the model's decisions.
The effectiveness of tree-based and boosting classifiers for flaky test detection is well-documented. A comprehensive study on this topic provides a great overview of these models.
[PDF] EVALUATION OF MACHINE LEARNING MODELS IN PREDICTING ...
This Master's Thesis evaluates multiple ML models for predicting software flakiness. The sections below introduce the specific classifiers we're focusing on today, providing academic context for their use in this domain.
Please read sections 3.3.2. Tree-Based Classifiers and 3.3.3. Gradient Boosting Classifiers. These sections give a concise, high-level description of RandomForestClassifier and XGBClassifier, which are the exact models we're implementing.
XGBoost: Optimized for Performance
XGBoost (eXtreme Gradient Boosting) is an advanced and highly optimized implementation of the gradient boosting algorithm. Like other boosting methods, it builds models sequentially.
Here's an intuitive way to think about it:
- It starts by training a simple, weak decision tree.
- It looks at the errors (or "residuals") this tree made.
- It then trains a second tree specifically to predict those errors.
- It continues this process, with each new tree adding a "correction" to the ensemble, gradually improving the overall prediction.
XGBoost includes several innovations like built-in regularization (to prevent overfitting), parallel processing capabilities, and efficient handling of missing data, making it a favorite in machine learning competitions and industrial applications.
Key Advantages:
- Often delivers state-of-the-art performance.
- Extremely fast and memory-efficient.
- Provides advanced features like regularization and cross-validation capabilities.
The thesis you just reviewed highlights the exceptional performance of XGBoost. In its comprehensive comparison, XGBoost emerged as the top-performing model.
[PDF] EVALUATION OF MACHINE LEARNING MODELS IN PREDICTING ...
Let's look at the results from that same study. This will give you a clear idea of how these models stack up against each other in a real-world flaky test prediction task.
Please review Table 7 in section 4.2.2. Tree-Based Classifiers and Table 8 in section 4.2.3. Gradient Boosting Classifiers. Then, read the first paragraph of the Discussion (Chapter 5). Notice how XGBoost consistently achieves top scores in accuracy, precision, and recall, making it the best overall performer in that analysis.
It's worth noting that while XGBoost often wins, the best model can be dataset-dependent. Another study, "The Vocabulary of Flaky Tests in the Context of SAP HANA" (LINK), found that RandomForest performed slightly better on their specific datasets. This is why it's good practice to test a few strong candidates, which is exactly what we'll do next.
Practical Implementation: Training and Comparing Classifiers
Now, let's put everything together. We will use the imblearn.pipeline.Pipeline from the last lesson and train two separate models: one with RandomForestClassifier and another with XGBClassifier. This will allow us to directly compare their performance on the same data.
You'll need to install the xgboost library if you haven't already:pip install xgboost
The code below performs the full workflow:
- Creates a synthetic imbalanced dataset for demonstration.
- Splits the data into training and testing sets.
- Defines two pipelines, each containing a SMOTE step and a classifier.
- Fits both pipelines to the training data.
- Evaluates both on the unseen test data and prints their classification reports.
import numpy as np
from collections import Counter
# Scikit-learn and XGBoost imports
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.metrics import classification_report
# Imbalanced-learn import
from imblearn.pipeline import Pipeline
from imblearn.over_sampling import SMOTE
# 1. Create a synthetic imbalanced dataset (same as last lesson)
X, y = make_classification(n_samples=10000, n_features=20, n_informative=10,
n_redundant=5, n_classes=2, n_clusters_per_class=2,
weights=[0.98, 0.02], flip_y=0, random_state=42)
print(f"Original dataset shape: {Counter(y)}")
# 2. Split data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42, stratify=y)
print(f"Training set shape: {Counter(y_train)}")
print(f"Test set shape: {Counter(y_test)}\n")
# 3. Define the RandomForest pipeline with SMOTE
rf_pipeline = Pipeline(steps=[
('smote', SMOTE(random_state=42)),
('classifier', RandomForestClassifier(random_state=42, n_jobs=-1))
])
# 4. Define the XGBoost pipeline with SMOTE
# Note: XGBoost has its own ways of handling imbalance (e.g., scale_pos_weight),
# but using it in a SMOTE pipeline is a perfectly valid and common approach.
xgb_pipeline = Pipeline(steps=[
('smote', SMOTE(random_state=42)),
('classifier', XGBClassifier(random_state=42, use_label_encoder=False, eval_metric='logloss', n_jobs=-1))
])
# 5. Train the RandomForest pipeline
print("Training RandomForest model...")
rf_pipeline.fit(X_train, y_train)
# 6. Train the XGBoost pipeline
print("Training XGBoost model...")
xgb_pipeline.fit(X_train, y_train)
# 7. Evaluate both models on the test set
print("\n--- Evaluation Results ---\n")
# RandomForest results
y_pred_rf = rf_pipeline.predict(X_test)
print("Classification Report: RandomForest")
print(classification_report(y_test, y_pred_rf, target_names=['Stable Test (0)', 'Flaky Test (1)']))
# XGBoost results
y_pred_xgb = xgb_pipeline.predict(X_test)
print("\nClassification Report: XGBoost")
print(classification_report(y_test, y_pred_xgb, target_names=['Stable Test (0)', 'Flaky Test (1)']))
When you run this code, pay close attention to the recall and F1-score for the 'Flaky Test (1)' class in both reports. You will likely see that both models perform very well, thanks to the combination of powerful algorithms and proper handling of class imbalance. The slight differences between their scores demonstrate the value of experimenting with more than one model.
Conclusion
Congratulations! You have successfully trained and evaluated two industry-standard binary classifiers for the task of flaky test detection. This is a huge step toward building a practical tool.
Key Takeaways:
- Ensemble methods, like Bagging (RandomForest) and Boosting (XGBoost), combine multiple models to achieve superior performance and robustness.
- RandomForest is a reliable, all-around performer that is resistant to overfitting and easy to use.
- XGBoost is a highly optimized boosting algorithm that often achieves state-of-the-art results with great speed.
- Both classifiers can be seamlessly integrated into an
imblearn.pipelineto ensure that resampling techniques like SMOTE are applied correctly to the training data only. - Comparing multiple strong models is a best practice to find the optimal one for your specific problem and dataset.
Preview of the Next Lesson:
We've now covered a complete classification workflow. In the next lesson, we will switch gears to a different type of machine learning problem. You will learn how to train a learning-to-rank model to prioritize technical debt items by estimated impact. This will introduce you to a new class of models and prepare you for another one of the project ideas you were interested in exploring.