|
31 | 31 | # End Important |
32 | 32 |
|
33 | 33 |
|
| 34 | +def assign_values(X: np.array, var_list: list) -> dict: |
| 35 | + """ |
| 36 | + This function takes an np.array X and a list of variable names as input arguments |
| 37 | + and returns a dictionary where the keys are the variable names and the values are assigned from X. |
| 38 | +
|
| 39 | + Args: |
| 40 | + X (np.array): |
| 41 | + A 2D numpy array where each column represents a variable. |
| 42 | + var_list (list): |
| 43 | + A list of strings representing variable names. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + dict: |
| 47 | + A dictionary where keys are variable names and values are assigned from X. |
| 48 | +
|
| 49 | + Raises: |
| 50 | + ValueError: If the length of var_list does not match the number of columns in X. |
| 51 | +
|
| 52 | + Examples: |
| 53 | + >>> import numpy as np |
| 54 | + >>> from spotpython.hyperparameters.values import assign_values |
| 55 | + >>> X = np.array([[1, 2], [3, 4], [5, 6]]) |
| 56 | + >>> var_list = ['a', 'b'] |
| 57 | + >>> result = assign_values(X, var_list) |
| 58 | + >>> print(result) |
| 59 | + {'a': array([1, 3, 5]), 'b': array([2, 4, 6])} |
| 60 | + """ |
| 61 | + if X.shape[1] != len(var_list): |
| 62 | + raise ValueError("Length of var_list does not match the number of columns in X.") |
| 63 | + |
| 64 | + result = {var_list[i]: X[:, i] for i in range(len(var_list))} |
| 65 | + return result |
| 66 | + |
| 67 | + |
| 68 | +def get_tuned_architecture(spot_tuner, force_minX=False) -> dict: |
| 69 | + """ |
| 70 | + Returns the tuned architecture. If the spot tuner has noise, |
| 71 | + it returns the architecture with the lowest mean (.min_mean_X), |
| 72 | + otherwise it returns the architecture with the lowest value (.min_X). |
| 73 | +
|
| 74 | + Args: |
| 75 | + spot_tuner (object): |
| 76 | + spot tuner object. |
| 77 | + force_minX (bool): |
| 78 | + If True, return the architecture with the lowest value (.min_X). |
| 79 | +
|
| 80 | + Returns: |
| 81 | + (dict): |
| 82 | + dictionary containing the tuned architecture. |
| 83 | + """ |
| 84 | + if not spot_tuner.noise or force_minX: |
| 85 | + X = spot_tuner.to_all_dim(spot_tuner.min_X.reshape(1, -1)) |
| 86 | + else: |
| 87 | + # noise or force_minX is False: |
| 88 | + X = spot_tuner.to_all_dim(spot_tuner.min_mean_X.reshape(1, -1)) |
| 89 | + fun_control = copy(spot_tuner.fun_control) |
| 90 | + config = get_one_config_from_X(X, fun_control) |
| 91 | + return config |
| 92 | + |
| 93 | + |
34 | 94 | def generate_one_config_from_var_dict( |
35 | 95 | var_dict: Dict[str, np.ndarray], |
36 | 96 | fun_control: Dict[str, Union[List[str], str]], |
@@ -275,36 +335,6 @@ def get_dict_with_levels_and_types(fun_control: Dict[str, Any], v: Dict[str, Any |
275 | 335 | return new_dict |
276 | 336 |
|
277 | 337 |
|
278 | | -def assign_values(X: np.array, var_list: list) -> dict: |
279 | | - """ |
280 | | - This function takes an np.array X and a list of variable names as input arguments |
281 | | - and returns a dictionary where the keys are the variable names and the values are assigned from X. |
282 | | -
|
283 | | - Args: |
284 | | - X (np.array): |
285 | | - A 2D numpy array where each column represents a variable. |
286 | | - var_list (list): |
287 | | - A list of strings representing variable names. |
288 | | -
|
289 | | - Returns: |
290 | | - dict: |
291 | | - A dictionary where keys are variable names and values are assigned from X. |
292 | | -
|
293 | | - Examples: |
294 | | - >>> import numpy as np |
295 | | - >>> from spotpython.hyperparameters.values import assign_values |
296 | | - >>> X = np.array([[1, 2], [3, 4], [5, 6]]) |
297 | | - >>> var_list = ['a', 'b'] |
298 | | - >>> result = assign_values(X, var_list) |
299 | | - >>> print(result) |
300 | | - {'a': array([1, 3, 5]), 'b': array([2, 4, 6])} |
301 | | - """ |
302 | | - result = {} |
303 | | - for i, var_name in enumerate(var_list): |
304 | | - result[var_name] = X[:, i] |
305 | | - return result |
306 | | - |
307 | | - |
308 | 338 | def modify_boolean_hyper_parameter_levels(fun_control, hyperparameter, levels) -> None: |
309 | 339 | """ |
310 | 340 | This function modifies the levels of a boolean hyperparameter in the fun_control dictionary. |
@@ -983,68 +1013,6 @@ def get_default_hyperparameters_as_array(fun_control) -> np.array: |
983 | 1013 | return X0 |
984 | 1014 |
|
985 | 1015 |
|
986 | | -# def get_default_hyperparameters_for_core_model(fun_control) -> dict: |
987 | | -# """Get the default hyper parameters for the core model. |
988 | | - |
989 | | -# Args: |
990 | | -# fun_control (dict): |
991 | | -# The function control dictionary. |
992 | | - |
993 | | -# Returns: |
994 | | -# (dict): |
995 | | -# The default hyper parameters for the core model. |
996 | | - |
997 | | -# Examples: |
998 | | -# >>> from river.tree import HoeffdingAdaptiveTreeRegressor |
999 | | -# from spotriver.data.river_hyper_dict import RiverHyperDict |
1000 | | -# fun_control = {} |
1001 | | -# add_core_model_to_fun_control(core_model=HoeffdingAdaptiveTreeRegressor, |
1002 | | -# fun_control=func_control, |
1003 | | -# hyper_dict=RiverHyperDict, |
1004 | | -# filename=None) |
1005 | | -# get_default_hyperparameters_for_core_model(fun_control) |
1006 | | -# {'leaf_prediction': 'mean', |
1007 | | -# 'leaf_model': 'NBAdaptive', |
1008 | | -# 'splitter': 'HoeffdingAdaptiveTreeSplitter', |
1009 | | -# 'binary_split': 'info_gain', |
1010 | | -# 'stop_mem_management': False} |
1011 | | -# """ |
1012 | | -# values = get_default_values(fun_control) |
1013 | | -# print(f"values: {values}") |
1014 | | -# pprint.pprint(fun_control) |
1015 | | -# values = get_dict_with_levels_and_types(fun_control=fun_control, v=values, default=True) |
1016 | | -# values = convert_keys(values, fun_control["var_type"]) |
1017 | | -# values = transform_hyper_parameter_values(fun_control=fun_control, hyper_parameter_values=values) |
1018 | | -# return values |
1019 | | - |
1020 | | - |
1021 | | -def get_tuned_architecture(spot_tuner, fun_control, force_minX=False) -> dict: |
1022 | | - """ |
1023 | | - Returns the tuned architecture. If the spot tuner has noise, |
1024 | | - it returns the architecture with the lowest mean (.min_mean_X), |
1025 | | - otherwise it returns the architecture with the lowest value (.min_X). |
1026 | | -
|
1027 | | - Args: |
1028 | | - spot_tuner (object): |
1029 | | - spot tuner object. |
1030 | | - fun_control (dict): |
1031 | | - dictionary containing control parameters for the hyperparameter tuning. |
1032 | | - force_minX (bool): |
1033 | | - If True, return the architecture with the lowest value (.min_X). |
1034 | | -
|
1035 | | - Returns: |
1036 | | - (dict): |
1037 | | - dictionary containing the tuned architecture. |
1038 | | - """ |
1039 | | - if not spot_tuner.noise or force_minX: |
1040 | | - X = spot_tuner.to_all_dim(spot_tuner.min_X.reshape(1, -1)) |
1041 | | - else: |
1042 | | - # noise or force_minX is False: |
1043 | | - X = spot_tuner.to_all_dim(spot_tuner.min_mean_X.reshape(1, -1)) |
1044 | | - config = get_one_config_from_X(X, fun_control) |
1045 | | - return config |
1046 | | - |
1047 | | - |
1048 | 1016 | def create_model(config, fun_control, **kwargs) -> object: |
1049 | 1017 | """ |
1050 | 1018 | Creates a model for the given configuration and control parameters. |
|
0 commit comments