首页 > 解决方案 > 升级的 sklearn 使我之前的 onehotencoder 无法转换

问题描述

我将我以前的一个 ml 模型存储到 pickle 中,并计划稍后将其用于生产。

在很长一段时间内一切正常。几个月后,我升级了我的 sklearn,现在我加载它我收到了这个警告......

> c:\programdata\miniconda3\lib\site-packages\sklearn\base.py:318:
> UserWarning: Trying to unpickle estimator OneHotEncoder from version
> 0.20.1 when using version 0.22.2.post1. This might lead to breaking code or invalid results. Use at your own risk.   UserWarning)

当我将它用于转换时,我收到了这个错误:

model_pipeline["ohe"].transform(df)

错误说:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-72436472fbb4> in <module>
----> 1 model_pipeline["ohe"].transform(df_merge[['CATEGORY']][:])

c:\programdata\miniconda3\lib\site-packages\sklearn\preprocessing\_encoders.py in transform(self, X)
    392         n_samples, n_features = X_int.shape
    393 
--> 394         if self.drop is not None:
    395             to_drop = self.drop_idx_.reshape(1, -1)
    396 

AttributeError: 'OneHotEncoder' object has no attribute 'drop'

这是模型管道的训练非常昂贵。我有什么可以在不重新训练所有内容的情况下修复这个模型管道的吗?谢谢!

标签: scikit-learn

解决方案


我也遇到了同样的问题。在我的情况下,这是因为尝试加载和使用我用以前版本的 scikit-learn 创建的编码器。当我重新创建编码器并保存时,加载后的问题消失了。


推荐阅读