GridSearch on Model and Classifiers

谁说我不能喝 提交于 2019-12-02 08:32:11

问题


I just came across this example on Model Grid Selection here:

https://chrisalbon.com/machine_learning/model_selection/model_selection_using_grid_search/

Question:

The example reads

# Create a pipeline
pipe = Pipeline([('classifier', RandomForestClassifier())])

# Create space of candidate learning algorithms and their hyperparameters
search_space = [{'classifier': [LogisticRegression()],
                 'classifier__penalty': ['l1', 'l2'],
                 'classifier__C': np.logspace(0, 4, 10)},
                {'classifier': [RandomForestClassifier()],
                 'classifier__n_estimators': [10, 100, 1000],
                 'classifier__max_features': [1, 2, 3]}]lassifier', RandomForestClassifier())])

As I understand the code, search_space contains the used classifiers and their parameters. However, I don't get what the purpose of Pipeline and why it contains RandomForestClassifier()?

Background: In my desired workflow, I need to train a doc2vec model (gensim), based on 3 different classifiers. Both the model and the classifiers should apply GridSearch to parameters. I like to store the results in a table and save the best model, that is the one with the highest accuracy.


回答1:


Pipeline is used to chain sequential data transformation models followed last by the classifier / regressor. Something like first converting the text to numbers using TfidfVectorizer and then training the classifier.

pipe = Pipeline([('vectorizer',TfidfVectorizer()), 
                 ('classifier', RandomForestClassifier())])

For only a single class, no need of Pipeline.

Here in your code, its used as a placeholder, so that the parameters can be used by using the 'classifier' prefix. And the classifier itself can be substituted from the params.



来源:https://stackoverflow.com/questions/50272416/gridsearch-on-model-and-classifiers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!