XGBoost (Extreme Gradient Boosting) is a popular and powerful machine learning algorithm for classification and regression tasks. It's an ensemble learning technique that belongs to the gradient-boosting family of algorithms. XGBoost is known for its efficiency, scalability, and effectiveness in handling various machine-learning problems.

Key features of XGBoost

  1. Gradient Boosting: XGBoost is a variant of gradient boosting with shallow decision trees as base models. These trees are created in a greedy manner by recursively partitioning the data based on the feature that leads to the best split.
  2. Regularization: XGBoost incorporates regularization techniques to prevent overfitting. It includes terms in the objective function that penalize complex models, which helps in better generalization.
  3. Objective Function: XGBoost optimizes an objective function that combines the loss function (e.g., mean squared error for regression, log loss for classification) and regularization terms. The algorithm seeks to find the best model that minimizes this objective function.
  4. Parallel and Distributed Computing: XGBoost is designed to be efficient and scalable. It utilizes parallel and distributed computing techniques to speed up the training process, making it suitable for large datasets.

XGBoost's effectiveness lies in its ability to produce accurate predictions while managing issues like overfitting and underfitting. It has gained popularity in various machine-learning competitions and real-world applications due to its strong predictive performance and versatility.


Firstly, we have to admit that XGBoost has no realization in the sklearn library, so we have to install xgboost manually using the following command in the console of your interpreter:
pip install xgboost.
After the installation is finished, we can use XGBoost to solve the tasks.

What is DMatrix?

Before we start working with the XGBoost ensemble model, we must get familiar with a specific data structure - DMatrix.
In XGBoost, DMatrix is a data structure that is optimized for efficiency and used to store the dataset during training and prediction. It's a core concept in the xgboost library and is designed to handle large datasets memory-efficient and fast. DMatrix serves as an input container for the training and testing data.

DMatrix example

XGBoost usage example

Code Description
  • Create DMatrix objects:
  • DMatrix objects dtrain and dtest are created using xgb.DMatrix(), which is an efficient data structure for XGBoost. They store the training and testing data along with labels.
  • Set hyperparameters:
  • A params dictionary is defined to set hyperparameters for the XGBoost classifier:
    - 'objective': 'multi:softmax' indicates that the objective is a cross-entropy loss function, and predictions are created using the softmax function.
    - 'num_class': 3 specifies that there are three classes in the dataset.
  • Train the XGBoost classifier:
  • The XGBoost classifier is trained using xgb.train() with the params dictionary and dtrain as input.
  • Make predictions:
  • Predictions are made using the trained model on the testing data using model.predict().
    You can find the official documentation with all the necessary information about implementing this model in Python on the official website. Go here if needed.


    What model is better to use if you want to avoid overfitting?

    Select the correct answer

    Everything was clear?

    Section 3. Chapter 5