From Decision Trees to XGBoost: A Practical Comparison on Breast Cancer Dataset

🧠 Introduction
In machine learning, choosing the right model can significantly impact performance. While simple models like Decision Trees are easy to interpret, advanced ensemble techniques like Random Forest and XGBoost often deliver better accuracy.
In this blog, I experimented with:
Decision Tree 🌳
Random Forest 🌲🌲
XGBoost ⚡
using the Breast Cancer dataset from sklearn.
📊 Dataset Overview
I used the built-in dataset:
Total samples: 569
Features: 30 numerical features
Target:
0 = Malignant1 = Benign
from sklearn.datasets import load_breast_cancer
After loading:
Converted into Pandas DataFrame
Split into:
Training set
Validation set
Test set
🔀 Data Splitting Strategy
Instead of a simple split, I used a 3-way split:
Train → Model learning
Validation → Hyperparameter tuning
Test → Final evaluation
Train: ~70%
Validation: ~15%
Test: ~15%
This ensures no data leakage and better generalization.
🌳 Step 1: Decision Tree (Baseline Model)
First, I trained a Decision Tree without depth restriction.
📌 Observation
Very high training accuracy
Lower validation accuracy
Large gap → Overfitting
Train Accuracy: ~1.00
Validation Accuracy: ~0.92
Gap: High → Overfitting
Depth: Large
👉 This shows that Decision Trees can memorize data.
📉 Step 2: Controlling Overfitting (Max Depth)
I experimented with tree depths from 1 to 20.
📈 Key Insight
Small depth → underfitting
Large depth → overfitting
Optimal depth → balance point
The graph clearly shows:
- Validation accuracy peaks at moderate depth
🌲 Step 3: Random Forest (Bagging)
Next, I used:
RandomForestClassifier(n_estimators=100)
💡 Why Random Forest?
Combines multiple trees
Reduces variance
Improves generalization
📊 Result
Random Forest Val Accuracy > Decision Tree
Improvement observed
👉 Ensemble learning significantly reduces overfitting.
🌳🌳 Step 4: Number of Trees Impact
I tested:
1 → 200 trees
📌 Observation
Accuracy improves as trees increase
Stabilizes after certain point
Diminishing returns after ~100 trees
⚡ Step 5: XGBoost (Boosting)
Then I trained:
xgb.XGBClassifier(
n_estimators=100,
learning_rate=0.1,
max_depth=6
)
💡 Why XGBoost?
Boosting technique
Learns from previous errors
Often best for structured data
📊 Result
Validation Accuracy: Highest
Test Accuracy: Strong performance
👉 XGBoost outperformed both Decision Tree and Random Forest.
⏱ Step 6: Early Stopping (Advanced Optimization)
To avoid overfitting:
early_stopping_rounds=20
📌 Benefits
Stops training when no improvement
Saves computation
Prevents overfitting
Best iteration found automatically
Improved validation accuracy
🔍 Feature Importance Analysis
Random Forest Top Features:
Mean Radius
Mean Texture
Mean Perimeter
XGBoost Top Features:
Similar key medical indicators
Slight variation in ranking
👉 Helps in model interpretability and domain understanding.
🏆 Final Model Comparison
| Model | Validation Accuracy | Test Accuracy |
|---|---|---|
| Decision Tree | Lower | Lower |
| Random Forest | Better | Better |
| XGBoost | Best | Best |
🎯 Key Learnings
✅ 1. Decision Trees Overfit Easily
Without constraints, they memorize training data.
✅ 2. Random Forest Reduces Variance
Bagging improves stability and accuracy.
✅ 3. XGBoost is Powerful
Boosting captures complex patterns effectively.
✅ 4. Hyperparameter Tuning Matters
Depth, trees, learning rate → huge impact.
✅ 5. Early Stopping is Crucial
Prevents unnecessary training and overfitting.
🧪 Conclusion
This experiment clearly shows the evolution:
Decision Tree → Random Forest → XGBoost
(Simple → Ensemble → Advanced Boosting)
If you're working with structured/tabular data:
👉 Start with Random Forest 👉 Move to XGBoost for best performance
🔗 Bonus: Where This Helps
This approach is useful in:
Healthcare predictions 🏥
Fraud detection 💳
Customer churn 📉
Recommendation systems 🛒
About the author
- I am an Information Technology Analyst at NTT DATA North America, where I specialize in DevOps, automation testing, and cloud-native deployments. Over the past 3 years, I’ve built CI/CD pipelines, test automation frameworks, and Generative AI solutions that help organizations streamline their workflows and improve efficiency. My Background - I studied at the Indian Institute of Technology, Patna. - Since July 2022, I’ve been contributing to projects at NTT DATA North America. My Skills - Automation Testing: REST API and UI automation using Java, Selenium, REST Assured, TestNG. - DevOps Tools: GitHub Actions, Jenkins, Docker, Terraform, Kubernetes, Azure DevOps, AWS. - Programming: Proficient in Python and Java. - Cloud & Microservices: Deploying Python-based applications on AWS EC2, integrating with MySQL and OpenAI APIs. - Monitoring & Performance: Experienced with Datadog, Grafana, JMeter, Gatling. My Achievements - Improved test efficiency by 40% through automation frameworks. - Led Generative AI initiatives, including prompt engineering, LangChain workflows, and retrieval-augmented generation (RAG) systems. - Enhanced application performance by 10% through API call caching and load testing. My Professional Presence I am active on LinkedIn, where I have over 4,000 followers and 500+ connections
Comments
Join the discussionNo comments yet. Be the first to comment.
