1

when I retrieve Hyperopt trials with

my_trial = trials.trials[n]['misc']['vals'] 

I get a dictionary composed by lists each of single elements.

{   'base_score': [0.5],
    'colsample_bylevel': [0.5],
    'colsample_bytree': [0.7000000000000001],
    'learning_rate': [0.2],
    'max_depth': [10.0],
    'min_child': [80.0],
    'min_split_loss': [0.8],
    'n_estimators': [500.0],
    'norm': [1],
    'norm_norm': [2],
    'quant_distr': [],
    'scale': [3],
    'scale_pos_w': [3.1],
    'subsample': [0.8]}

However in order to use space_eval(space, my_trial) I need a dictionary where values are the elements of the lists above, not the lists.

Indeed when I retrieve the best trial with 'best' I obtain

{   'base_score': 0.8,
    'colsample_bylevel': 0.8,
    'colsample_bytree': 0.5,
    'learning_rate': 0.5,
    'max_depth': 11.0,
    'min_child': 90.0,
    'min_split_loss': 0.4,
    'n_estimators': 100.0,
    'norm': 1,
    'norm_norm': 2,
    'scale': 3,
    'scale_pos_w': 7.2,
    'subsample': 0.5}

which is usable to instantiate the corresponding point in 'space' with

point = space_eval(space, my_trial)

I would like to transform the first dictionary into the second, with a simple direct instruction if at all possible

many thanks!

luide
  • 37
  • 6

2 Answers2

5

You can use a dictionary comprehension:

d = {k: v[0] for k, v in d.items() if v}
jpp
  • 159,742
  • 34
  • 281
  • 339
0

You can transform this example by dict comprehension (empty lists will get transformed to None):

my_dict = {   'base_score': [0.5],
    'colsample_bylevel': [0.5],
    'colsample_bytree': [0.7000000000000001],
    'learning_rate': [0.2],
    'max_depth': [10.0],
    'min_child': [80.0],
    'min_split_loss': [0.8],
    'n_estimators': [500.0],
    'norm': [1],
    'norm_norm': [2],
    'quant_distr': [],
    'scale': [3],
    'scale_pos_w': [3.1],
    'subsample': [0.8]}

new_dict = {k:v[0] if len(v) > 0 else None for k, v in my_dict.items()}

print(new_dict)

Outputs:

{'base_score': 0.5, 'colsample_bylevel': 0.5, 'colsample_bytree': 0.7000000000000001, 'learning_rate': 0.2, 'max_depth': 10.0, 'min_child': 80.0, 'min_split_loss': 0.8, 'n_estimators': 500.0, 'norm': 1, 'norm_norm': 2, 'quant_distr': None, 'scale': 3, 'scale_pos_w': 3.1, 'subsample': 0.8}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91