尝试运行 xgboost.predict 或 xgboost.score 时出现奇怪的错误

问题描述 投票:0回答:3

我正在尝试在数据集上运行 xgboost 回归模型,而不会丢失任何数据。

# Run GBM on training dataset
# Create xgboost object
pts_xgb = xgb.XGBRegressor(objective="reg:squarederror", missing=None, seed=42)

# Fit xgboost onto data
pts_xgb.fit(X_train
    ,y_train
    ,verbose=True
    ,early_stopping_rounds=10
    ,eval_metric='rmse'
    ,eval_set=[(X_test,y_test)])

模型创建似乎工作正常,我使用以下命令确认 X_train 和 y_train 没有空值:

print(X_train.isnull().values.sum()) # prints 0
print(y_train.isnull().values.sum()) # prints 0

但是当我运行以下代码时,出现以下错误。

代码:

pts_xgb.score(X_train,y_train)

错误:

---------------------------------------------------------------------------
XGBoostError                              Traceback (most recent call last)
<ipython-input-37-39b223d418b2> in <module>
----> 1 pts_xgb.score(X_train_test,y_train_test)

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sklearn/base.py in score(self, X, y, sample_weight)
    551 
    552         from .metrics import r2_score
--> 553         y_pred = self.predict(X)
    554         return r2_score(y, y_pred, sample_weight=sample_weight)
    555 

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/xgboost/sklearn.py in predict(self, X, output_margin, ntree_limit, validate_features, base_margin, iteration_range)
    818         if self._can_use_inplace_predict():
    819             try:
--> 820                 predts = self.get_booster().inplace_predict(
    821                     data=X,
    822                     iteration_range=iteration_range,

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/xgboost/core.py in inplace_predict(self, data, iteration_range, predict_type, missing, validate_features, base_margin, strict_shape)
   1844             from .data import _maybe_np_slice
   1845             data = _maybe_np_slice(data, data.dtype)
-> 1846             _check_call(
   1847                 _LIB.XGBoosterPredictFromDense(
   1848                     self.handle,

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/xgboost/core.py in _check_call(ret)
    208     """
    209     if ret != 0:
--> 210         raise XGBoostError(py_str(_LIB.XGBGetLastError()))
    211 
    212 

XGBoostError: [09:18:58] /Users/travis/build/dmlc/xgboost/src/c_api/c_api_utils.h:157: Invalid missing value: null
Stack trace:
  [bt] (0) 1   libxgboost.dylib                    0x000000011e4e7064 dmlc::LogMessageFatal::~LogMessageFatal() + 116
  [bt] (1) 2   libxgboost.dylib                    0x000000011e4d9afc xgboost::GetMissing(xgboost::Json const&) + 268
  [bt] (2) 3   libxgboost.dylib                    0x000000011e4e0a13 void InplacePredictImpl<xgboost::data::ArrayAdapter>(std::__1::shared_ptr<xgboost::data::ArrayAdapter>, std::__1::shared_ptr<xgboost::DMatrix>, char const*, xgboost::Learner*, unsigned long, unsigned long, unsigned long long const**, unsigned long long*, float const**) + 531
  [bt] (3) 4   libxgboost.dylib                    0x000000011e4e04d3 XGBoosterPredictFromDense + 339
  [bt] (4) 5   libffi.dylib                        0x00007fff2dc7f8e5 ffi_call_unix64 + 85

如果我尝试运行,也会发生同样的错误

pts_xgb.predict(X_train)

编辑:这不是 X_train 或 y_train 中任何缺失/空值的问题。当使用以下数据集时,我遇到了同样的错误,该数据集比我的实际数据集小得多(见下文):

X_train:1

y_火车:2

有人知道为什么会发生这种情况吗?我找不到讨论同一问题的任何其他论坛。

python xgboost predict
3个回答
9
投票

这是一个缺失/空值问题

而不是

xgb.XGBRegressor(objective="reg:squarederror", missing=None, seed=42)

尝试

xgb.XGBRegressor(objective="reg:squarederror", missing=1, seed=42)

原因请参阅答案:如何使用 scikit-learn 的 XGBRegressor 的缺失参数


0
投票

当我想绘制混淆矩阵时,对于“ xgb.XGBClassifier ”,我也遇到了这个错误。 我删除参数“missing = None”,就可以了。 我希望这对您和其他有同样问题的人有所帮助。


0
投票

我在使用

missing='nan'
时遇到了同样的问题,我通过将其更改为
missing=float('nan')
来修复它。

© www.soinside.com 2019 - 2024. All rights reserved.