Course Wiki
XGBoost parameter tuning — Machine Learning Zoomcamp Module 6
XGBoost has various tunable parameters but the three most important ones are:
Related Pages
Machine Learning Zoomcamp › Module 6: Decision Trees and Ensemble Learning › XGBoost parameter tuning
Notes
XGBoost has various tunable parameters but the three most important ones are:
eta(default=0.3)- It is also called
learning_rateand is used to prevent overfitting by regularizing the weights of new features in each boosting step. range: [0, 1]
- It is also called
max_depth(default=6)- Maximum depth of a tree. Increasing this value will make the model more complex and more likely to overfit. range: [0, inf]
min_child_weight(default=1)- Minimum number of samples in leaf node. range: [0, inf]
For XGBoost models, there are other ways of finding the best parameters as well but the one we implement in the notebook follows the sequence of:
- First find the best value for
eta - Second, find the best value for
max_depth - Third, find the best value for
min_child_weight
Other useful parameter are:
subsample(default=1)- Subsample ratio of the training instances. Setting it to 0.5 means that model would randomly sample half of the training data prior to growing trees. range: (0, 1]
colsample_bytree(default=1)- This is similar to random forest, where each tree is made with the subset of randomly choosen features.
lambda(default=1)- Also called
reg_lambda. L2 regularization term on weights. Increasing this value will make model more conservative.
- Also called
alpha(default=0)- Also called
reg_alpha. L1 regularization term on weights. Increasing this value will make model more conservative.
- Also called
Alternative: Tuning XGBoost using a loop
Instead of repeating the training process manually for each eta value, you can use a loop to automate it (and other parameters to be tuned):
# Train XGBoost models for each eta and store AUC results
scores = {} # dictionary to store results for each eta
etas = [0.01, 0.05, 0.1, 0.3, 1.0] # list of parameter values. in this case it is 'eta'.
for eta in etas:
evals_result = {}
xgb_params = {
'eta': eta,
'max_depth': 6,
'min_child_weight': 1,
'objective': 'binary:logistic',
'eval_metric': 'auc',
'nthread': 8,
'seed': 1,
'verbosity': 1
}
model = xgb.train(
xgb_params,
dtrain,
evals=watchlist,
verbose_eval=0,
num_boost_round=200,
evals_result=evals_result
)
columns = ['iter', 'train_auc', 'val_auc']
train_aucs = list(evals_result['train'].values())[0]
val_aucs = list(evals_result['val'].values())[0]
df_results = pd.DataFrame(
list(zip(range(1, len(train_aucs) + 1), train_aucs, val_aucs)),
columns=columns
)
key = f'eta={eta}'
scores[key] = df_results
Add notes from the video (PRs are welcome)
| ⚠️ |
The notes are written by the community. If you see an error here, please create a PR with a fix. |
Key concepts
Related notes
- mlz-m01-introduction-to-numpy
- mlz-m01-introduction-to-pandas
- mlz-m01-linear-algebra-refresher
- mlz-m01-model-selection-process
- mlz-m01-setting-up-the-environment
- mlz-m01-summary
- mlz-m02-baseline-model-for-car-price-prediction-project
- mlz-m02-car-price-prediction-project
- mlz-m02-car-price-prediction-project-summary
- mlz-m02-categorical-variables
- mlz-m02-computing-rmse-on-validation-data
- mlz-m02-data-preparation
- mlz-m02-exploratory-data-analysis
- mlz-m02-feature-engineering
- mlz-m02-linear-regression
- mlz-m02-linear-regression-vector-form
- mlz-m02-regularization
- mlz-m02-root-mean-squared-error-rmse
- mlz-m02-setting-up-the-validation-framework
- mlz-m02-training-linear-regression-normal-equation
- mlz-m02-tuning-the-model
- mlz-m02-using-the-model
- mlz-m03-data-preparation
- mlz-m03-eda
- mlz-m03-explore-more
- mlz-m03-feature-importance-churn-rate-and-risk-ratio
- mlz-m03-feature-importance-correlation
- mlz-m03-feature-importance-mutual-information
- mlz-m03-logistic-regression
- mlz-m03-model-interpretation
- mlz-m03-one-hot-encoding
- mlz-m03-setting-up-the-validation-framework
- mlz-m03-training-logistic-regression-with-scikit-learn
- mlz-m04-accuracy-and-dummy-model
- mlz-m04-confusion-table
- mlz-m04-cross-validation
- mlz-m04-evaluation-metrics-session-overview
- mlz-m04-explore-more
- mlz-m04-precision-and-recall
- mlz-m04-roc-auc
- mlz-m04-roc-curves
- mlz-m04-summary
- mlz-m05-summary
- mlz-m06-credit-risk-scoring-project
- mlz-m06-data-cleaning-and-preparation
- mlz-m06-decision-tree-learning-algorithm
- mlz-m06-decision-trees
- mlz-m06-decision-trees-parameter-tuning
- mlz-m06-ensemble-learning-and-random-forest
- mlz-m06-explore-more
- mlz-m06-gradient-boosting-and-xgboost
- mlz-m06-selecting-the-best-model
- mlz-m06-summary
- mlz-m08-fashion-classification
- mlz-m08-installation-of-tensorflow
- mlz-m08-regularization-and-dropout
- mlz-m08-transfer-learning
- mlz-m09-explore-more
- mlz-m10-creating-a-pre-processing-service