Recipe: How to tune Hyper parameters using Random Search in Python?
HYPERPARAMETER TUNING

How to tune Hyper parameters using Random Search in Python?

This recipe helps you tune Hyper parameters using Random Search in Python
In [1]:
## How to tune Hyper-parameters using Random Search in Python
def Snippet_143():
    print()
    print(format('How to tune Hyper-parameters using Random Search in Python','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    from scipy.stats import uniform
    from sklearn import linear_model, datasets
    from sklearn.model_selection import RandomizedSearchCV

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

    # Create logistic regression
    logistic = linear_model.LogisticRegression()

    # Create Hyperparameter Search Space
    # Create regularization penalty space
    penalty = ['l1', 'l2']

    # Create regularization hyperparameter distribution using uniform distribution
    C = uniform(loc=0, scale=4)

    # Create hyperparameter options
    hyperparameters = dict(C=C, penalty=penalty)

    # Create randomized search 5-fold cross validation and 100 iterations
    clf = RandomizedSearchCV(logistic, hyperparameters, random_state=1, n_iter=100,
                             cv=5, verbose=0, n_jobs=-1)

    # Fit randomized search
    best_model = clf.fit(X, y)

    # View best hyperparameters
    print('Best Penalty:', best_model.best_estimator_.get_params()['penalty'])
    print('Best C:', best_model.best_estimator_.get_params()['C'])

Snippet_143()
************How to tune Hyper-parameters using Random Search in Python************
Best Penalty: l1
Best C: 1.668088018810296


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from Dell EMC
1 developer from Jio
1 developer from Amazon
1 developer from HCL
1 developer from Vodafone
1 developer from ANAC
1 developer from HvH
1 developer from Wipro
1 developer from Capita
1 developer from ICU Medical