How to select model using Grid Search in Python?
0

How to select model using Grid Search in Python?

This recipe helps you select model using Grid Search in Python
In [1]:
## How to select model using Grid Search in Python
def Snippet_144():
    print()
    print(format('How to select model using Grid Search in Python','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import numpy as np
    from sklearn import datasets
    from sklearn.linear_model import LogisticRegression
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.model_selection import GridSearchCV
    from sklearn.pipeline import Pipeline

    # Set random seed
    np.random.seed(0)

    # Load Iris Dataset
    iris = datasets.load_iris()
    X = iris.data
    y = iris.target

    # Create Pipeline With Model Selection Search Space
    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]
                    }]

    # Create grid search 
    clf = GridSearchCV(pipe, search_space, cv=5, verbose=0, n_jobs = -1)

    # Conduct Model Selection Using Grid Search
    best_model = clf.fit(X, y)

    # View best model
    print(); print(best_model.best_estimator_.get_params()['classifier'])

Snippet_144()
*****************How to select model using Grid Search in Python******************

LogisticRegression(C=7.742636826811269, class_weight=None, dual=False,
          fit_intercept=True, intercept_scaling=1, max_iter=100,
          multi_class='warn', n_jobs=None, penalty='l1', random_state=None,
          solver='warn', tol=0.0001, verbose=0, warm_start=False)