调用PMMLPipeline.fit()报TypeError:参数数量不符,求排查方案
问题分析:PMMLPipeline.fit()触发TypeError
环境版本
- scikitlearn 1.1.1
- XGBoost 1.7.6
- nyoka 5.5.0
- scikitlearn2pmml 0.94.1
问题场景
运行以下代码时触发TypeError:
pipeline = PMMLPipeline([("classifier", XGBClassifier(**params))]) pipeline.fit(X_train, y_train)
报错信息
TypeError Traceback (most recent call last) Cell In[28], line 1 ----> 1 pipeline.fit(X_train, y_train) File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\sklearn\base.py:1474, in _fit_context..decorator..wrapper(estimator, *args, **kwargs) 1467 estimator._validate_params() 1469 with config_context( 1470 skip_parameter_validation=( 1471 prefer_skip_nested_validation or global_skip_validation 1472 ) 1473 ): -> 1474 return fit_method(estimator, *args, **kwargs) File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\sklearn\pipeline.py:471, in Pipeline.fit(self, X, y, **params) 428 """Fit the model. 429 430 Fit all the transformers one after the other and sequentially transform the ... 468 Pipeline with fitted steps. 469 """ 470 routed_params = self._check_method_params(method="fit", props=params) --> 471 Xt = self._fit(X, y, routed_params) 472 with _print_elapsed_time("Pipeline", self._log_message(len(self.steps) - 1)): 473 if self._final_estimator != "passthrough": TypeError: PMMLPipeline._fit() takes from 2 to 3 positional arguments but 4 were given
原因分析与解决方法
这个报错的核心是scikit-learn版本与scikitlearn2pmml版本不兼容:
- scikit-learn 1.1.x开始在
Pipeline.fit()方法中新增了routed_params参数,会传递给内部的_fit()方法。 - 你使用的scikitlearn2pmml 0.94.1中的
PMMLPipeline类继承自scikit-learn的Pipeline,但它重写的_fit()方法没有适配这个新增参数,只接收2-3个位置参数,导致调用时参数数量不匹配。
解决步骤:
- 版本适配:要么升级scikitlearn2pmml到0.96.0及以上版本(该版本修复了此兼容性问题);要么降级scikit-learn到1.0.x版本,与旧版scikitlearn2pmml兼容。
- 替代方案:如果暂时无法升级/降级,可以先使用scikit-learn原生
Pipeline训练模型,之后再用scikitlearn2pmml工具将模型转换为PMML格式,避开直接调用PMMLPipeline的问题。
内容的提问来源于stack exchange,提问作者Oscar Benitez
相关产品推荐
相关产品推荐

