【问题标题】:Save a scikit-learn pipeline to a file将 scikit-learn 管道保存到文件
【发布时间】:2019-12-19 09:25:06
【问题描述】:

如何将经过训练的 scikit-learn 管道保存到本地文件?官方文档是这样说的:https://scikit-learn.org/stable/modules/model_persistence.html

但在尝试保存管道时,我收到错误消息。示例:

estimators = [
    ('tfidf', TfidfVectorizer(tokenizer=lambda string: string.split(),
                             min_df=20, 
                             max_df=0.75,
                             ngram_range=(1,1))),
    ('clf', RandomForestClassifier(n_estimators=100,
                                   n_jobs=-1, 
                                   class_weight='balanced'))
]

p = Pipeline(estimators)
p.fit(x_train, y_train)

model = 'model.joblib'
joblib.dump(p, model)

但是,我收到错误消息“PicklingError: Can't pickle at 0x7f4c9f1e50d0>: it's not found as ma​​in.”。

我该如何解决这个问题?

【问题讨论】:

  • 你试过json.dump而不是joblib吗?

标签: python scikit-learn


【解决方案1】:

抱歉,伙计们问了这个问题。我找到了解决方案:

但并非所有东西都可以(轻松)腌制:例如生成器、内部类、lambda 函数和 defaultdicts。对于 lambda 函数,您需要使用一个名为 dill 的附加包。使用 defaultdicts,您需要使用模块级函数来创建它们。

来源:https://www.datacamp.com/community/tutorials/pickle-python-tutorial

【讨论】:

  • 你可以关闭它。 (尽管我认为其中一些线程是开放的以供参考)
猜你喜欢
  • 2015-08-14
  • 2016-10-25
  • 2017-02-25
  • 2019-09-15
  • 2021-03-17
  • 2022-01-11
  • 2017-07-13
  • 2022-01-13
  • 2012-05-22
相关资源
最近更新 更多