When working with XGBoost, it’s important to know which version you have installed.
Different versions may have different features, bug fixes, or compatibility requirements.
Checking your XGBoost version is straightforward in Python.
import xgboost
print(xgboost.__version__)
Example output (your output may differ):
2.0.3
Here’s what’s happening:
- We import the
xgboostmodule. - We use the
__version__attribute of thexgboostmodule, which holds the installed version as a string. - Printing
xgboost.__version__gives us the version number in the formatmajor.minor.patch.
The version string follows the semantic versioning convention:
- The
majorversion indicates significant changes that may break backward compatibility. - The
minorversion indicates new features or improvements that are backward compatible. - The
patchversion indicates bug fixes or minor changes.
Knowing your XGBoost version is useful when:
- You’re following a tutorial or example code that requires a specific version.
- You’re working with other libraries that have compatibility requirements.
- You’re reporting a bug or seeking help on forums or issue trackers.
To compare versions, you can split the version string and compare the individual parts as integers. For example:
import xgboost
required_version = "2.0.0"
current_version = xgboost.__version__
required_parts = [int(x) for x in required_version.split(".")]
current_parts = [int(x) for x in current_version.split(".")]
if current_parts >= required_parts:
print(f"XGBoost version {current_version} meets the minimum requirement of {required_version}")
else:
print(f"XGBoost version {current_version} does not meet the minimum requirement of {required_version}")
This script compares the current version with a minimum required version, which is useful when running code that depends on specific XGBoost features or bug fixes.