保留(pickle)自定义 sklearn 管道的推荐方法是什么?
What is the recommended way to persist (pickle) custom sklearn pipelines?
我构建了一个 sklearn pipeline that combines a standard support vector regression component with some custom transformers that create features. This pipeline is then put into an object which is trained and then pickled (this seems to be the recommended way)。 unpickled 对象用于进行预测。
为了分发,将其转换为带有 pyinstaller 的可执行文件。
当我从单元测试中调用 unpickled 回归对象时,它工作正常。
但是,当我尝试使用 PyInstaller 二进制文件进行预测时,我得到一个很长的 stack trace,结尾为:
module = loader.load_module(fullname) File "messagestream.pxd", line 5, in init scipy.optimize._trlib._trlib ImportError: No module named 'scipy._lib.messagestream'
这感觉像是某种酸洗错误,可能是由于酸洗与 pyinstaller 的相互作用。我如何重构我的代码,以便我的自定义管道在 unpickling 后像标准的 sklearn 回归器一样轻松和稳健地运行?
好的,经过一番谷歌搜索后,根本原因似乎不是 pickling,这只是一个 pyinstaller "hidden imports" 问题,但出于某种原因,它只在 pickling 时出现(不要'不要问我为什么)。
以下解决了我的眼前问题:编辑 .spec
文件以使用 Scipy
添加以下隐藏导入:
hiddenimports=['scipy._lib.messagestream']
我还需要一些与其他库相关的其他隐藏导入
hiddenimports=['sklearn.neighbors.typedefs',
'scipy._lib.messagestream',
'pandas._libs.tslibs.timedeltas' ]
如果有人只是想通过 CLI 参数而不是通过 Roko 的回答中提供的 .spec 文件来执行此操作,则语法如下:
pyinstaller --hidden-import scipy._lib.messagestream --onefile your_python_file_here.py
我构建了一个 sklearn pipeline that combines a standard support vector regression component with some custom transformers that create features. This pipeline is then put into an object which is trained and then pickled (this seems to be the recommended way)。 unpickled 对象用于进行预测。
为了分发,将其转换为带有 pyinstaller 的可执行文件。
当我从单元测试中调用 unpickled 回归对象时,它工作正常。
但是,当我尝试使用 PyInstaller 二进制文件进行预测时,我得到一个很长的 stack trace,结尾为:
module = loader.load_module(fullname) File "messagestream.pxd", line 5, in init scipy.optimize._trlib._trlib ImportError: No module named 'scipy._lib.messagestream'
这感觉像是某种酸洗错误,可能是由于酸洗与 pyinstaller 的相互作用。我如何重构我的代码,以便我的自定义管道在 unpickling 后像标准的 sklearn 回归器一样轻松和稳健地运行?
好的,经过一番谷歌搜索后,根本原因似乎不是 pickling,这只是一个 pyinstaller "hidden imports" 问题,但出于某种原因,它只在 pickling 时出现(不要'不要问我为什么)。
以下解决了我的眼前问题:编辑 .spec
文件以使用 Scipy
添加以下隐藏导入:
hiddenimports=['scipy._lib.messagestream']
我还需要一些与其他库相关的其他隐藏导入
hiddenimports=['sklearn.neighbors.typedefs',
'scipy._lib.messagestream',
'pandas._libs.tslibs.timedeltas' ]
如果有人只是想通过 CLI 参数而不是通过 Roko 的回答中提供的 .spec 文件来执行此操作,则语法如下:
pyinstaller --hidden-import scipy._lib.messagestream --onefile your_python_file_here.py