首页 > 解决方案 > 如何在 python 中对 PMML 模型进行超参数调整?

问题描述

我正在使用 Python 中的以下代码创建 RandomForest PMML 模型

from sklearn2pmml.pipeline import PMMLPipeline
from sklearn2pmml import sklearn2pmml
rf=RandomForestClassifier()
rf = PMMLPipeline([('random',rf)])
rf.fit(X_train, y_train)
sklearn2pmml(rf, "classification pmml file/random.pmml",with_repr=True)

我正在使用 Python 中的以下代码加载保存的 RandomForest 模型

from pypmml import Model
rf = Model.fromFile('classification pmml file/random.pmml')

如何在 Python 中对此 RandomForest PMML 模型进行超参数调整?

标签: pythonmachine-learningrandom-foresthyperparameterspmml

解决方案


您可以像往常一样进行超参数调整;如果使用SkLearn2PMML包将生成的调谐管道转换为 PMML 表示,则无需执行任何特殊操作。

简而言之,如果您只调整一个估算器,那么只需将其包装GridSearchCV到位。例如:

pipeline = PMMLPipeline([
  ("tuned-rf", GridSearchCV(RandomForestClassifier(..), param_grid = {..}))
])
pipeline.fit(X, y)

如果您正在调整多个估算器,那么您可以将GridSearchCV其视为顶级工作流引擎,并将整个管道包装到其中。调整后的管道可以作为GridSearchCV.best_estimator_属性获得:

pipeline = PMMLPipeline([
  ("rf", RandomForestClassifier(..))
])
gridsearch = GridSearchCV(pipeline, param_gird = {..})
gridsearch.fit(X, y)
pipeline = gridsearch.best_estimator_

更多详情,请参阅以下技术文章:https ://openscoring.io/blog/2019/12/25/converting_sklearn_gridsearchcv_pipeline_pmml/


推荐阅读