XGBoost (eXtreme Gradient Boosting) implementation.
Classes
XGBoost classifier for high-performance gradient boosting.
__init__( self, n_estimators: int = 100, max_depth: int = 6, learning_rate: float = 0.3, subsample: float = 1.0, colsample_bytree: float = 1.0, min_child_weight: float = 1.0, gamma: float = 0.0, reg_alpha: float = 0.0, reg_lambda: float = 1.0, objective: str = 'binary:logistic', random_state: Optional[int] = None, )
Overview
The algorithm builds an additive ensemble of decision trees:
- Initialize the model with a constant prediction (e.g., log-odds for classification)
- For each boosting round, compute the negative gradient (pseudo-residuals) and
- Fit a new regression tree to the negative gradient using an
- Prune the tree using the \gamma (minimum loss reduction) threshold
- Add the new tree to the ensemble, scaled by the learning rate \eta
- Repeat until the specified number of boosting rounds is reached
Theory
At boosting round t, XGBoost minimizes the regularized objective:
where the regularization term is:
Using a second-order Taylor expansion, the objective becomes:
where g_i = \partial_{\hat{y}} l(y_i, \hat{y}_i^{(t-1)}) and h_i = \partial^2_{\hat{y}} l(y_i, \hat{y}_i^{(t-1)}) are the first and second order gradients. The optimal leaf weight for leaf j is:
Parameters
n_estimators
max_depth
learning_rate
subsample
colsample_bytree
min_child_weight
gamma
reg_alpha
reg_lambda
objective
"multi:softprob" for multi-class tasks.
random_state
Attributes
model_
classes_
fit().
n_classes_
fit().
Notes
Complexity:
- Training: O(T \cdot n \cdot d \cdot D) where T = n_estimators,
- Prediction: O(T \cdot D) per sample
- Structured / tabular classification tasks with moderate-to-large datasets
- When you need built-in handling of missing values
- Competitions and benchmarks where predictive accuracy is paramount
- When L1 and L2 regularization are needed to control model complexity
- Datasets that benefit from second-order gradient optimization
References
See Also
Train an XGBoost classifier on a binary classification task:
>>> from tuiml.algorithms.gradient_boosting import XGBoostClassifier
>>> import numpy as np
>>>
>>> X_train = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
>>> y_train = np.array([0, 0, 1, 1])
>>> clf = XGBoostClassifier(n_estimators=100, learning_rate=0.1)
>>> clf.fit(X_train, y_train)
>>> y_pred = clf.predict(X_train)
Methods
__repr__
(self) -> str
XGBoost regressor for high-performance gradient boosting on continuous targets.
__init__( self, n_estimators: int = 100, max_depth: int = 6, learning_rate: float = 0.3, subsample: float = 1.0, colsample_bytree: float = 1.0, min_child_weight: float = 1.0, gamma: float = 0.0, reg_alpha: float = 0.0, reg_lambda: float = 1.0, objective: str = 'reg:squarederror', random_state: Optional[int] = None, )
Overview
The regression variant follows the same additive training procedure:
- Initialize predictions with a constant value (e.g., mean of targets)
- For each boosting round, compute the gradient and Hessian of the
- Fit a regression tree to the negative gradient using approximate
- Prune the tree using the \gamma threshold and regularization
- Update predictions by adding the new tree scaled by learning rate \eta
- Repeat until all boosting rounds are completed
Theory
For the default squared-error objective, the loss for sample i is:
The gradients are g_i = \hat{y}_i - y_i and h_i = 1. The regularized objective at round t is:
where T is the number of leaves, w_j are leaf weights, \lambda is L2 regularization, and \alpha is L1 regularization.
Parameters
n_estimators
max_depth
learning_rate
subsample
colsample_bytree
min_child_weight
gamma
reg_alpha
reg_lambda
objective
random_state
Attributes
model_
Notes
Complexity:
- Training: O(T \cdot n \cdot d \cdot D) where T = n_estimators,
- Prediction: O(T \cdot D) per sample
- Structured / tabular regression tasks with moderate-to-large datasets
- When the data contains missing values that should be handled natively
- When L1 and L2 regularization are needed for controlling overfitting
- Benchmarks where predictive accuracy on continuous targets is paramount
References
See Also
Train an XGBoost regressor on a simple regression task:
>>> from tuiml.algorithms.gradient_boosting import XGBoostRegressor
>>> import numpy as np
>>>
>>> X_train = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
>>> y_train = np.array([1.5, 3.5, 5.5, 7.5])
>>> reg = XGBoostRegressor(n_estimators=100, max_depth=5)
>>> reg.fit(X_train, y_train)
>>> y_pred = reg.predict(X_train)
Methods
__repr__
(self) -> str