Analysis of hyperparameter search results

Analysis of hyperparameter search results#

In the previous notebook we showed how to implement a randomized search fortuning the hyperparameters of aHistGradientBoostingClassifier to fit theadult_census dataset. In practice, a randomized hyperparameter search isusually run with a large number of iterations.

In order to avoid the computational cost and still make a decent analysis, weload the results obtained from a similar search with 500 iterations.

importpandasaspdcv_results=pd.read_csv("../figures/randomized_search_results.csv",index_col=0)cv_results
mean_fit_timestd_fit_timemean_score_timestd_score_timeparam_classifier__l2_regularizationparam_classifier__learning_rateparam_classifier__max_binsparam_classifier__max_leaf_nodesparam_classifier__min_samples_leafparamssplit0_test_scoresplit1_test_scoresplit2_test_scoresplit3_test_scoresplit4_test_scoremean_test_scorestd_test_scorerank_test_score
00.5404560.0627250.0520690.0026612.4670470.55007586226{'classifier__l2_regularization': 2.4670474863...0.8565580.8622710.8577670.8544910.8566750.8575520.00258648
11.1105360.0334030.0741420.0021650.0154490.00114660191{'classifier__l2_regularization': 0.0154488709...0.7589740.7589410.7589410.7589410.7589410.7589470.000013323
21.1374840.0531500.0929930.0290051.0950930.0042741511710{'classifier__l2_regularization': 1.0950934559...0.7832670.7589410.7764130.7791430.7589410.7713410.010357311
33.9351080.2029930.1181050.0236580.0036210.0013051816437{'classifier__l2_regularization': 0.0036210968...0.7589740.7589410.7589410.7589410.7589410.7589470.000013323
40.2552190.0383010.0560480.0167360.0000815.4073829783{'classifier__l2_regularization': 8.1060737427...0.7589740.7589410.7589410.7589410.7589410.7589470.000013323
.........................................................
4950.4524110.0230060.0555630.0008460.0000750.36437392174{'classifier__l2_regularization': 7.4813767874...0.8583320.8650010.8626810.8603600.8607700.8614290.00225834
4961.1330420.0144560.0781860.0021995.0659460.0012227171{'classifier__l2_regularization': 5.0659455480...0.7589740.7589410.7589410.7589410.7589410.7589470.000013323
4970.9118280.0171670.0765630.0051302.4600250.0444081677{'classifier__l2_regularization': 2.4600250010...0.8399070.8497130.8468470.8460280.8443900.8453770.003234140
4981.1681200.1218190.0612830.0007600.0000680.2879042271465{'classifier__l2_regularization': 6.7755366885...0.8618810.8650010.8624080.8599510.8618620.8622210.00162333
4990.8237740.1206860.0603510.0149580.4452180.00511219819{'classifier__l2_regularization': 0.4452178932...0.7645690.7659020.7659020.7649470.7650830.7652810.000535319

500 rows × 18 columns

We define a function to remove the prefixes in the hyperparameters columnnames.

defshorten_param(param_name):if"__"inparam_name:returnparam_name.rsplit("__",1)[1]returnparam_namecv_results=cv_results.rename(shorten_param,axis=1)cv_results
mean_fit_timestd_fit_timemean_score_timestd_score_timel2_regularizationlearning_ratemax_binsmax_leaf_nodesmin_samples_leafparamssplit0_test_scoresplit1_test_scoresplit2_test_scoresplit3_test_scoresplit4_test_scoremean_test_scorestd_test_scorerank_test_score
00.5404560.0627250.0520690.0026612.4670470.55007586226{'classifier__l2_regularization': 2.4670474863...0.8565580.8622710.8577670.8544910.8566750.8575520.00258648
11.1105360.0334030.0741420.0021650.0154490.00114660191{'classifier__l2_regularization': 0.0154488709...0.7589740.7589410.7589410.7589410.7589410.7589470.000013323
21.1374840.0531500.0929930.0290051.0950930.0042741511710{'classifier__l2_regularization': 1.0950934559...0.7832670.7589410.7764130.7791430.7589410.7713410.010357311
33.9351080.2029930.1181050.0236580.0036210.0013051816437{'classifier__l2_regularization': 0.0036210968...0.7589740.7589410.7589410.7589410.7589410.7589470.000013323
40.2552190.0383010.0560480.0167360.0000815.4073829783{'classifier__l2_regularization': 8.1060737427...0.7589740.7589410.7589410.7589410.7589410.7589470.000013323
.........................................................
4950.4524110.0230060.0555630.0008460.0000750.36437392174{'classifier__l2_regularization': 7.4813767874...0.8583320.8650010.8626810.8603600.8607700.8614290.00225834
4961.1330420.0144560.0781860.0021995.0659460.0012227171{'classifier__l2_regularization': 5.0659455480...0.7589740.7589410.7589410.7589410.7589410.7589470.000013323
4970.9118280.0171670.0765630.0051302.4600250.0444081677{'classifier__l2_regularization': 2.4600250010...0.8399070.8497130.8468470.8460280.8443900.8453770.003234140
4981.1681200.1218190.0612830.0007600.0000680.2879042271465{'classifier__l2_regularization': 6.7755366885...0.8618810.8650010.8624080.8599510.8618620.8622210.00162333
4990.8237740.1206860.0603510.0149580.4452180.00511219819{'classifier__l2_regularization': 0.4452178932...0.7645690.7659020.7659020.7649470.7650830.7652810.000535319

500 rows × 18 columns

As we have more than 2 parameters in our randomized-search, we cannotvisualize the results using a heatmap. We could still do it pair-wise, buthaving a two-dimensional projection of a multi-dimensional problem can lead toa wrong interpretation of the scores.

importseabornassnsimportnumpyasnpdf=pd.DataFrame({"max_leaf_nodes":cv_results["max_leaf_nodes"],"learning_rate":cv_results["learning_rate"],"score_bin":pd.cut(cv_results["mean_test_score"],bins=np.linspace(0.5,1.0,6)),})sns.set_palette("YlGnBu_r")ax=sns.scatterplot(data=df,x="max_leaf_nodes",y="learning_rate",hue="score_bin",s=50,color="k",edgecolor=None,)ax.set_xscale("log")ax.set_yscale("log")_=ax.legend(title="mean_test_score",loc="center left",bbox_to_anchor=(1,0.5))
../_images/484f5991e5626873f754ce893a9a0be8ad17e0aebfe11bff4ca5201acd7ce5c2.png

In the previous plot we see that the top performing values are located in aband of learning rate between 0.01 and 1.0, but we have no control in how theother hyperparameters interact with such values for the learning rate.Instead, we can visualize all the hyperparameters at the same time using aparallel coordinates plot.

importnumpyasnpimportplotly.expressaspxfig=px.parallel_coordinates(cv_results.rename(shorten_param,axis=1).apply({"learning_rate":np.log10,"max_leaf_nodes":np.log2,"max_bins":np.log2,"min_samples_leaf":np.log10,"l2_regularization":np.log10,"mean_test_score":lambdax:x,}),color="mean_test_score",color_continuous_scale=px.colors.sequential.Viridis,)fig.show()

Note

Wetransformed most axis values by taking a log10 or log2 tospread the active ranges and improve the readability of the plot.

The parallel coordinates plot displays the values of the hyperparameters ondifferent columns while the performance metric is color coded. Thus, we areable to quickly inspect if there is a range of hyperparameters which isworking or not.

It is possible toselect a range of results by clicking and holding on anyaxis of the parallel coordinate plot. You can then slide (move) the rangeselection and cross two selections to see the intersections. You can undo aselection by clicking once again on the same axis.

In particular for this hyperparameter search, it is interesting to confirmthat the yellow lines (top performing models) all reach intermediate valuesfor the learning rate, that is, tick values between -2 and 0 which correspondto learning rate values of 0.01 to 1.0 once we invert back the log10 transformfor that axis.

But now we can also observe that it is not possible to select the highestperforming models by selecting lines of on themax_bins axis with tickvalues between 1 and 3.

The other hyperparameters are not very sensitive. We can check that if weselect thelearning_rate axis tick values between -1.5 and -0.5 andmax_bins tick values between 5 and 8, we always select top performingmodels, whatever the values of the other hyperparameters.

In this notebook, we saw how to interactively explore the results of a largerandomized search with multiple interacting hyperparameters. In particular weobserved that some hyperparameters have very little impact on thecross-validation score, while others have to be adjusted within a specificrange to get models with good predictive accuracy.