Using a grid search optimization model parameters

1. Simple grid search method

  • Lasso algorithm to adjust the number of different parameters

############################# using a grid search optimization model parameters ############## ######################### 
# import lasso regression model 
from sklearn.linear_model import lasso 
# import splitting tool data set 
from sklearn.model_selection import train_test_split 
# import wine data set 
from sklearn.datasets import load_wine 
# Loading data set wine 
wine load_wine = () 
# difference data set is divided into a training set and test set 
X_train, X_test, y_train, y_test = train_test_split (wine.data, wine.target , = 38 is random_state) 
# set as the initial score 0 
BEST_SCORE = 0 
# set parameter alpha traversing .01, .1 
for alpha in [0.01,0.1,1.0,10.0]: 
    # traversal 100,1000,5000 maximum number of iterations, 10000 
    for max_iter in [100,1000,5000,10000]: 
        lasso lasso = (Alpha = Alpha, max_iter = max_iter) 
        # lasso regression model training
        lasso.fit (X_train, y_train) 
        Score = lasso.score (X_test, android.permission.FACTOR.) 
        # make the best score of all scores in the highest value 
        IF Score> BEST_SCORE: 
            BEST_SCORE = Score 
            # dictionary definitions, parameters and returns the best best maximum number of iterations 
            best_parameters = { 'alpha': alpha , ' maximum number of iterations': max_iter} 

# print result 
print (' model maximum score: {} :. 3F '. the format (BEST_SCORE)) 
print (' optimal parameter settings: {} '. format (best_parameters) )
Model maximum score: 0.889 
optimal parameter set: { 'alpha': 0.01, ' maximum number of iterations': 100}
# Difference data set is divided into a training set and test set 
X_train, X_test, y_train, android.permission.FACTOR. = Train_test_split (wine.data, wine.target, random_state = 0) 
# set as the initial score 0 
BEST_SCORE = 0 
# Set parameter alpha of 0.01, 0.1 traversal , 1, 10 
for Alpha in [0.01,0.1,1.0,10.0]: 
    # The maximum number of iterations to traverse 100,1000,5000,10000 
    for max_iter in [100,1000,5000,10000]: 
        Lasso Lasso = (Alpha = Alpha, = max_iter max_iter) 
        # training lasso regression model 
        lasso.fit (X_train, y_train) 
        score = lasso.score (X_test, android.permission.FACTOR.) 
        # make the best score of the highest scores of all 
        IF score> BEST_SCORE: 
            BEST_SCORE = score 
            # define dictionary, returns the optimal parameters and the optimum maximum number of iterations 
            best_parameters = { 'alpha': alpha , ' maximum number of iterations': max_iter} 

# print result
print ( 'Model maximum score: {} :. 3F'. the format (BEST_SCORE)) 
print ( 'optimal parameter set: {}'. format (best_parameters ))
Model maximum score: 0.830 
optimal parameter set: { 'alpha': 0.1, ' the maximum number of iterations': 100}

2. the cross-validation bound grid search

# Import numpy 
Import numpy NP AS 
# cross validation tool introduced 
from sklearn.model_selection Import cross_val_score 
# alpha parameter set traversing .01, .1 
for alpha in [0.01,0.1,1.0,10.0]: 
    # 100 traverse the maximum number of iterations, 1000,5000,10000 
    for max_iter in [100,1000,5000,10000]: 
        lasso lasso = (Alpha = Alpha, max_iter = max_iter) 
        # training lasso regression model 
        lasso.fit (X_train, y_train) 
        Scores = cross_val_score (lasso, X_train, y_train, CV =. 6) 
        Score = np.mean (Scores) 
        IF Score> BEST_SCORE: 
            BEST_SCORE = Score 
            # dictionary definitions, returns the optimal parameters and the optimum maximum number of iterations 
            best_parameters = { 'alpha': alpha , ' maximum iteration times': max_iter} 

# print result
print ( 'Model maximum score: {} :. 3F'. the format (BEST_SCORE)) 
print ( 'optimal parameter set: {}'. format (best_parameters ))
Model maximum score: 0.865 
optimal parameter set: { 'alpha': 0.01, ' the maximum number of iterations': 100}
# Model fits the data with the optimum parameters 
Lasso = Lasso (Alpha = 0.01, max_iter = 100) .fit (X_train, y_train) 
# print the test score data set 
print ( 'test score data set: {:. 3f}'. Format (lasso.score (X_test, y_test)) )
Test data set score: 0.819
# Guiding grid search tool 
from sklearn.model_selection Import GridSearchCV 
# define the parameters required to traverse the dictionary 
params = { 'alpha': [ 0.01,0.1,1.0,10.0], 'max_iter': [100,1000,5000,10000 ]} 
model # definitions and parameters used in the search grid 
grid_search = GridSearchCV (Lasso, the params, CV =. 6, IID = False) 
# model fits the data using a grid search 
grid_search.fit (X_train, y_train) 
# print result 
print ( 'model highest score: {} :. 3F' the format (grid_search.score (X_test, android.permission.FACTOR.)).) 
Print ( 'most parameters: {}'. format (grid_search.best_params_ ))
Model highest score: 0.819 
most parameters: { 'alpha': 0.01, 'max_iter': 100}
# Print best_score_ properties grid search of 
print ( 'cross-validation highest score: {:. 3f}' format ( grid_search.best_score_).)
Cross-validation highest score: 0.865

to sum up:  

  • The method itself is GridSearchCV grid search packaged together and cross-validation.
  • Although GridSearchCV is a very powerful feature, but due to the need to repeatedly modeling, the calculation time required for longer.

 

Quoted from the article: "layman's language python machine learning"

Guess you like

Origin www.cnblogs.com/weijiazheng/p/10966005.html