问题
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