Xgboost Python Tutorial – Complete Guide

Welcome to this all-in-one tutorial about XGBoost in Python! If you’ve been searching for the perfect entry-point into the world of machine learning and data science, then this is indeed the perfect opportunity for you. This comprehensive tutorial is designed to illuminate all learners, irrespective of your mastery of Python.

What is XGBoost?

XGBoost, which stands for “Extreme Gradient Boosting”, is a powerful machine learning algorithm widely used in the world of competitive data science. It’s known for its speed and performance.

At its core, XGBoost is a software library providing a gradient boosting framework. It is principally used for modelling sophisticated machine learning applications, predicting outcomes and interpreting complex datasets.

Why should you learn XGBoost?

Learning XGBoost is akin to adding a potent tool in your data science toolbox. Moreover:

  • It is a popular choice for winning solutions in data science competitions.
  • XGBoost is efficient, flexible and portable across multiple platforms.
  • It can handle a variety of data types and offers great interpretability.

Now that we’ve piqued your interest, let’s make a deep dive into the magical world of XGBoost! Just like any enchanted journey, we’ll go step by step, gradually unraveling the layers of this potent tool. Happy learning.

CTA Small Image

Part 2: Using XGBoost for a Simple Regression Problem

Let’s illustrate how XGBoost works by dwelling on a straightforward regression problem.

Firstly, we’ll import our necessary libraries:

import xgboost as xgb
import numpy as np

Next, we’ll create our data:

X = np.random.rand(100, 1) 
y = 2 + 5 * X + np.random.randn(100, 1)

We are now ready to create our XGBoost model:

model = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,
                max_depth = 5, alpha = 10, n_estimators = 10)

With our model set up, we can now fit it to our data:

model.fit(X, y)

And finally, we’ll make some predictions:

preds = model.predict(X)

Part 3: Using XGBoost for a Classification Problem

Let’s illustrate the usage of XGBoost in a classification problem. For illustration purposes, we’ll use a very famous dataset, the iris dataset.

First things first, we’ll import our libraries:

import xgboost as xgb
from sklearn import datasets
from sklearn.model_selection import train_test_split

Next, we load our dataset:

iris = datasets.load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

We’re now ready to create our XGBoost model:

model = xgb.XGBClassifier(objective ='multi:softmax', colsample_bytree = 0.3, learning_rate = 0.1,
                max_depth = 5, alpha = 10, n_estimators = 10)

As before, with our model set up, we can now fit it to our data:

model.fit(X_train, y_train)

And finally, we’ll again make some predictions:

preds = model.predict(X_test)

With this, you should have a basic understanding of how XGBoost works and how you can employ it in both regression and classification problems. The power of XGBoost is truly impressive, and with continual practice, we trust you’ll master it in no time. Happy coding!

Part 4: Understanding XGBoost Parameters

An integral part of mastering XGBoost is understanding its parameters. The key parameters in any XGBoost model are:

  • max_depth: Maximum depth of a tree. Increasing this value will make the model more complex and more likely to overfit.
  • min_child_weight: Minimum sum of instance weight (hessian) needed in a child. If the tree partition step results in a leaf node with the sum of instance weight less than min_child_weight, then the building process will give up further partitioning.
  • gamma: Minimum loss reduction required to make a further partition on a leaf node of the tree.
  • subsample: Subsample ratio of the training instances. Setting it to 0.5 means that XGBoost would randomly sample half of the training data prior to growing trees and this will prevent overfitting.
  • colsample_bytree: Subsample ratio of columns when constructing each tree.

Let’s train a model using one of the parameters:

model = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.7, learning_rate = 0.1, 
                max_depth = 5, alpha = 10, n_estimators = 10, min_child_weight=3, gamma=0.4, subsample=0.8)
model.fit(X, y)

As you see, altering these parameters can deeply affect the learning algorithm and consequently, the unbiased quality of your predictions.

Part 5: XGBoost Evaluation Metrics

Another important facet is understanding how to evaluate the performance of your model. For Regression, common evaluation metrics include Mean Squared Error (MSE), Mean Absolute Error (MAE) and R² Score. For Classification, Log Loss and AUC are common metrics.

Fitting the model and checking the evaluation score gives us:

model.fit(X_train, y_train)
train_preds = model.predict(X_train)
test_preds = model.predict(X_test)

If we have a regression model, the R² score can be evaluated as:

print('Train R2 Score : %.3f'%model.score(X_train, y_train))
print('Test R2 Score : %.3f'%model.score(X_test, y_test))

In case we have a classification model, accuracy score will be:

from sklearn.metrics import accuracy_score
print('Train Accuracy Score : %.3f'%accuracy_score(y_train, train_preds))
print('Test Accuracy Score : %.3f'%accuracy_score(y_test, test_preds))

Working with XGBoost does require a solid understanding of its parameters and evaluation metrics. However, once grasped, you’ll find it one of the most flexible and powerful algorithms out there.

Part 6: Hyperparameter Tuning in XGBoost

The best way to determine the optimum values for hyperparameters is through a grid search. This involves training and evaluating a model multiple times using combinations of different hyperparameters.

Let’s look at an example of hyperparameter tuning using GridSearchCV from the sklearn library:

from sklearn.model_selection import GridSearchCV

param_test = {
gsearch = GridSearchCV(estimator = xgb.XGBClassifier( learning_rate =0.1, n_estimators=140, max_depth=5,
 min_child_weight=1, gamma=0, subsample=0.8, colsample_bytree=0.8,
 objective= 'binary:logistic', nthread=4, scale_pos_weight=1, seed=27), 
 param_grid = param_test, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch.best_params_, gsearch.best_score_

These best parameters are the optimum hyperparameters for our classifier.

These examples should instill you with the basic building blocks of managing, tuning and utilising XGBoost. As you can see, we at Zenva encourage and truly believe in learning through doing. Therefore, don’t just read this tutorial, instead, try out these code snippets, construct your examples, and learn in a more immersive manner. To the world of XGBoost and beyond! We hope this tutorial served as a fun and informative introduction to your machine learning journey. Let’s keep on learning!

Part 7: Where to go next?

By now, we trust that you’ve begun your exciting journey down the road of Python and machine learning with XGBoost. But remember, this is just the preface – there’s a whole book of knowledge waiting to be devoured!

For those of you who are hungry for more, we’re pleased to introduce our exclusive course – the Python Mini-Degree. This comprehensive collection of courses covers a broad spectrum of Python programming topics, from the basics of coding and algorithms to sophisticated game and app development.

At Zenva, we have always believed that the best way to learn is by doing. That’s why our Python Mini-Degree is loaded with hands-on projects that allow you to build your own games, applications, and real-world Python projects. By completing these courses, not only do you have the opportunity to showcase a growing portfolio of Python projects, but also the potential to achieve tangible results like landing a job, starting your own business, or even publishing your own games!

Our curriculum is designed to be flexible and accessible for beginners and experienced programmers alike. You can access it on any device, anytime, anywhere. Our qualified instructors who are experts in their fields have carefully crafted these courses along with quizzes and coding challenges to reinforce learning. Rest assured, Zenva Academy is committed to being your steadfast guide on this journey from novice to professional.

Looking for a broader collection? Check out our Python courses here. Our range of Python courses covers varying levels of complexity, making it suitable for those just getting started and those wishing to push their existing Python skills to the limit!


As we close this whirlwind tour of XGBoost with Python, take a moment to celebrate the learning that has happened. Machine learning is an exciting field with immense possibilities, and you’ve just taken a significant leap ahead by understanding XGBoost, one of its crown jewels.

As with any journey, there will be challenges and roadblocks, but at Zenva, we’re always prepared to give you a guiding hand. This tutorial is merely a grain of sand in the vast desert of knowledge. Feel like exploring more? Join us in our comprehensive Python Mini-Degree course, your next stepping stone towards mastery in Python and machine learning. We look forward to walking alongside you on this journey, sculpting the future – one line of code at a time. Happy coding!

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.