Automating Hyperparameter Choice
Finding the best hyperparameter of interest without writing hundreds of lines of code for hundreds of models is an important efficiency gain that will greatly assist your future machine learning model building.
An important hyperparameter for the GBM algorithm is the learning rate. But which learning rate is best for this problem? By writing a loop to search through a number of possibilities, collating these and viewing them you can find the best one.
Possible learning rates to try include 0.001, 0.01, 0.05, 0.1, 0.2 and 0.5
You will have available X_train
, X_test
, y_train
& y_test
datasets, and GradientBoostingClassifier
has been imported for you.
This exercise is part of the course
Hyperparameter Tuning in Python
Exercise instructions
- Create a
learning_rates
list for the learning rates, and aresults_list
to hold the accuracy score of your predictions. - Write a loop to create a GBM model for each learning rate mentioned and create predictions for each model.
- Save the learning rate and accuracy score to a
results_list
. - Turn the results list into a DataFrame and print it out.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Set the learning rates & results storage
learning_rates = ____
results_list = ____
# Create the for loop to evaluate model predictions for each learning rate
for learning_rate in ____:
model = ____(learning_rate=____)
predictions = ____.fit(____, ____).predict(____)
# Save the learning rate and accuracy score
results_list.append([____, accuracy_score(y_test, ____)])
# Gather everything into a DataFrame
results_df = pd.DataFrame(____, columns=['learning_rate', 'accuracy'])
print(results_df)