Lightweight, versatile, and platform agnostic architecture

Optuna is entirely written in Python and has few dependencies. This means that we can quickly move to the real example once you get interested in Optuna.

Quadratic Function Example

Usually, Optuna is used to optimize hyperparameters, but as an example, let’s optimize a simple quadratic function: \((x - 2)^2\).

First of all, import optuna.

import optuna

In optuna, conventionally functions to be optimized are named objective.

def objective(trial):
    x = trial.suggest_float("x", -10, 10)
    return (x - 2) ** 2

This function returns the value of \((x - 2)^2\). Our goal is to find the value of x that minimizes the output of the objective function. This is the “optimization.” During the optimization, Optuna repeatedly calls and evaluates the objective function with different values of x.

A Trial object corresponds to a single execution of the objective function and is internally instantiated upon each invocation of the function.

The suggest APIs (for example, suggest_float()) are called inside the objective function to obtain parameters for a trial. suggest_float() selects parameters uniformly within the range provided. In our example, from \(-10\) to \(10\).

To start the optimization, we create a study object and pass the objective function to method optimize() as follows.

study = optuna.create_study()
study.optimize(objective, n_trials=100)

You can get the best parameter as follows.

best_params = study.best_params
found_x = best_params["x"]
print("Found x: {}, (x - 2)^2: {}".format(found_x, (found_x - 2) ** 2))

Out:

Found x: 1.9826151008093253, (x - 2)^2: 0.00030223471986992015

We can see that the x value found by Optuna is close to the optimal value of 2.

Note

When used to search for hyperparameters in machine learning, usually the objective function would return the loss or accuracy of the model.

Study Object

Let us clarify the terminology in Optuna as follows:

  • Trial: A single call of the objective function

  • Study: An optimization session, which is a set of trials

  • Parameter: A variable whose value is to be optimized, such as x in the above example

In Optuna, we use the study object to manage optimization. Method create_study() returns a study object. A study object has useful properties for analyzing the optimization outcome.

To get the dictionary of parameter name and parameter values:

Out:

{'x': 1.9826151008093253}

To get the best observed value of the objective function:

study.best_value

Out:

0.00030223471986992015

To get the best trial:

study.best_trial

Out:

FrozenTrial(number=45, values=[0.00030223471986992015], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 903415), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 906403), params={'x': 1.9826151008093253}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=45, state=TrialState.COMPLETE, value=None)

To get all trials:

study.trials

Out:

[FrozenTrial(number=0, values=[4.622525635654521], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 780737), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 781073), params={'x': -0.1500059617718552}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=0, state=TrialState.COMPLETE, value=None), FrozenTrial(number=1, values=[125.37959480405107], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 781514), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 781789), params={'x': -9.1973030147465}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=1, state=TrialState.COMPLETE, value=None), FrozenTrial(number=2, values=[10.82734895735902], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 782184), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 782426), params={'x': 5.29049372547024}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=2, state=TrialState.COMPLETE, value=None), FrozenTrial(number=3, values=[54.3163587988606], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 782808), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 783045), params={'x': 9.36996328341333}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=3, state=TrialState.COMPLETE, value=None), FrozenTrial(number=4, values=[0.642444520889234], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 783426), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 783664), params={'x': 2.801526369428501}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=4, state=TrialState.COMPLETE, value=None), FrozenTrial(number=5, values=[37.66468549436966], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 784054), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 784298), params={'x': -4.13715614062162}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=5, state=TrialState.COMPLETE, value=None), FrozenTrial(number=6, values=[105.47085037183489], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 784677), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 784919), params={'x': -8.269900212360142}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=6, state=TrialState.COMPLETE, value=None), FrozenTrial(number=7, values=[6.664274809132924], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 785300), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 785541), params={'x': 4.581525674699542}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=7, state=TrialState.COMPLETE, value=None), FrozenTrial(number=8, values=[2.142438683142781], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 785919), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 786166), params={'x': 3.463707171241154}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=8, state=TrialState.COMPLETE, value=None), FrozenTrial(number=9, values=[16.460785274712578], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 786540), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 786785), params={'x': 6.057189331878977}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=9, state=TrialState.COMPLETE, value=None), FrozenTrial(number=10, values=[7.596821813084001], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 787160), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 790594), params={'x': -0.7562332653612613}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=10, state=TrialState.COMPLETE, value=None), FrozenTrial(number=11, values=[0.014830999242841305], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 791046), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 793907), params={'x': 1.8782174099354045}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=11, state=TrialState.COMPLETE, value=None), FrozenTrial(number=12, values=[0.27024089065635454], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 794356), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 797134), params={'x': 1.4801530122657682}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=12, state=TrialState.COMPLETE, value=None), FrozenTrial(number=13, values=[32.78925799317673], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 797569), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 800354), params={'x': -3.7261905306387364}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=13, state=TrialState.COMPLETE, value=None), FrozenTrial(number=14, values=[17.9145792017395], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 800790), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 803680), params={'x': -2.232561777663676}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=14, state=TrialState.COMPLETE, value=None), FrozenTrial(number=15, values=[0.3335906764752217], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 804160), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 807028), params={'x': 1.4224269081101322}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=15, state=TrialState.COMPLETE, value=None), FrozenTrial(number=16, values=[29.88156582935929], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 807464), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 810276), params={'x': 7.466403372360961}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=16, state=TrialState.COMPLETE, value=None), FrozenTrial(number=17, values=[65.15653631594563], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 810713), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 813520), params={'x': -6.07195988071953}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=17, state=TrialState.COMPLETE, value=None), FrozenTrial(number=18, values=[0.8580487706648771], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 813957), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 816769), params={'x': 1.0736907802116633}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=18, state=TrialState.COMPLETE, value=None), FrozenTrial(number=19, values=[12.036115618913025], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 817211), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 820026), params={'x': -1.4693105394174537}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=19, state=TrialState.COMPLETE, value=None), FrozenTrial(number=20, values=[0.08983174503737067], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 820465), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 823236), params={'x': 2.2997194438760533}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=20, state=TrialState.COMPLETE, value=None), FrozenTrial(number=21, values=[0.02523986947106314], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 823676), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 826502), params={'x': 2.1588706060637497}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=21, state=TrialState.COMPLETE, value=None), FrozenTrial(number=22, values=[0.8252945159447451], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 826941), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 829734), params={'x': 2.9084572174542647}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=22, state=TrialState.COMPLETE, value=None), FrozenTrial(number=23, values=[32.660740071065355], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 830174), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 832987), params={'x': 7.7149575738639875}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=23, state=TrialState.COMPLETE, value=None), FrozenTrial(number=24, values=[4.6250794708678775], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 833451), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 836250), params={'x': 4.150599793282766}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=24, state=TrialState.COMPLETE, value=None), FrozenTrial(number=25, values=[0.600867544369816], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 836713), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 839478), params={'x': 1.2248435355556815}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=25, state=TrialState.COMPLETE, value=None), FrozenTrial(number=26, values=[22.500224613247493], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 839956), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 842732), params={'x': -2.743440166508637}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=26, state=TrialState.COMPLETE, value=None), FrozenTrial(number=27, values=[20.856047483645035], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 843171), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 845999), params={'x': 6.566842178534861}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=27, state=TrialState.COMPLETE, value=None), FrozenTrial(number=28, values=[3.4678917497941883], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 846439), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 849317), params={'x': 0.13777236896393674}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=28, state=TrialState.COMPLETE, value=None), FrozenTrial(number=29, values=[0.2799079606847535], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 849777), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 852613), params={'x': 2.529063286086602}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=29, state=TrialState.COMPLETE, value=None), FrozenTrial(number=30, values=[7.149044312476836], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 853077), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 855944), params={'x': -0.6737696820176633}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=30, state=TrialState.COMPLETE, value=None), FrozenTrial(number=31, values=[0.2094562641379605], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 856423), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 859274), params={'x': 1.5423360794884957}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=31, state=TrialState.COMPLETE, value=None), FrozenTrial(number=32, values=[3.840182817316717], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 859735), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 862615), params={'x': 0.040361559543006686}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=32, state=TrialState.COMPLETE, value=None), FrozenTrial(number=33, values=[7.684562491093895], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 863093), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 865967), params={'x': 4.772104343471561}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=33, state=TrialState.COMPLETE, value=None), FrozenTrial(number=34, values=[0.08648497708881706], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 866432), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 869307), params={'x': 2.2940832825728403}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=34, state=TrialState.COMPLETE, value=None), FrozenTrial(number=35, values=[2.7802804569361], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 869776), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 872739), params={'x': 3.667417301378422}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=35, state=TrialState.COMPLETE, value=None), FrozenTrial(number=36, values=[0.2914099973497543], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 873206), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 876097), params={'x': 2.539824042952659}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=36, state=TrialState.COMPLETE, value=None), FrozenTrial(number=37, values=[14.478555901057849], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 876568), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 879423), params={'x': 5.805069762968591}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=37, state=TrialState.COMPLETE, value=None), FrozenTrial(number=38, values=[54.14182918825286], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 879889), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 882756), params={'x': 9.358113154080526}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=38, state=TrialState.COMPLETE, value=None), FrozenTrial(number=39, values=[3.39012356727366], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 883228), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 886125), params={'x': 0.15877118008823787}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=39, state=TrialState.COMPLETE, value=None), FrozenTrial(number=40, values=[9.140191482408541], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 886595), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 889500), params={'x': 5.0232749597759945}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=40, state=TrialState.COMPLETE, value=None), FrozenTrial(number=41, values=[0.022374841150278862], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 889969), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 892860), params={'x': 2.1495822220395153}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=41, state=TrialState.COMPLETE, value=None), FrozenTrial(number=42, values=[0.10128190202151761], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 893329), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 896263), params={'x': 2.3182481767764234}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=42, state=TrialState.COMPLETE, value=None), FrozenTrial(number=43, values=[2.4113059607587966], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 896732), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 899602), params={'x': 3.552838034296815}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=43, state=TrialState.COMPLETE, value=None), FrozenTrial(number=44, values=[1.8724646867162191], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 900078), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 902946), params={'x': 0.6316196849135036}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=44, state=TrialState.COMPLETE, value=None), FrozenTrial(number=45, values=[0.00030223471986992015], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 903415), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 906403), params={'x': 1.9826151008093253}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=45, state=TrialState.COMPLETE, value=None), FrozenTrial(number=46, values=[10.459573460936912], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 906874), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 909771), params={'x': -1.2341263829567501}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=46, state=TrialState.COMPLETE, value=None), FrozenTrial(number=47, values=[4.532722059110264], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 910242), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 913140), params={'x': 4.129019036812556}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=47, state=TrialState.COMPLETE, value=None), FrozenTrial(number=48, values=[0.0017846812970982952], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 913611), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 916536), params={'x': 2.0422454884821835}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=48, state=TrialState.COMPLETE, value=None), FrozenTrial(number=49, values=[137.37649743779068], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 917006), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 919920), params={'x': -9.720772049561866}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=49, state=TrialState.COMPLETE, value=None), FrozenTrial(number=50, values=[17.59138062842823], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 920408), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 923318), params={'x': -2.1942079858333474}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=50, state=TrialState.COMPLETE, value=None), FrozenTrial(number=51, values=[0.007003629721844669], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 923790), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 926896), params={'x': 1.9163123084208635}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=51, state=TrialState.COMPLETE, value=None), FrozenTrial(number=52, values=[0.15101361038309288], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 927370), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 930301), params={'x': 1.6113953031896129}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=52, state=TrialState.COMPLETE, value=None), FrozenTrial(number=53, values=[1.152663221206002], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 930776), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 933663), params={'x': 3.073621544682297}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=53, state=TrialState.COMPLETE, value=None), FrozenTrial(number=54, values=[1.6603951553023883], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 934144), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 937058), params={'x': 0.7114367864546232}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=54, state=TrialState.COMPLETE, value=None), FrozenTrial(number=55, values=[7.66121991319164], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 937533), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 940465), params={'x': -0.7678908781221199}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=55, state=TrialState.COMPLETE, value=None), FrozenTrial(number=56, values=[0.031996395324460476], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 940939), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 943859), params={'x': 1.8211246374581997}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=56, state=TrialState.COMPLETE, value=None), FrozenTrial(number=57, values=[1.259940403011692], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 944358), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 947238), params={'x': 3.122470669109751}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=57, state=TrialState.COMPLETE, value=None), FrozenTrial(number=58, values=[1.813660615557408], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 947712), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 950832), params={'x': 0.6532778254007221}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=58, state=TrialState.COMPLETE, value=None), FrozenTrial(number=59, values=[4.2504116674953485], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 951309), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 954264), params={'x': 4.061652654424442}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=59, state=TrialState.COMPLETE, value=None), FrozenTrial(number=60, values=[63.92437123943474], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 954745), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 957666), params={'x': -5.995271805225557}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=60, state=TrialState.COMPLETE, value=None), FrozenTrial(number=61, values=[0.05178838918434056], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 958142), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 961082), params={'x': 1.772429375392296}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=61, state=TrialState.COMPLETE, value=None), FrozenTrial(number=62, values=[0.028507812146971935], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 961559), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 964539), params={'x': 1.831157433841546}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=62, state=TrialState.COMPLETE, value=None), FrozenTrial(number=63, values=[1.0444596886876985], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 965016), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 967951), params={'x': 0.9780118940576175}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=63, state=TrialState.COMPLETE, value=None), FrozenTrial(number=64, values=[5.065259172885068], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 968445), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 971373), params={'x': -0.2506130660078085}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=64, state=TrialState.COMPLETE, value=None), FrozenTrial(number=65, values=[1.9953317518842815], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 971851), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 974829), params={'x': 3.4125621231946868}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=65, state=TrialState.COMPLETE, value=None), FrozenTrial(number=66, values=[10.69481555468029], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 975308), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 978254), params={'x': 5.270292885152688}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=66, state=TrialState.COMPLETE, value=None), FrozenTrial(number=67, values=[0.004645773032712577], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 978734), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 981701), params={'x': 2.068159907810329}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=67, state=TrialState.COMPLETE, value=None), FrozenTrial(number=68, values=[5.731180002418792], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 982180), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 985162), params={'x': 4.393988304570177}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=68, state=TrialState.COMPLETE, value=None), FrozenTrial(number=69, values=[0.3708006981144697], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 985640), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 989334), params={'x': 2.608934067132452}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=69, state=TrialState.COMPLETE, value=None), FrozenTrial(number=70, values=[0.8575747442514786], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 989756), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 992735), params={'x': 1.0739466839044964}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=70, state=TrialState.COMPLETE, value=None), FrozenTrial(number=71, values=[0.009540934341449397], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 993212), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 996231), params={'x': 2.0976777064710745}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=71, state=TrialState.COMPLETE, value=None), FrozenTrial(number=72, values=[0.04733006659202077], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 39, 996707), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 39, 999649), params={'x': 2.2175547438968426}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=72, state=TrialState.COMPLETE, value=None), FrozenTrial(number=73, values=[2.696860424303799], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 142), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 3091), params={'x': 0.3577879478265301}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=73, state=TrialState.COMPLETE, value=None), FrozenTrial(number=74, values=[2.9424458870569876], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 3571), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 6569), params={'x': 3.715355906818462}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=74, state=TrialState.COMPLETE, value=None), FrozenTrial(number=75, values=[0.8061905087282698], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 7052), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 10118), params={'x': 2.897881121712819}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=75, state=TrialState.COMPLETE, value=None), FrozenTrial(number=76, values=[0.7500739954897269], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 10600), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 13580), params={'x': 1.133931875953325}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=76, state=TrialState.COMPLETE, value=None), FrozenTrial(number=77, values=[0.042796934492297406], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 14062), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 17050), params={'x': 2.2068741996777206}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=77, state=TrialState.COMPLETE, value=None), FrozenTrial(number=78, values=[5.206004867767515], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 17535), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 20532), params={'x': -0.2816671246629108}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=78, state=TrialState.COMPLETE, value=None), FrozenTrial(number=79, values=[1.2464364001455208], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 21016), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 23999), params={'x': 3.1164391609691595}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=79, state=TrialState.COMPLETE, value=None), FrozenTrial(number=80, values=[15.084639063593848], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 24492), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 27464), params={'x': 5.8838948316855655}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=80, state=TrialState.COMPLETE, value=None), FrozenTrial(number=81, values=[0.4443489087070769], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 27960), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 31001), params={'x': 1.3334049889872586}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=81, state=TrialState.COMPLETE, value=None), FrozenTrial(number=82, values=[0.0011076003716708467], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 31483), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 34500), params={'x': 2.033280630578023}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=82, state=TrialState.COMPLETE, value=None), FrozenTrial(number=83, values=[0.0004794865334998025], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 34985), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 38000), params={'x': 2.021897180948693}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=83, state=TrialState.COMPLETE, value=None), FrozenTrial(number=84, values=[0.4233634909782312], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 38483), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 41488), params={'x': 2.650663884796314}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=84, state=TrialState.COMPLETE, value=None), FrozenTrial(number=85, values=[0.01819803164986583], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 41971), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 44999), params={'x': 1.8650999197558955}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=85, state=TrialState.COMPLETE, value=None), FrozenTrial(number=86, values=[0.20404681552283285], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 45486), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 48504), params={'x': 1.548284585692681}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=86, state=TrialState.COMPLETE, value=None), FrozenTrial(number=87, values=[2.383956554998908], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 48991), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 51988), params={'x': 0.45599334360278476}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=87, state=TrialState.COMPLETE, value=None), FrozenTrial(number=88, values=[10.485983947034226], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 52472), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 55493), params={'x': -1.2382069030613572}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=88, state=TrialState.COMPLETE, value=None), FrozenTrial(number=89, values=[3.5089049236401566], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 55992), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 58982), params={'x': 3.87320712246141}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=89, state=TrialState.COMPLETE, value=None), FrozenTrial(number=90, values=[6.390007628378404], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 59472), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 62500), params={'x': -0.5278464408223857}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=90, state=TrialState.COMPLETE, value=None), FrozenTrial(number=91, values=[0.007612444074436744], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 62938), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 66043), params={'x': 1.9127506786591624}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=91, state=TrialState.COMPLETE, value=None), FrozenTrial(number=92, values=[0.0027324153759265015], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 66483), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 69556), params={'x': 1.9477274892900054}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=92, state=TrialState.COMPLETE, value=None), FrozenTrial(number=93, values=[1.6578272363959687], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 69998), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 73058), params={'x': 3.287566400771614}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=93, state=TrialState.COMPLETE, value=None), FrozenTrial(number=94, values=[1.1181457813294902], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 73500), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 76588), params={'x': 0.9425758744337775}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=94, state=TrialState.COMPLETE, value=None), FrozenTrial(number=95, values=[7.140266458143628], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 77029), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 80102), params={'x': 4.672127702439318}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=95, state=TrialState.COMPLETE, value=None), FrozenTrial(number=96, values=[0.7010203798641512], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 80540), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 83606), params={'x': 2.8372695980770777}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=96, state=TrialState.COMPLETE, value=None), FrozenTrial(number=97, values=[0.3315584842363145], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 84062), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 87124), params={'x': 1.4241888467246275}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=97, state=TrialState.COMPLETE, value=None), FrozenTrial(number=98, values=[0.02053247223552032], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 87562), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 90644), params={'x': 1.856708436272332}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=98, state=TrialState.COMPLETE, value=None), FrozenTrial(number=99, values=[3.0222891862430687], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 40, 91085), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 40, 94199), params={'x': 0.26152676573866435}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=99, state=TrialState.COMPLETE, value=None)]

To get the number of trials:

len(study.trials)

Out:

100

By executing optimize() again, we can continue the optimization.

study.optimize(objective, n_trials=100)

To get the updated number of trials:

len(study.trials)

Out:

200

As the objective function is so easy that the last 100 trials don’t improve the result. However, we can check the result again:

best_params = study.best_params
found_x = best_params["x"]
print("Found x: {}, (x - 2)^2: {}".format(found_x, (found_x - 2) ** 2))

Out:

Found x: 1.9965209047117898, (x - 2)^2: 1.2104104024446561e-05

Total running time of the script: ( 0 minutes 0.709 seconds)

Gallery generated by Sphinx-Gallery