Learn how to use a grid search to find optimal hyperparameters for training.
- [Instructor] Two common problems in machines learning models are over fitting and under fitting. We can often fix either problem by adjusting the hyperparameters on our model. The problem is that machine learning models have lots of hyperparameters to adjust. Often the best way to find the best settings is just through trial and error, but it can take a lot of work to try all the possible combinations. Let's open up train_model.py. When we create our model, we pass in these parameters. We have six different parameters here that we can tune and most of these parameters accept any number, so we literally have an infinite number of combinations we could try.
A solution for this problem is to use a grid search. A grid search is where you list out a range of settings you want to try for each parameter, and you literally try them all. You train and test the model for every combination of parameters. The combination of parameters that generates the best predictions are the set of parameters you should use for your real model. Luckily, scikit-learn totally automates this process. Let's open up grid_search.py. This is almost exactly the same code as in train_model.py. The first difference is that we declare our model without passing in any parameters.
Instead, we have a param grid below. The param grid has an array for each parameter. For each setting, we add the range of values that we want to try. The ranges we have here are good values to try for most problems. A good strategy is to try a few values for each parameter, where it increases or decreases by a significant amount, like 1.0 to 0.3 to 0.1, like we have here. There's not much point in trying values that are very close, like 1.0 to 0.95, since the results probably won't be that much different.
Next, define the grid search using the grid search CV function. This takes in the model object, the param grid, and the number of CPUs we want to use to run our grid search. If you have a computer with more than one CPU, you can speed things up by using all of them. Next, we call fit on the grid search object to run the grid search. It's very important that we only pass the training data into the grid search CV function. We don't give it access to our test data set. The CV in grid search CV stands for cross-validation. The function will automatically slice up the training data into smaller subsets and use part of the data for training different models and a different part of the data for testing those models.
This means that the model configuration's done without ever seeing our test data. It keeps our test data totally hidden to make sure we are doing a completely blind test of the final model. Running the grid search will take a long time since it's actually training a model for every possible combination of parameters in the para grid. When it's done training, it will print out the model hyperparameters that work the best. It will also tell us the mean average error for both data sets when using the best parameters.
- Setting up the development environment
- Building a simple home value estimator
- Finding the best weights automatically
- Working with large data sets efficiently
- Training a supervised machine learning model
- Exploring a home value data set
- Deciding how much data is needed
- Preparing the features
- Training the value estimator
- Measuring accuracy with mean absolute error
- Improving a system
- Using the machine learning model to make predictions