Sklearn如何保存使用Joblib或Pickle从管道和GridSearchCV创建的模型? [英] Sklearn How to Save a Model Created From a Pipeline and GridSearchCV Using Joblib or Pickle?

查看:451
本文介绍了Sklearn如何保存使用Joblib或Pickle从管道和GridSearchCV创建的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用pipelineGridSearchCV确定最佳参数后,如何在pickle/joblib中重新使用此过程?我看到了当它是单个分类器时该怎么做...

After identifying the best parameters using a pipeline and GridSearchCV, how do I pickle/joblib this process to re-use later? I see how to do this when it's a single classifier...

from sklearn.externals import joblib
joblib.dump(clf, 'filename.pkl') 

但是在执行并完成gridsearch之后,如何使用最佳参数保存总体pipeline?

But how do I save this overall pipeline with the best parameters after performing and completing a gridsearch?

我尝试过:

  • joblib.dump(grid, 'output.pkl')-但这转储了每个gridsearch 尝试(许多文件)
  • joblib.dump(pipeline, 'output.pkl')-但是我 不要认为其中包含最佳参数
  • joblib.dump(grid, 'output.pkl') - But that dumped every gridsearch attempt (many files)
  • joblib.dump(pipeline, 'output.pkl') - But I don't think that contains the best parameters
X_train = df['Keyword']
y_train = df['Ad Group']

pipeline = Pipeline([
  ('tfidf', TfidfVectorizer()),
  ('sgd', SGDClassifier())
  ])

parameters = {'tfidf__ngram_range': [(1, 1), (1, 2)],
              'tfidf__use_idf': (True, False),
              'tfidf__max_df': [0.25, 0.5, 0.75, 1.0],
              'tfidf__max_features': [10, 50, 100, 250, 500, 1000, None],
              'tfidf__stop_words': ('english', None),
              'tfidf__smooth_idf': (True, False),
              'tfidf__norm': ('l1', 'l2', None),
              }

grid = GridSearchCV(pipeline, parameters, cv=2, verbose=1)
grid.fit(X_train, y_train)

#These were the best combination of tuning parameters discovered
##best_params = {'tfidf__max_features': None, 'tfidf__use_idf': False,
##               'tfidf__smooth_idf': False, 'tfidf__ngram_range': (1, 2),
##               'tfidf__max_df': 1.0, 'tfidf__stop_words': 'english',
##               'tfidf__norm': 'l2'}

推荐答案

from sklearn.externals import joblib
joblib.dump(grid.best_estimator_, 'filename.pkl')

如果要将对象转储到一个文件中,请使用:

If you want to dump your object into one file - use:

joblib.dump(grid.best_estimator_, 'filename.pkl', compress = 1)

这篇关于Sklearn如何保存使用Joblib或Pickle从管道和GridSearchCV创建的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆