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

将Scikit XGBoostRegressor模型转daal4py推理时报错无法转为oneDAL表

问题:XGBoost模型转daal格式推理时出现RuntimeError:无法将Python对象转换为oneDAL表

我用Scikit XGBoostRegressor API训练了一个二分类任务的XGBoost模型,想转成daal格式提升推理速度,但一直碰到RuntimeError:Could not convert Python object to oneDAL table。

我的代码:

with open(r'xgboost-model', 'rb') as input_file:
    model = pickle.load(input_file)

import daal4py as d4p
daal_model = d4p.get_gbt_model_from_xgboost(model.get_booster()) 

predict = d4p.gbt_classification_prediction(2).compute(test2, daal_model).prediction

报错信息:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In [13], line 1
----> 1 prediction = d4p.gbt_classification_prediction(2).compute(test2, daal_model).prediction

File build/daal4py_cy.pyx:12628, in daal4py._daal4py.gbt_classification_prediction.compute()

File build/daal4py_cy.pyx:246, in daal4py._daal4py._execute_with_context.exec_func()

File build/daal4py_cy.pyx:12611, in daal4py._daal4py.gbt_classification_prediction._compute()

RuntimeError: Could not convert Python object to oneDAL table.
解决方法

这个错误的核心是test2的格式不被daal4py兼容,oneDAL仅支持特定类型的输入数据,比如numpy数组、pandas DataFrame或daal4py原生的dal_table对象。可以按以下步骤修复:

  • 处理稀疏矩阵:如果test2是scipy稀疏矩阵(如csr_matrix),daal4py无法直接转换,需先转为numpy数组:

    test2 = test2.toarray()
    
  • 强制转为numpy数组:不管当前数据格式,显式转换为numpy数组确保兼容性:

    import numpy as np
    test2 = np.asarray(test2)
    
  • 直接创建dal_table:若上述方法无效,可手动将数据转为daal4py的dal_table:

    test2_daal = d4p.ndarray_to_table(test2)
    predict = d4p.gbt_classification_prediction(2).compute(test2_daal, daal_model).prediction
    

另外需要注意:你用XGBoostRegressor训练二分类任务本身不合理,应该使用XGBClassifier。回归器模型在daal4py转换时可能存在兼容性问题,建议换用XGBClassifier重新训练后再尝试转换。

内容的提问来源于stack exchange,提问作者DevKnight2001

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 02:46:18