coremltools无法将XGBoost分类器转换为coreML模型

时间:2017-12-31 21:56:57

标签: python ios xcode9 xgboost coremltools

我对coremltools有疑问。

我想将经过培训的xgboost分类器模型转换为coreML模型。

import coremltools
import xgboost as xgb


X, y = get_data()

xgb_model = xgb.XGBClassifier()
xib_model.train(X, y)
coreml_model = coremltools.converters.xgboost.convert(xgb_model)
coremltools.save('my_model.mlmodel')

错误如下:

>>> coremltools.converters.xgboost.convert(xgb_model)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/karas/.pyenv/versions/anaconda2-4.3.0/lib/python2.7/site-packages/coremltools/converters/xgboost/_tree.py", line 51, in convert
    return _MLModel(_convert_tree_ensemble(model, feature_names, target, force_32bit_float = force_32bit_float))
  File "/Users/karas/.pyenv/versions/anaconda2-4.3.0/lib/python2.7/site-packages/coremltools/converters/xgboost/_tree_ensemble.py", line 143, in convert_tree_ensemble
    raise TypeError("Unexpected type. Expecting XGBoost model.")
TypeError: Unexpected type. Expecting XGBoost model.

1 个答案:

答案 0 :(得分:0)

快速解决方案:

coreml_model = coremltools.converters.xgboost.convert(xgb_model._Booster)

有关此转换器的更多信息: 我刚遇到这个问题,所以我调试到_tree_ensemble.py,这就是我找到的:

第一个参数&#39;模型&#39;应为_xgboost.core.Booster_xgboost.XGBRegressor或前两个数据的 .json文件路径。此外,如果使用.json文件,则必须提供第二个参数feature_names

另外,根据the python examples on github,还有另一种方法可以获得模型:

import numpy as np
import scipy.sparse
import pickle
import xgboost as xgb

### simple example
# load file from text file, also binary buffer generated by xgboost
dtrain = xgb.DMatrix('../data/agaricus.txt.train')
dtest = xgb.DMatrix('../data/agaricus.txt.test')

# specify parameters via map, definition are same as c++ version
param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic'}

# specify validations set to watch performance
watchlist = [(dtest, 'eval'), (dtrain, 'train')]
num_round = 2
booster = xgb.train(param, dtrain, num_round, watchlist)

请注意booster此处为_xgboost.core.Booster

然后你可以做

import coremltools
coreml_model = coremltools.converters.xgboost.convert(booster)
coreml_model.save('my_model.mlmodel')