Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Support multi-objective study for from_optuna (#216)
Browse files Browse the repository at this point in the history
* clarify the supported study type

* revert docs change and support multi-objective

* add test for from_optuna with multi-objective value

* fix returned type
  • Loading branch information
nzw0301 authored Oct 21, 2021
1 parent 79b3d52 commit 4bd678e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
12 changes: 10 additions & 2 deletions hiplot/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# LICENSE file in the root directory of this source tree.

import csv
import enum
import uuid
import json
import warnings
Expand Down Expand Up @@ -514,11 +515,18 @@ def from_optuna(study: "optuna.study.Study") -> "Experiment": # No type hint to

# Create a list of dictionary objects using study trials
# All parameters are taken using params.copy()

hyper_opt_data = []
for each_trial in study.trials:
trial_params = {}
trial_params["value"] = each_trial.value # name = value, as it could be RMSE / accuracy, or any value that the user selects for tuning
num_objectives = len(each_trial.values)

if num_objectives == 1:
trial_params["value"] = each_trial.value # name = value, as it could be RMSE / accuracy, or any value that the user selects for tuning
else:
for objective_id, value in enumerate(each_trial.values):
trial_params[f"value_{objective_id}"] = value

trial_params["uid"] = each_trial.number
trial_params.update(each_trial.params.copy())
hyper_opt_data.append(trial_params)
Expand Down
20 changes: 20 additions & 0 deletions hiplot/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ def objective(trial: "optuna.trial.Trial") -> float:
xp._asdict()


def test_from_optuna_multi_objective() -> None:

def objective(trial: "optuna.trial.Trial") -> tp.Tuple[float, float]:
x = trial.suggest_float("x", -1, 1)
y = trial.suggest_float("y", -1, 1)
return x ** 2, y

study = optuna.create_study(directions=["minimize", "minimize"])
study.optimize(objective, n_trials=3)

# Create a dataframe from the study.
df = study.trials_dataframe()
assert isinstance(df, pd.DataFrame)
assert df.shape[0] == 3 # n_trials.
xp = hip.Experiment.from_optuna(study)
assert len(xp.datapoints) == 3
xp.validate()
xp._asdict()


def test_from_dataframe_nan_values() -> None:
# Pandas automatically convert numeric-based columns None to NaN in dataframes
# Pandas will also automatically convert columns with NaN from integer to floats, since NaN is considered a float
Expand Down

0 comments on commit 4bd678e

Please sign in to comment.