Skip to content

Commit 361057a

Browse files
0.13.4
model_default fixed: get_one_core_model_from_X accepts new parameter "default", if a default core model is used
1 parent 357aaf8 commit 361057a

2 files changed

Lines changed: 69 additions & 46 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotPython"
10-
version = "0.13.3"
10+
version = "0.13.4"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/hyperparameters/values.py

Lines changed: 68 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44
from sklearn.pipeline import make_pipeline
55
from river import compose
66
from typing import Union, List, Dict, Generator, Any
7-
87
from spotPython.utils.convert import class_for_name
98
from spotPython.utils.transform import transform_hyper_parameter_values
109

1110

1211
def generate_one_config_from_var_dict(
13-
var_dict: Dict[str, np.ndarray], fun_control: Dict[str, Union[List[str], str]]
12+
var_dict: Dict[str, np.ndarray],
13+
fun_control: Dict[str, Union[List[str], str]],
14+
default: bool = False,
1415
) -> Generator[Dict[str, Union[int, float]], None, None]:
1516
"""Generate one configuration from a dictionary of variables (as a generator).
1617
1718
This function takes a dictionary of variables as input arguments and returns a generator
1819
that yields dictionaries with the values from the arrays in the input dictionary.
1920
2021
Args:
21-
var_dict (dict): A dictionary where keys are variable names and values are numpy arrays.
22-
fun_control (dict): A dictionary which (at least) has an entry with the following key:
22+
var_dict (dict):
23+
A dictionary where keys are variable names and values are numpy arrays.
24+
fun_control (dict):
25+
A dictionary which (at least) has an entry with the following key:
2326
"var_type" (list): A list of variable types. If the entry is not "num" the corresponding
2427
value will be converted to the type "int".
28+
default (bool):
29+
A boolean value indicating whether to use the default values from fun_control.
2530
2631
Returns:
2732
Generator[dict]: A generator that yields dictionaries with the values from the arrays in the input dictionary.
@@ -36,13 +41,15 @@ def generate_one_config_from_var_dict(
3641
"""
3742
for values in iterate_dict_values(var_dict):
3843
values = convert_keys(values, fun_control["var_type"])
39-
values = get_dict_with_levels_and_types(fun_control=fun_control, v=values)
44+
values = get_dict_with_levels_and_types(fun_control=fun_control, v=values, default=default)
4045
values = transform_hyper_parameter_values(fun_control=fun_control, hyper_parameter_values=values)
4146
yield values
4247

4348

4449
def return_conf_list_from_var_dict(
45-
var_dict: Dict[str, np.ndarray], fun_control: Dict[str, Union[List[str], str]]
50+
var_dict: Dict[str, np.ndarray],
51+
fun_control: Dict[str, Union[List[str], str]],
52+
default: bool = False,
4653
) -> List[Dict[str, Union[int, float]]]:
4754
"""Return a list of configurations from a dictionary of variables.
4855
@@ -68,7 +75,7 @@ def return_conf_list_from_var_dict(
6875
[{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}]
6976
"""
7077
conf_list = []
71-
for values in generate_one_config_from_var_dict(var_dict, fun_control):
78+
for values in generate_one_config_from_var_dict(var_dict, fun_control, default=default):
7279
conf_list.append(values)
7380
return conf_list
7481

@@ -127,7 +134,7 @@ def convert_keys(d: Dict[str, Union[int, float, str]], var_type: List[str]) -> D
127134
return d
128135

129136

130-
def get_dict_with_levels_and_types(fun_control: Dict[str, Any], v: Dict[str, Any]) -> Dict[str, Any]:
137+
def get_dict_with_levels_and_types(fun_control: Dict[str, Any], v: Dict[str, Any], default=False) -> Dict[str, Any]:
131138
"""Get dictionary with levels and types.
132139
The function maps the numerical output of the hyperparameter optimization to the corresponding levels
133140
of the hyperparameter needed by the core model, i.e., the tuned algorithm.
@@ -140,8 +147,12 @@ def get_dict_with_levels_and_types(fun_control: Dict[str, Any], v: Dict[str, Any
140147
via getattr("class", value).
141148
142149
Args:
143-
fun_control (Dict[str, Any]): A dictionary containing information about the core model hyperparameters.
144-
v (Dict[str, Any]): A dictionary containing the numerical output of the hyperparameter optimization.
150+
fun_control (Dict[str, Any]):
151+
A dictionary containing information about the core model hyperparameters.
152+
v (Dict[str, Any]):
153+
A dictionary containing the numerical output of the hyperparameter optimization.
154+
default (bool):
155+
A boolean value indicating whether to use the default values from fun_control.
145156
146157
Returns:
147158
Dict[str, Any]:
@@ -214,7 +225,10 @@ def get_dict_with_levels_and_types(fun_control: Dict[str, Any], v: Dict[str, Any
214225
'max_size': 500.0
215226
}
216227
"""
217-
d = fun_control["core_model_hyper_dict"]
228+
if default:
229+
d = fun_control["core_model_hyper_dict_default"]
230+
else:
231+
d = fun_control["core_model_hyper_dict"]
218232
new_dict = {}
219233
for key, value in v.items():
220234
if key in d and d[key]["type"] == "factor":
@@ -773,14 +787,20 @@ def add_core_model_to_fun_control(fun_control, core_model, hyper_dict=None, file
773787
fun_control.update({"var_type": var_type, "var_name": var_name, "lower": lower, "upper": upper})
774788

775789

776-
def get_one_core_model_from_X(X, fun_control=None):
790+
def get_one_core_model_from_X(
791+
X,
792+
fun_control=None,
793+
default=False,
794+
):
777795
"""Get one core model from X.
778796
779797
Args:
780798
X (np.array):
781799
The array with the hyper parameter values.
782800
fun_control (dict):
783801
The function control dictionary.
802+
default (bool):
803+
A boolean value indicating whether to use the default values from fun_control.
784804
785805
Returns:
786806
(class):
@@ -798,8 +818,9 @@ def get_one_core_model_from_X(X, fun_control=None):
798818
get_one_core_model_from_X(X, fun_control)
799819
HoeffdingAdaptiveTreeRegressor()
800820
"""
801-
var_dict = assign_values(X=X, var_list=get_var_name(fun_control))
802-
config = return_conf_list_from_var_dict(var_dict, fun_control)[0]
821+
var_dict = assign_values(X, fun_control["var_name"])
822+
# var_dict = assign_values(X, get_var_name(fun_control))
823+
config = return_conf_list_from_var_dict(var_dict, fun_control, default=default)[0]
803824
core_model = fun_control["core_model"](**config)
804825
return core_model
805826

@@ -940,37 +961,39 @@ def get_default_hyperparameters_as_array(fun_control) -> np.array:
940961
return X0
941962

942963

943-
def get_default_hyperparameters_for_core_model(fun_control) -> dict:
944-
"""Get the default hyper parameters for the core model.
945-
946-
Args:
947-
fun_control (dict):
948-
The function control dictionary.
949-
950-
Returns:
951-
(dict):
952-
The default hyper parameters for the core model.
953-
954-
Examples:
955-
>>> from river.tree import HoeffdingAdaptiveTreeRegressor
956-
from spotRiver.data.river_hyper_dict import RiverHyperDict
957-
fun_control = {}
958-
add_core_model_to_fun_control(core_model=HoeffdingAdaptiveTreeRegressor,
959-
fun_control=func_control,
960-
hyper_dict=RiverHyperDict,
961-
filename=None)
962-
get_default_hyperparameters_for_core_model(fun_control)
963-
{'leaf_prediction': 'mean',
964-
'leaf_model': 'NBAdaptive',
965-
'splitter': 'HoeffdingAdaptiveTreeSplitter',
966-
'binary_split': 'info_gain',
967-
'stop_mem_management': False}
968-
"""
969-
values = get_default_values(fun_control)
970-
values = get_dict_with_levels_and_types(fun_control=fun_control, v=values)
971-
values = convert_keys(values, fun_control["var_type"])
972-
values = transform_hyper_parameter_values(fun_control=fun_control, hyper_parameter_values=values)
973-
return values
964+
# def get_default_hyperparameters_for_core_model(fun_control) -> dict:
965+
# """Get the default hyper parameters for the core model.
966+
967+
# Args:
968+
# fun_control (dict):
969+
# The function control dictionary.
970+
971+
# Returns:
972+
# (dict):
973+
# The default hyper parameters for the core model.
974+
975+
# Examples:
976+
# >>> from river.tree import HoeffdingAdaptiveTreeRegressor
977+
# from spotRiver.data.river_hyper_dict import RiverHyperDict
978+
# fun_control = {}
979+
# add_core_model_to_fun_control(core_model=HoeffdingAdaptiveTreeRegressor,
980+
# fun_control=func_control,
981+
# hyper_dict=RiverHyperDict,
982+
# filename=None)
983+
# get_default_hyperparameters_for_core_model(fun_control)
984+
# {'leaf_prediction': 'mean',
985+
# 'leaf_model': 'NBAdaptive',
986+
# 'splitter': 'HoeffdingAdaptiveTreeSplitter',
987+
# 'binary_split': 'info_gain',
988+
# 'stop_mem_management': False}
989+
# """
990+
# values = get_default_values(fun_control)
991+
# print(f"values: {values}")
992+
# pprint.pprint(fun_control)
993+
# values = get_dict_with_levels_and_types(fun_control=fun_control, v=values, default=True)
994+
# values = convert_keys(values, fun_control["var_type"])
995+
# values = transform_hyper_parameter_values(fun_control=fun_control, hyper_parameter_values=values)
996+
# return values
974997

975998

976999
def get_tuned_architecture(spot_tuner, fun_control, force_minX=False) -> dict:

0 commit comments

Comments
 (0)