Hitesh Sahu
Hitesh SahuHitesh Sahu
  1. Home
  2. ›
  3. posts
  4. ›
  5. …

  6. ›
  7. 3 3 XGBoost

Loading ⏳
Fetching content, this won’t take long…


💡 Did you know?

🍯 Honey never spoils — archaeologists found 3,000-year-old jars still edible.

🍪 This website uses cookies

No personal data is stored on our servers however third party tools Google Analytics cookies to measure traffic and improve your website experience. Learn more

Loading ⏳
Fetching content, this won’t take long…


💡 Did you know?

🍯 Honey never spoils — archaeologists found 3,000-year-old jars still edible.
AI-Machine-Learning

    AI-AgenticAI

    AI-DeepLearning

    AI-GenAI

    AI-Infrastructure

    AI-Machine-Learning
    • Machine Learning Learning Path


    • Stanford AI Scientist Roadmap 2026


    • Machine Learning: Introduction and Core Algorithms


    • Linear Regression Explained: Single Variable and Multivariate Models with Gradient Descent


    • Evaluating a Hypothesis in Neural Networks


    • Bias-Variance Dilemma


    • Cost Function Regularization: Balancing Bias and Variance in Machine Learning Models


    • Polynomial Regression


    • Normal Equation in Linear Regression: Formula, Intuition, and Comparison with Gradient Descent


    • Logistic Regression for Classification: Concept, Sigmoid Function, Cost Function, and Implementation


    • Logistic Regression for Classification: Concept, Sigmoid Function, Cost Function, and Implementation


    • Support Vector Machines (SVM): Maximizing Margins for Robust Machine Learning Models


    • XGBoost (Extreme Gradient Boosting) Explained


    • Dimensionality Reduction in Machine Learning


    • Principal Component Analysis (PCA) Explained


    • t-SNE (t-distributed Stochastic Neighbor Embedding) Explained


    • K-Means Clustering


    • Anomaly Detection: Identifying Rare and Unusual Patterns in Data


    • Anomaly Detection Using Gaussian Distribution in Machine Learning


    • Anomaly Detection Using Multivariate Gaussian Distribution


    • Recommender Systems: Collaborative Filtering, Content-Based Filtering, and Hybrid Approaches


    • Collaborative Filtering: Building Recommender Systems with Feature Learning


    • Photo OCR: Sliding Window Detection, Character Segmentation and Recognition


    • Large Scale Machine Learning: Training Models on Massive Datasets


    • Stochastic Gradient Descent (SGD): Efficient Optimization for Large Datasets


    • MapReduce for Large-Scale Machine Learning: Distributed Training at Scale


    • AI-Machine-Learning Index


    AI-Math

    AWS

    Azure

    kubernetes

    Management

    Programming

    Terraform

    Z_Appendix

Cover Image for XGBoost (Extreme Gradient Boosting) Explained
AI-Machine-Learning

XGBoost (Extreme Gradient Boosting) Explained

Learn how XGBoost works, including gradient boosting, decision trees, residual learning, regularization, and why XGBoost is one of the most powerful machine learning algorithms for structured and tabular data.

AI
Machine Learning
XGBoost
Gradient Boosting
Decision Trees
Ensemble Learning
← Previous

Pinned Memory (Page-Locked Memory) in CUDA and GPU Computing

Next →

t-SNE (t-distributed Stochastic Neighbor Embedding) Explained

XGBoost (Extreme Gradient Boosting)

XGBoost is an optimized gradient boosting algorithm that combines multiple decision trees sequentially to build highly accurate predictive models.

XGBoost is a highly optimized machine learning algorithm based on:

  • Gradient Boosting
  • Decision Trees

It is widely used for:

  • structured/tabular data
  • classification
  • regression
  • ranking problems

XGBoost became extremely popular because of:

  • high accuracy
  • speed
  • scalability
  • strong Kaggle competition performance

Sample Code

import xgboost as xgb

# read in data
dtrain = xgb.DMatrix('demo/data/agaricus.txt.train')
dtest = xgb.DMatrix('demo/data/agaricus.txt.test')

# specify parameters via map
param = {
         'max_depth':2, 
         'eta':1, 
         'objective':'binary:logistic' 
         }
num_round = 2
bst = xgb.train(param, dtrain, num_round)

# make prediction
preds = bst.predict(dtest)

Core Idea

XGBoost builds multiple decision trees sequentially.

Each new tree learns:

  • errors
  • residuals
  • mistakes

from previous trees.

High-Level Workflow

flowchart TD

    A[Training Data]

    A --> B[Tree 1]

    B --> C[Prediction Error]

    C --> D[Tree 2 Learns Residuals]

    D --> E[Updated Prediction]

    E --> F[More Trees Added]

    F --> G[Final Strong Model]

Why "Boosting"?

Boosting means:

  • combining many weak learners
  • into one strong learner

Weak learner:

  • slightly better than random

Strong learner:

  • highly accurate predictor

Ensemble Learning

XGBoost is an:

  • Ensemble Learning algorithm

It combines many decision trees.

flowchart LR

    A[Tree 1]
    B[Tree 2]
    C[Tree 3]
    D[Tree N]

    A --> E[Combined Prediction]
    B --> E
    C --> E
    D --> E

Gradient Boosting Concept

Each new tree minimizes the loss function using gradients.

Fm(x)=Fm−1(x)+hm(x)F_m(x) = F_{m-1}(x)+ h_m(x)Fm​(x)=Fm−1​(x)+hm​(x)

Where:

  • Fm(x)F_m(x)Fm​(x) = updated model
  • hm(x)h_m(x)hm​(x) = new tree correcting errors

Training Process

Step 1

Train first decision tree.

Step 2

Compute prediction errors.

Residual=y−y^\text{Residual} = y - \hat{y}Residual=y−y^​

Step 3

Train next tree on residuals.

Step 4

Add new tree predictions to existing model.

Step 5

Repeat iteratively.

Example Flow

sequenceDiagram

    participant D as Dataset
    participant T1 as Tree 1
    participant T2 as Tree 2
    participant T3 as Tree 3

    D->>T1: Initial Training

    T1->>T2: Residual Errors

    T2->>T3: Remaining Errors

    T3-->>D: Final Prediction

Objective Function

XGBoost minimizes:

L=∑il(yi,y^i)+∑kΩ(fk)\mathcal{L} = \sum_i l(y_i, \hat{y}_i)+ \sum_k \Omega(f_k)L=i∑​l(yi​,y^​i​)+k∑​Ω(fk​)

Where:

  • lll = loss function
  • Ω\OmegaΩ = regularization term
  • fkf_kfk​ = decision trees

Regularization

XGBoost includes regularization to reduce overfitting.

Ω(f)=γT+12λ∣∣w∣∣2\Omega(f) = \gamma T + \frac{1}{2}\lambda ||w||^2Ω(f)=γT+21​λ∣∣w∣∣2

Where:

  • TTT = number of leaves
  • www = leaf weights
  • γ,λ\gamma, \lambdaγ,λ = regularization parameters

Why XGBoost is Powerful

FeatureBenefit
Gradient boostingHigh accuracy
RegularizationPrevents overfitting
Parallel processingFaster training
Tree pruningBetter optimization
Missing value handlingRobust training
Sparse optimizationEfficient memory usage

Important Hyperparameters

ParameterPurpose
n_estimatorsNumber of trees
max_depthTree depth
learning_rateStep size
subsampleRow sampling
colsample_bytreeFeature sampling
gammaSplit regularization
lambdaL2 regularization

Learning Rate

Controls contribution of each tree.

Fm(x)=Fm−1(x)+ηhm(x)F_m(x) = F_{m-1}(x) + \eta h_m(x)Fm​(x)=Fm−1​(x)+ηhm​(x)

Where:

  • η\etaη = learning rate

Small learning rate:

  • slower learning
  • better generalization

Decision Tree Structure

flowchart TD

    A[Feature Split]

    A -->|Condition True| B[Left Branch]

    A -->|Condition False| C[Right Branch]

    B --> D[Prediction]

    C --> E[Prediction]

XGBoost Pipeline

flowchart TD

    A[Raw Data]

    A --> B[Feature Engineering]

    B --> C[Train/Test Split]

    C --> D[XGBoost Training]

    D --> E[Model Evaluation]

    E --> F[Predictions]

Limitations

LimitationDescription
Can overfitEspecially deep trees
Large modelsMemory intensive
Less effective for images/textDeep learning better
Hyperparameter tuning neededMany parameters

XGBoost vs Random Forest

XGBoostRandom Forest
Sequential treesParallel trees
BoostingBagging
Learns residualsIndependent trees
Higher accuracySimpler
More tuning requiredEasier to use

XGBoost vs Neural Networks

XGBoostNeural Networks
Excellent for tabular dataExcellent for unstructured data
Faster trainingSlower training
Less data requiredLarge data preferred
More interpretableLess interpretable

Applications of XGBoost

Common Use Cases

XGBoost is often the best choice when:

  • dataset is tabular
  • features are structured
  • dataset size is moderate
  • interpretability matters

Example Use Cases

  • Fraud Detection
  • Credit Scoring
  • Recommendation Systems
  • Customer Churn
  • Sales Forecasting
  • Medical Prediction
  • Kaggle Competitions

Advantages

AdvantageDescription
High accuracyExcellent predictive power
Handles tabular data wellIndustry standard
Fast trainingOptimized implementation
Robust to missing valuesAutomatic handling
Feature importance supportInterpretability


Related Posts

  • Selecting the Right AI Model — XGBoost vs neural networks is one of the most common model selection decisions; this post covers when structured data favors tree-based models
  • Large-Scale Machine Learning — gradient boosting with large datasets uses similar mini-batch and parallelism ideas covered in large-scale ML
  • Support Vector Machines (SVM) — the margin-based classifier XGBoost is commonly benchmarked against
  • Dimensionality Reduction in Machine Learning — shifting from supervised classifiers to unsupervised feature reduction
Hitesh Sahu
Written by Hitesh Sahu, a passionate developer and blogger.

Tue May 26 2026

Share This on

← Previous

Pinned Memory (Page-Locked Memory) in CUDA and GPU Computing

Next →

t-SNE (t-distributed Stochastic Neighbor Embedding) Explained

AI-Machine-Learning/3-3-XGBoost
Let's work together
hiteshkrsahu@gmail.com
Munich 🥨, Germany 🇩🇪, EU
Playstore
Hitesh Sahu's apps on Google Play Store
Need Help?
Let's Connect
Navigation
  Home/About
  Skills
  Work/Projects
  Lab/Experiments
  Contribution
  Awards
  Art/Sketches
  Thoughts
  Contact
Links
  Sitemap
  Legal Notice
  Privacy Policy

Made with

NextJS logo

NextJS by

hitesh Sahu

| © 2026 All rights reserved.