XGBoost 0.7二分类训练报错:base_score需处于(0,1)区间
Hey there! I’ve dealt with this exact error when working on binary classification tasks with XGBoost, so let’s walk through what’s going wrong and how to fix it quickly.
What’s Causing the Error?
The error message clearly calls out: base_score must be in (0,1) for logistic loss.
XGBoost’s default base_score is 0.5 (which is safely in the (0,1) range), so this usually pops up for one of two reasons:
- Your training labels (
y_train) aren’t in the expected format for binary logistic regression (i.e., they’re not 0/1, maybe -1/1 or another range) - There’s a quirk with your XGBoost version where the default
base_scoreisn’t being applied correctly
How to Fix It
Here are three straightforward solutions to try:
Explicitly set the
base_scoreparameter
Even though 0.5 is the default, explicitly defining it can override any unexpected environment or version-related issues. Update your code to:import xgboost as xgb XGB = xgb.XGBClassifier(base_score=0.5) model = XGB.fit(X_train, y_train)Verify your training labels
For XGBoost’s default binary logistic loss (objective='binary:logistic'), your labels should be 0 and 1. If youry_trainuses a different encoding (like -1 and 1), you have two options:- Convert your labels to 0/1 (e.g., replace -1 with 0 using
y_train = (y_train + 1) // 2) - Switch to a different objective that supports your label format, like
objective='binary:hinge'
- Convert your labels to 0/1 (e.g., replace -1 with 0 using
Upgrade XGBoost to the latest stable version
Older versions of XGBoost had rare bugs related tobase_scorehandling. Run this command to update:pip install --upgrade xgboost
Since you had success with scikit-learn’s GradientBoosting, making sure your labels are 0/1 and explicitly setting base_score should get your XGBoost model training smoothly.
内容的提问来源于stack exchange,提问作者Oussama Jabri

