首页 > 解决方案 > 在 python 中的 sklearn 中保存 EllipticEnvelope 模型

问题描述

我需要保存一个基于 EllipticEnvelope 的模型,然后将其转换为 Tensorflow Lite 图像以在移动应用程序上使用它。有人可以帮我如何存储模型吗?

model =  EllipticEnvelope(contamination=outliers_fraction, 
                         #behaviour="new",
                         random_state=RANDOM_SEED, support_fraction=0.7)

model.fit(std_data)

我试过这样的事情:

json_model = model.to_json()
open('model_architecture.json', 'w').write(json_model)

但我得到这个错误:

'EllipticEnvelope' 对象没有属性 'to_json'

标签: pythontensorflowscikit-learntensorflow-lite

解决方案


它不起作用,因为EllipticEnvelope类没有该方法。该类的可用方法在这里: https ://scikit-learn.org/stable/modules/generated/sklearn.covariance.EllipticEnvelope.html

没有简单的方法将 sklearn 模型保存为 JSON。为此,您必须手动编写方法以保存到 JSON 并从 JSON 加载。

SKLearn 建议将模型保存为 pickle 或使用 Joblib。这在这里描述: https ://scikit-learn.org/stable/modules/model_persistence.html

在这里您可以阅读示例教程如何将 JSON 转储/加载功能添加到继承的 sklearn 类中: https ://stackabuse.com/scikit-learn-save-and-restore-models/

尽管如果您可以使用泡菜,我会不鼓励您这样做。

我不确定是否有简单的方法可以将 sklearn 模型转换为 Tensorflow Lite。


推荐阅读