使用 sklearn2pmml 将 XGBoost 分类器写入 pmml 时出错

Error writing XGBoost Classifier to pmml with sklearn2pmml

我想使用 sklearn2pmml 将我的 XGBoost 模型保存为 pmml。我正在使用 Python V3.7.3 和 Sklearn 0.20.3 & sklearn2pmml V0.53.0。我的数据主要是二进制数据,只有 3 列连续数据,我是 运行 我在 Databricks 中的笔记本,并将我的 Spark 数据帧转换为 pandas 数据帧。下面的代码片段

import xgboost as xgb

from sklearn_pandas import DataFrameMapper
from sklearn.compose import ColumnTransformer

from sklearn2pmml import sklearn2pmml
from sklearn2pmml.pipeline import PMMLPipeline
from sklearn2pmml.decoration import ContinuousDomain
from sklearn.preprocessing import StandardScaler

X = pdf[continuous_features + numericCols]
y = pdf["Label"]


mapper = DataFrameMapper(
  [([cont_column], [ContinuousDomain(), StandardScaler()]) for cont_column in continuous_features] +
  [([c for c in numericCols], None)] # no transformation
)

clf = xgb.XGBClassifier(objective='multi:softprob',eval_metric='auc',num_class = 2,
                        n_jobs =6,max_delta_step=1, min_child_weight=14, gamma=1.5, subsample = 0.8,
                        colsample_bytree = 0.5, max_depth=10, learning_rate = 0.1)


pipeline = PMMLPipeline([
  ("mapper", mapper),
  ("estimator", clf)
])

pipeline.fit(X,y.values.reshape(-1,))

sklearn2pmml(pipeline, "xgb_V1.pmml", with_repr = True)

管道适合数据,使用 pipeline.score(X,y) 和 pipeline.predict(X) 生成分数和预测,但是当我尝试将其写入 pmml 时,我得到以下错误:

Standard output is empty
Standard error:
Feb 21, 2020 1:53:30 PM org.jpmml.sklearn.Main run
INFO: Parsing PKL..
Feb 21, 2020 1:53:30 PM org.jpmml.sklearn.Main run
INFO: Parsed PKL in 47 ms.
Feb 21, 2020 1:53:30 PM org.jpmml.sklearn.Main run
INFO: Converting..
Feb 21, 2020 1:53:30 PM sklearn2pmml.pipeline.PMMLPipeline initTargetFields
WARNING: Attribute 'sklearn2pmml.pipeline.PMMLPipeline.target_fields' is not set. Assuming y as the name of the target field
Feb 21, 2020 1:53:30 PM org.jpmml.sklearn.Main run
SEVERE: Failed to convert
java.lang.IllegalArgumentException: Attribute 'xgboost.sklearn.XGBClassifier._le' has an unsupported value (Python class xgboost.compat.XGBoostLabelEncoder)
 at org.jpmml.sklearn.CastFunction.apply(CastFunction.java:45)
 at org.jpmml.sklearn.PyClassDict.get(PyClassDict.java:82)
 at sklearn.LabelEncoderClassifier.getLabelEncoder(LabelEncoderClassifier.java:40)
 at sklearn.LabelEncoderClassifier.getClasses(LabelEncoderClassifier.java:34)
 at sklearn.ClassifierUtil.getClasses(ClassifierUtil.java:32)
 at sklearn2pmml.pipeline.PMMLPipeline.encodePMML(PMMLPipeline.java:133)
 at org.jpmml.sklearn.Main.run(Main.java:145)
 at org.jpmml.sklearn.Main.main(Main.java:94)
Caused by: java.lang.ClassCastException: Cannot cast net.razorvine.pickle.objects.ClassDict to sklearn.preprocessing.LabelEncoder
 at java.lang.Class.cast(Class.java:3369)
 at org.jpmml.sklearn.CastFunction.apply(CastFunction.java:43)
 ... 7 more

Exception in thread "main" java.lang.IllegalArgumentException: Attribute 'xgboost.sklearn.XGBClassifier._le' has an unsupported value (Python class xgboost.compat.XGBoostLabelEncoder)
 at org.jpmml.sklearn.CastFunction.apply(CastFunction.java:45)
 at org.jpmml.sklearn.PyClassDict.get(PyClassDict.java:82)
 at sklearn.LabelEncoderClassifier.getLabelEncoder(LabelEncoderClassifier.java:40)
 at sklearn.LabelEncoderClassifier.getClasses(LabelEncoderClassifier.java:34)
 at sklearn.ClassifierUtil.getClasses(ClassifierUtil.java:32)
 at sklearn2pmml.pipeline.PMMLPipeline.encodePMML(PMMLPipeline.java:133)
 at org.jpmml.sklearn.Main.run(Main.java:145)
 at org.jpmml.sklearn.Main.main(Main.java:94)
Caused by: java.lang.ClassCastException: Cannot cast net.razorvine.pickle.objects.ClassDict to sklearn.preprocessing.LabelEncoder
 at java.lang.Class.cast(Class.java:3369)
 at org.jpmml.sklearn.CastFunction.apply(CastFunction.java:43)

我认为这可能是 Sklearn 和 sklearn2pmml 之间的版本不兼容问题 post https://github.com/jpmml/sklearn2pmml/issues/197,但我认为我安装的版本应该没问题。关于这是怎么回事的任何想法?提前致谢

这可能是 XGBoost 包版本问题。 SkLearn2PMML 包期望标签编码器(XGBClassifier._le 属性)是一个 "normal" Scikit-Learn 标签编码器 class(sklearn.preprocessing.(label|_label).LabelEncoder),但在你的情况下它是不同的(xgboost.compat.XGBoostLabelEncoder).

这个 xgboost.compat.XGBoostLabelEncoder 是在哪个 XGBOost 包版本中引入的?要么很旧,要么很新。

无论如何,请使用 JPMML-SkLearn 项目 here 提出功能请求以解决此问题。