如何修复“ DMatrix / Booster尚未初始化或已被处置”错误

时间:2019-05-22 03:59:26

标签: python python-3.x xgboost

我训练了一个xgboost模型并将其保存。然后,将其复制到另一个系统中,以通过以下代码预测结果。

Python 3.7, xgboost 0.8, 康达

如果我从csv文件加载数据,则同一模型文件可以工作。

try:
    d_input = xgb.DMatrix([].append(input_vector))
    xgb_model = xgb.Booster({'nthread': 4})
    xgb_model.load_model('./models/xgboost.model')
    print("Load model successfully.")
    print(xgb_model)
    docs[i].weight = xgb_model.predict(d_input)
except Exception as fail_xgb:
    print(fail_xgb)

错误消息是:

Load model successfully.

<xgboost.core.Booster object at 0x1a1e72d4a8>
b'[11:51:46] src/c_api/c_api.cc:817: DMatrix/Booster has not been intialized or has already been disposed.\n\nStack trace returned 5 entries:\n[bt] (0) 0   libxgboost.dylib                    0x0000001a1dfe7181 dmlc::StackTrace() + 305\n[bt] (1) 1   libxgboost.dylib                    0x0000001a1dfe6f0f dmlc::LogMessageFatal::~LogMessageFatal() + 47\n[bt] (2) 2   libxgboost.dylib                    0x0000001a1e0007c3 XGDMatrixNumCol + 99\n[bt] (3) 3   libffi.6.dylib                      0x000000010b6e6884 ffi_call_unix64 + 76\n[bt] (4) 4   ???                                 0x0000700000e63d70 0x0 + 123145317399920\n\n'

1 个答案:

答案 0 :(得分:0)

该错误已修复。只需更改

d_input = xgb.DMatrix([].append(input_vector))

d_input = xgb.DMatrix([[].append(input_vector)])

异常消息非常奇怪。我对此感到困惑。