0:00

Grid Search allows us to scan through multiple free parameters with few lines of code.

Â Parameters like the alpha term discussed in

Â the previous video are not part of the fitting or training process.

Â These values are called hyperparameters.

Â Scikit-learn has a means of automatically

Â iterating over these hyperparameters using cross-validation.

Â This method is called Grid Search.

Â Grid Search takes the model or objects you would like to

Â train and different values of the hyperparameters.

Â It then calculates the mean square error or R-squared for various hyperparameter values,

Â allowing you to choose the best values.

Â Let the small circles represent different hyperparameters.

Â We start off with one value for hyperparameters and train the model.

Â We use different hyperparameters to train the model.

Â We continue the process until we have exhausted the different free parameter values.

Â Each model produces an error.

Â We select the hyperparameter that minimizes the error.

Â To select the hyperparameter,

Â we split our dataset into three parts,

Â the training set, validation set, and test set.

Â We train the model for different hyperparameters.

Â We use the R-squared or mean square error for each model.

Â We select the hyperparameter that minimizes the mean squared

Â error or maximizes the R-squared on the validation set.

Â We finally test our model performance using the test data.

Â This is the scikit-learn web page,

Â where the object constructor parameters are given.

Â It should be noted that the attributes of an object are also called parameters.

Â We will not make the distinction even though some of

Â the options are not hyperparameters per se.

Â In this module, we will focus on

Â the hyperparameter alpha and the normalization parameter.

Â The value of your Grid Search is a Python list that contains a Python dictionary.

Â The key is the name of the free parameter.

Â The value of the dictionary is the different values of the free parameter.

Â This can be viewed as a table with various free parameter values.

Â We also have the object or model.

Â The Grid Search takes on the scoring method.

Â In this case, R-squared the number of folds,

Â the model or object,

Â and the free parameter values.

Â Some of the outputs include the different scores for different free parameter values.

Â In this case, the R-squared along with a free parameter values that have the best score.

Â First, we import the libraries we need,

Â including GridSearchCV, the dictionary of parameter values.

Â We create a ridge regression object or model.

Â We then create a GridSearchCV object.

Â The inputs are the ridge regression object,

Â the parameter values, and the number of folds.

Â We will use R-squared.

Â This is the default scoring method. We fit the object.

Â We can find the best values for the free parameters using the attribute best estimator.

Â We can also get information like the mean score on

Â the validation data using the attribute CV result.

Â What are the advantages of Grid Search is how quickly we can test multiple parameters.

Â For example, ridge regression has the option to normalize the data.

Â To see how to standardize, see module four.

Â The term alpha is the first element in the dictionary.

Â The second element is the normalized option.

Â The key is the name of the parameter.

Â The value is the different options in

Â this case because we can either normalize the data or not.

Â The values are True or False respectively.

Â The dictionary is a table or grid that contains two different values.

Â As before, we need the ridge regression object or model.

Â The procedure is similar except that we have

Â a table or grid of different parameter values.

Â The output is the score for all the different combinations of parameter values.

Â The code is also similar.

Â The dictionary contains the different free parameter values.

Â We can find the best value for the free parameters.

Â The resulting scores of the different free parameters are stored in

Â this dictionary, Grid1.cv_results _.

Â We can print out the score for the different free parameter values.

Â The parameter values are stored as shown here.

Â See the course labs for more examples.

Â