You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XGBoost 0.7二分类训练报错:base_score需处于(0,1)区间

Fixing XGBoostError: base_score must be in (0,1) for logistic loss

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_score isn’t being applied correctly

How to Fix It

Here are three straightforward solutions to try:

  • Explicitly set the base_score parameter
    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 your y_train uses a different encoding (like -1 and 1), you have two options:

    1. Convert your labels to 0/1 (e.g., replace -1 with 0 using y_train = (y_train + 1) // 2)
    2. Switch to a different objective that supports your label format, like objective='binary:hinge'
  • Upgrade XGBoost to the latest stable version
    Older versions of XGBoost had rare bugs related to base_score handling. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:13:06