jpmml / jpmml-sklearn

Java library and command-line application for converting Scikit-Learn pipelines to PMML
GNU Affero General Public License v3.0
531 stars 117 forks source link

Convert XGBRegressor Model to PMML File Encounter An Error #159

Closed EVOk06 closed 3 years ago

EVOk06 commented 3 years ago

I'm getting an problem when I try to convert XGBRegressor model to pmml model file. part of source code:

import xgboost as xgb

XGB2 = xgb.XGBRegressor(
learning_rate =0.1,
n_estimators=1000,
max_depth=7,
reg_alpha = 0.01,
colsample_bytree=0.8,
objective= 'reg:squarederror', 
nthread=8, random_state = 7,
use_label_encoder=False)
pipeline = PMMLPipeline([('regressor', XGB2)])
sklearn2pmml(pipeline, 'test.pmml', debug=True)

and then throw an java.lang.IllegalArgumentException, all debug information as followed

python: 3.7.7
sklearn: 0.24.1
sklearn2pmml: 0.69.0
joblib: 0.13.0
sklearn_pandas: 2.1.0
pandas: 1.2.3
numpy: 1.19.1
openjdk: 1.8.0_181
Executing command:
java -cp /home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/gson-2.8.6.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/guava-21.0.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/h2o-genmodel-3.32.0.4.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/h2o-logger-3.32.0.4.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/h2o-tree-api-0.3.17.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/istack-commons-runtime-3.0.11.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/jakarta.activation-1.2.2.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/jakarta.xml.bind-api-2.3.3.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/jaxb-runtime-2.3.3.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/jcommander-1.72.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/jpmml-converter-1.4.6.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/jpmml-h2o-1.1.4.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/jpmml-lightgbm-1.3.6.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/jpmml-python-1.0.11.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/jpmml-sklearn-1.6.15.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/jpmml-xgboost-1.5.0.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/pickle-1.1.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/pmml-model-1.5.11.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/pmml-model-metro-1.5.11.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/serpent-1.30.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/slf4j-api-1.7.30.jar:/home/xiewenkang/.local/lib/python3.7/site-packages/sklearn2pmml/resources/slf4j-jdk14-1.7.30.jar org.jpmml.sklearn.Main --pkl-pipeline-input /tmp/pipeline-x8q1gjb9.pkl.z --pmml-output test.pmml
Standard output is empty
Standard error:
Mar 31, 2021 1:22:03 PM org.jpmml.sklearn.Main run
INFO: Parsing PKL..
Mar 31, 2021 1:22:03 PM org.jpmml.sklearn.Main run
INFO: Parsed PKL in 7 ms.
Mar 31, 2021 1:22:03 PM org.jpmml.sklearn.Main run
INFO: Converting PKL to PMML..
Mar 31, 2021 1:22:03 PM sklearn2pmml.pipeline.PMMLPipeline initTargetFields
WARNING: Attribute 'sklearn2pmml.pipeline.PMMLPipeline.target_fields' is not set. Assuming y as the name of the target field
Mar 31, 2021 1:22:03 PM org.jpmml.sklearn.Main run
SEVERE: Failed to convert PKL to PMML
java.lang.IllegalArgumentException: Attribute 'xgboost.sklearn.XGBRegressor._Booster' not set
    at org.jpmml.python.PythonObject.get(PythonObject.java:69)
    at xgboost.sklearn.XGBRegressor.getBooster(XGBRegressor.java:50)
    at xgboost.sklearn.BoosterUtil.getLearner(BoosterUtil.java:66)
    at xgboost.sklearn.BoosterUtil.getNumberOfFeatures(BoosterUtil.java:39)
    at xgboost.sklearn.XGBRegressor.getNumberOfFeatures(XGBRegressor.java:35)
    at sklearn2pmml.pipeline.PMMLPipeline.initActiveFields(PMMLPipeline.java:532)
    at sklearn2pmml.pipeline.PMMLPipeline.encodePMML(PMMLPipeline.java:218)
    at org.jpmml.sklearn.Main.run(Main.java:233)
    at org.jpmml.sklearn.Main.main(Main.java:151)

Exception in thread "main" java.lang.IllegalArgumentException: Attribute 'xgboost.sklearn.XGBRegressor._Booster' not set
    at org.jpmml.python.PythonObject.get(PythonObject.java:69)
    at xgboost.sklearn.XGBRegressor.getBooster(XGBRegressor.java:50)
    at xgboost.sklearn.BoosterUtil.getLearner(BoosterUtil.java:66)
    at xgboost.sklearn.BoosterUtil.getNumberOfFeatures(BoosterUtil.java:39)
    at xgboost.sklearn.XGBRegressor.getNumberOfFeatures(XGBRegressor.java:35)
    at sklearn2pmml.pipeline.PMMLPipeline.initActiveFields(PMMLPipeline.java:532)
    at sklearn2pmml.pipeline.PMMLPipeline.encodePMML(PMMLPipeline.java:218)
    at org.jpmml.sklearn.Main.run(Main.java:233)
    at org.jpmml.sklearn.Main.main(Main.java:151)

Preserved joblib dump file(s): /tmp/pipeline-x8q1gjb9.pkl.z

Why this exception occur?And how to fix it?

vruusmann commented 3 years ago

Why this exception occur?

The XGB(Classifier|Regressor)._Booster attribute is initialized during model training.

You haven't trained your model yet.

And how to fix it?

Invoke (PMML)Pipeline.fit(X, y) with a training dataset.