Sub topic 13
Inspecting results and plotting
This chapter assumes you can write an objective function and run a study, as covered in earlier chapters. It also assumes you have scikit-learn or a similar library installed. Plots need Plotly, which you can install with pip install plotly.
After study.optimize() finishes, you need to know which settings won, how the search progressed, and which parameters mattered. Optuna gives you study attributes for the first question and plotting functions for the other two.
Reading results from the study
A study keeps every trial it ran. The most common attributes are:
study.best_valueis the best objective value found.study.best_paramsis a dictionary of the parameters that produced it.study.best_trialis the full trial object, including its number and state.study.trialsis a list of all trials.
For a table of every trial, use study.trials_dataframe(). It returns a pandas DataFrame, so pandas must be installed. Each row is one trial. The columns include number, value, state, and one params_<name> column per parameter.
import optuna
def objective(trial):
x = trial.suggest_float("x", -10, 10)
y = trial.suggest_float("y", -10, 10)
return (x - 2) ** 2 + 0.1 * (y + 1) ** 2
study = optuna.create_study(direction="minimize")
study.optimize(objective, n_trials=50)
print(study.best_params)
print(study.best_value)
df = study.trials_dataframe()
print(df.sort_values("value").head())The state column matters when you use pruning. A trial is COMPLETE, PRUNED or FAIL. Pruned trials have no final value in the usual sense, so filter on state before you compute statistics.
Plotting with optuna.visualization
The optuna.visualization module returns Plotly figures. Call .show() to open one in a browser or notebook.
from optuna.visualization import plot_optimization_history, plot_param_importances
plot_optimization_history(study).show()
plot_param_importances(study).show()The table lists the plots you will use most.
| Function | What it shows |
|---|---|
| `plot_optimization_history` | The value of each trial and the best value so far |
| `plot_param_importances` | Which parameters influenced the objective most |
| `plot_slice` | Objective value against each parameter, one panel per parameter |
| `plot_parallel_coordinate` | How combinations of parameters relate to good values |
| `plot_intermediate_values` | Learning curves reported by `trial.report`, including pruned trials |
Reading the plots
In the optimization history, a curve that is still falling at the last trial suggests more trials could help. A curve that went flat early suggests the search has converged or the space is too small.
Parameter importances are estimated from the completed trials. They need enough trials to be meaningful, so treat them as a rough guide after only a handful of runs. An importance score also depends on the ranges you chose. A parameter with a narrow range can look unimportant simply because you already restricted it.
The slice plot helps you spot a good value that sits at the edge of its range. If the best trials cluster at an upper or lower bound, widen that bound and run again.
Common errors
ImportErrorwhen plotting means Plotly is not installed.plot_param_importancesraises an error if the study has no completed trials, which can happen when every trial was pruned.- Calling
study.best_valueon an empty study raisesValueError. - In a multi-objective study,
best_trialandbest_valueare unavailable. Usestudy.best_trialsinstead.
Exercise
After a run, the slice plot shows that the best trials for learning_rate all sit at the upper bound of its range. What should you do, and why?
Show answer
Widen the upper bound and run more trials. Best results at the edge of a range suggest the true optimum may lie outside the range you searched, so the current search space is limiting the result.