如何使python xgboost模型成为pmml样式

时间:2019-01-10 08:47:10

标签: python-3.x xgboost pmml

如何使xgboost成为pmml?

reg = XGBRegressor(learning_rate=0.1, n_estimators=30, max_depth=4, min_child_weight=4, gamma=0.1,
                       subsample=0.9, colsample_bytree=0.8, objective='binary:logistic', reg_alpha=1,
                       scale_pos_weight=1, seed=27)
param_test = [{
        'max_depth': [i for i in range(1, 3)],
        'gamma': [i / 10.0 for i in range(0, 10)],
        'n_estimators': [i for i in range(2, 14, 2)],
}]
gsearch = GridSearchCV(reg, param_grid=param_test, scoring='neg_mean_squared_error', n_jobs=4, iid=False, cv=5)
gsearch.fit(x_train, y_train)
best_model = gsearch.best_estimator_

2 个答案:

答案 0 :(得分:1)

请参见SkLear2PMML软件包:https://github.com/jpmml/sklearn2pmml

首先,定义一个新的pmml管道,然后将XGBRegressor插入其中。然后,使用GridSearchCV学习者来设置pmml管道。最后,使用GridSearchCV.best_estimator_函数调用将sklearn2pmml.sklearn2pmml(应该是优化的pmml管道)导出为PMML数据格式:

pmml_pipeline = PMMLPipeline([
  ("regressor", XGBRegressor())
])
tuner = GridSearchCV(pmml_pipeline, ...)
tuner.fit(X, y)
sklearn2pmml(tuner.best_estimator_, "xgbregressor-pipeline.pmml")

另请参见以下演示文稿的幻灯片#26:https://www.slideshare.net/VilluRuusmann/converting-scikitlearn-to-pmml

答案 1 :(得分:0)

Standard error:
    sklearn2pmml(tuner.best_estimator_,'xgbregressor_pipeline.pmml')
java.lang.UnsupportedClassVersionError: org/jpmml/sklearn/Main : Unsupported major.minor version 52.0
  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn2pmml\__init__.py", line 246, in sklearn2pmml
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    raise RuntimeError("The JPMML-SkLearn conversion application has failed. The Java executable should have printed more information about the failure into its standard output and/or standard error streams")
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
RuntimeError: The JPMML-SkLearn conversion application has failed. The Java executable should have printed more information about the failure into its standard output and/or standard error streams
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Exception in thread "main"