Jar*_*rad 26 python pipeline scikit-learn grid-search
使用确定最佳参数后pipeline和GridSearchCV,我怎么pickle/ joblib后来这个过程中重新使用?当它是单个分类器时,我看到如何做到这一点......
from sklearn.externals import joblib
joblib.dump(clf, 'filename.pkl')
Run Code Online (Sandbox Code Playgroud)
但是,如何pipeline在执行和完成后用最佳参数保存整体gridsearch?
我试过了:
joblib.dump(grid, 'output.pkl') - 但是转储了每次网格搜索尝试(许多文件)joblib.dump(pipeline, 'output.pkl') - 但我不认为它包含最好的参数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'}
Run Code Online (Sandbox Code Playgroud)
Ibr*_*iev 31
from sklearn.externals import joblib
joblib.dump(grid.best_estimator_, 'filename.pkl')
Run Code Online (Sandbox Code Playgroud)
如果要将对象转储到一个文件中 - 请使用:
joblib.dump(grid.best_estimator_, 'filename.pkl', compress = 1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19686 次 |
| 最近记录: |