'How to use Gridsearchcv to tune BaseEstimators within AdaBoostClassifier

from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import AdaBoostClassifier

from sklearn.datasets import make_classification
# generate dataset
X, y = make_classification(n_samples=100, n_features=2, n_redundant=0,
    n_clusters_per_class=1, weights=[0.50], flip_y=0, random_state=4)

abc = AdaBoostClassifier(base_estimator=DecisionTreeClassifier())


parameters = {'base_estimator__max_depth':[i for i in range(2,11,2)],
              'base_estimator__min_samples_leaf':[1,2],
              'n_estimators':[10,50,250,1000],
              'learning_rate':[0.01,0.1]}

clf = GridSearchCV(abc, parameters,verbose=3,scoring='f1',n_jobs=-1)
clf.fit(X,y)

Here I am using GridsearchCV to tune AdaBoostClassifier as well as the base estimator which is DecisionTreeClassifier. Can I use GridsearchCV to tune AdaBoostClassifier and more than one base estimator, (decisiontree and SVC,...) at the same time?**



Solution 1:[1]

Specify a list of parameter grids of every base estimator:

from sklearn.dummy import DummyClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier

parameters = [
    {'boost': [AdaBoostClassifier(base_estimator=DecisionTreeClassifier())],
     'boost__base_estimator__max_depth': (None, 10),
     'boost__learning_rate': (0.01, 0.1)},
    {'boost': [AdaBoostClassifier(base_estimator=SVC())],
     'boost__base_estimator__degree': (2, 3),
     'boost__learning_rate': (0.01, 0.1)},
]
boost_pipe = Pipeline([('boost', DummyClassifier())])
grid = GridSearchCV(boost_pipe, parameters)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sanjar Adilov