加载训练有素的 scikit-learn/imblearn 管道模型时出现问题

Posted

技术标签:

【中文标题】加载训练有素的 scikit-learn/imblearn 管道模型时出现问题【英文标题】:Problem loading trained scikit-learn/imblearn pipeline model 【发布时间】:2021-06-02 12:11:33 【问题描述】:

我已经使用来自 Scikit-learn 的 imblearn 和 RandomForestClassifer 构建并训练了一个 imblearn.pipeline Pipeline

使用joblib.dump('model.joblib')保存模型。

但是,当我尝试加载模型时,它会引发错误

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-26-d3ee277020d2> in <module>
----> 1 model = joblib.load('model.joblib')

~/SageMaker/custom-miniconda/miniconda/envs/datascience/lib/python3.7/site-packages/joblib/numpy_pickle.py in load(filename, mmap_mode)
    583                     return load_compatibility(fobj)
    584 
--> 585                 obj = _unpickle(fobj, filename, mmap_mode)
    586     return obj

~/SageMaker/custom-miniconda/miniconda/envs/datascience/lib/python3.7/site-packages/joblib/numpy_pickle.py in _unpickle(fobj, filename, mmap_mode)
    502     obj = None
    503     try:
--> 504         obj = unpickler.load()
    505         if unpickler.compat_mode:
    506             warnings.warn("The file '%s' has been generated with a "

~/SageMaker/custom-miniconda/miniconda/envs/datascience/lib/python3.7/pickle.py in load(self)
   1086                     raise EOFError
   1087                 assert isinstance(key, bytes_types)
-> 1088                 dispatch[key[0]](self)
   1089         except _Stop as stopinst:
   1090             return stopinst.value

~/SageMaker/custom-miniconda/miniconda/envs/datascience/lib/python3.7/pickle.py in load_global(self)
   1374         module = self.readline()[:-1].decode("utf-8")
   1375         name = self.readline()[:-1].decode("utf-8")
-> 1376         klass = self.find_class(module, name)
   1377         self.append(klass)
   1378     dispatch[GLOBAL[0]] = load_global

~/SageMaker/custom-miniconda/miniconda/envs/datascience/lib/python3.7/pickle.py in find_class(self, module, name)
   1424             elif module in _compat_pickle.IMPORT_MAPPING:
   1425                 module = _compat_pickle.IMPORT_MAPPING[module]
-> 1426         __import__(module, level=0)
   1427         if self.proto >= 4:
   1428             return _getattribute(sys.modules[module], name)[0]

ModuleNotFoundError: No module named 'imblearn.over_sampling._smote.base'; 'imblearn.over_sampling._smote' is not a package

我确实在 conda 环境中安装了 imblearn。不知道为什么它没有找到 imblearn。任何提示都会有所帮助。

【问题讨论】:

【参考方案1】:

使用

python-m pip install package name 

在系统上全局安装它,也许它可以帮助

【讨论】:

以上是关于加载训练有素的 scikit-learn/imblearn 管道模型时出现问题的主要内容,如果未能解决你的问题,请参考以下文章

加载训练有素的 scikit-learn/imblearn 管道模型时出现问题

Pytorch模型保存与加载,并在加载的模型基础上继续训练

深度学习-数据加载优化-训练速度提升一倍

深度学习-数据加载优化-训练速度提升一倍

深度学习-数据加载优化-训练速度提升一倍

加载训练的模型参数并继续训练