transform
cod_to_nat_X(cod_X, cod_type, min_X=None, max_X=None, mean_X=None, std_X=None)
¶
Compute natural X-values from coded units based on the
setting of the cod_type
attribute. If cod_type
is “norm”, the values are
de-normalized from [0,1]. If cod_type
is “std”, the values are de-standardized.
Otherwise, the values are not modified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cod_X |
array
|
The coded X-values. |
required |
cod_type |
str
|
The type of coding (“norm”, “std”, or other). |
required |
min_X |
array
|
The minimum values of X. Defaults to None. |
None
|
max_X |
array
|
The maximum values of X. Defaults to None. |
None
|
mean_X |
array
|
The mean values of X. Defaults to None. |
None
|
std_X |
array
|
The standard deviation of X. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
X |
array
|
The natural (physical or real world) X-values. |
Source code in spotpython/utils/transform.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
|
cod_to_nat_y(cod_y, cod_type, min_y=None, max_y=None, mean_y=None, std_y=None)
¶
Compute natural y-values from coded units based on the
setting of the cod_type
attribute. If cod_type
is “norm”, the values are
de-normalized from [0,1]. If cod_type
is “std”, the values are de-standardized.
Otherwise, the values are not modified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cod_y |
array
|
The coded y-values. |
required |
cod_type |
str
|
The type of coding (“norm”, “std”, or other). |
required |
min_y |
array
|
The minimum values of y. Defaults to None. |
None
|
max_y |
array
|
The maximum values of y. Defaults to None. |
None
|
mean_y |
array
|
The mean values of y. Defaults to None. |
None
|
std_y |
array
|
The standard deviation of y. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
y |
array
|
The natural (physical or real world) y-values. |
Source code in spotpython/utils/transform.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
|
nat_to_cod_X(X, cod_type)
¶
Compute coded X-values from natural (physical or real world) units based on the
setting of the cod_type
attribute. If cod_type
is “norm”, the values are
normalized to [0,1]. If cod_type
is “std”, the values are standardized.
Otherwise, the values are not modified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
array
|
The input array. |
required |
cod_type |
str
|
The type of coding (“norm”, “std”, or other). |
required |
Returns:
Name | Type | Description |
---|---|---|
cod_X |
array
|
The coded X-values. |
min_X |
array
|
The minimum values of X. |
max_X |
array
|
The maximum values of X. |
mean_X |
array
|
The mean values of X. |
std_X |
array
|
The standard deviation of X. |
Source code in spotpython/utils/transform.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
nat_to_cod_y(y, cod_type)
¶
Compute coded y-values from natural (physical or real world) units based on the
setting of the cod_type
attribute. If cod_type
is “norm”, the values are
normalized to [0,1]. If cod_type
is “std”, the values are standardized.
Otherwise, the values are not modified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y |
array
|
The input array. |
required |
cod_type |
str
|
The type of coding (“norm”, “std”, or other). |
required |
Returns:
Name | Type | Description |
---|---|---|
cod_y |
array
|
The coded y-values. |
min_y |
array
|
The minimum values of y. |
max_y |
array
|
The maximum values of y. |
mean_y |
array
|
The mean values of y. |
std_y |
array
|
The standard deviation of y. |
Source code in spotpython/utils/transform.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
|
scale(X, lower, upper)
¶
Sample scaling from unit hypercube to different bounds. Converts a sample from [0, 1)
to [a, b)
.
The following transformation is used:
(b - a) * X + a
Note
equal lower and upper bounds are feasible.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
array
|
Sample to scale. |
required |
lower |
array
|
lower bound of transformed data. |
required |
upper |
array
|
upper bounds of transformed data. |
required |
Returns:
Type | Description |
---|---|
array
|
Scaled sample. |
Examples:
Transform three samples in the unit hypercube to (lower, upper) bounds:
>>> import numpy as np
>>> from scipy.stats import qmc
>>> from spotpython.utils.transform import scale
>>> lower = np.array([6, 0])
>>> upper = np.array([6, 5])
>>> sample = np.array([[0.5 , 0.75],
>>> [0.5 , 0.5],
>>> [0.75, 0.25]])
>>> scale(sample, lower, upper)
Source code in spotpython/utils/transform.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
transform_hyper_parameter_values(fun_control, hyper_parameter_values)
¶
Transform the values of the hyperparameters according to the transform function specified in fun_control if the hyperparameter is of type “int”, or “float” or “num”. Let fun_control = {“core_model_hyper_dict”:{ “leaf_prediction”: { “levels”: [“mean”, “model”, “adaptive”], “type”: “factor”, “default”: “mean”, “core_model_parameter_type”: “str”}, “max_depth”: { “type”: “int”, “default”: 20, “transform”: “transform_power_2”, “lower”: 2, “upper”: 20}}} and v = {‘max_depth’: 20,’leaf_prediction’: ‘mean’} and def transform_power_2(x): return 2**x. The function takes fun_control and v as input and returns a dictionary with the same structure as v. The function transforms the values of the hyperparameters according to the transform function specified in fun_control if the hyperparameter is of type “int”, or “float” or “num”. For example, transform_hyper_parameter_values(fun_control, v) returns {‘max_depth’: 1048576, ‘leaf_prediction’: ‘mean’}.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fun_control |
dict
|
A dictionary containing the information about the core model and the hyperparameters. |
required |
hyper_parameter_values |
dict
|
A dictionary containing the values of the hyperparameters. |
required |
Returns:
Type | Description |
---|---|
dict
|
A dictionary containing the values of the hyperparameters. |
Examples:
>>> import copy
from spotpython.utils.prepare import transform_hyper_parameter_values
fun_control = {
"core_model_hyper_dict": {
"leaf_prediction": {
"levels": ["mean", "model", "adaptive"],
"type": "factor",
"default": "mean",
"core_model_parameter_type": "str"},
"max_depth": {"type": "int",
"default": 20
"transform": "transform_power_2",
"lower": 2,
"upper": 20}}}
hyper_parameter_values = {'max_depth': 20,
'leaf_prediction': 'mean'}
transform_hyper_parameter_values(fun_control, hyper_parameter_values)
{'max_depth': 1048576,
'leaf_prediction': 'mean'}
Source code in spotpython/utils/transform.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
transform_multby2_int(x)
¶
Transformations for hyperparameters of type int.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
int
|
input, will be multiplied by 2 |
required |
Returns:
Type | Description |
---|---|
int
|
The result of multiplying x by 2. |
Examples:
>>> from spotpython.utils.transform import transform_multby2_int
>>> transform_multby2_int(3)
6
Source code in spotpython/utils/transform.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
transform_none_to_None(x)
¶
Transformations for hyperparameters of type None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
str
|
The string to transform. |
required |
Returns:
Type | Description |
---|---|
str
|
The transformed string. |
Examples:
>>> from spotpython.utils.transform import transform_none_to_None
>>> transform_none_to_None("none")
None
Note
Needed for sklearn.linear_model.LogisticRegression
Source code in spotpython/utils/transform.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
transform_power(base, x, as_int=False)
¶
Raises a given base to the power of x.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base |
int
|
The base to raise to the power of x. |
required |
x |
int
|
The exponent. |
required |
as_int |
bool
|
If True, returns the result as an integer. |
False
|
Returns:
Type | Description |
---|---|
float
|
The result of raising the base to the power of x. |
Examples:
>>> from spotpython.utils.transform import transform_power
>>> transform_power(2, 3)
8
Source code in spotpython/utils/transform.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
transform_power_10(x)
¶
Transformations for hyperparameters of type float.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
float
|
The exponent. |
required |
Returns:
Type | Description |
---|---|
float
|
The result of raising 10 to the power of x. |
Examples:
>>> from spotpython.utils.transform import transform_power_10
>>> transform_power_10(3)
1000
Source code in spotpython/utils/transform.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
transform_power_10_int(x)
¶
Transformations for hyperparameters of type int. Args: x (int): The exponent.
Returns:
Type | Description |
---|---|
int
|
The result of raising 10 to the power of x. |
Examples:
>>> from spotpython.utils.transform import transform_power_10_int
>>> transform_power_10_int(3)
1000
Source code in spotpython/utils/transform.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
transform_power_2(x)
¶
Transformations for hyperparameters of type float.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
float
|
The exponent. |
required |
Returns:
Type | Description |
---|---|
float
|
The result of raising 2 to the power of x. |
Examples:
>>> from spotpython.utils.transform import transform_power_2
>>> transform_power_2(3)
8
Source code in spotpython/utils/transform.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
transform_power_2_int(x)
¶
Transformations for hyperparameters of type int.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
int
|
The exponent. |
required |
Returns:
Type | Description |
---|---|
int
|
The result of raising 2 to the power of x. |
Examples:
>>> from spotpython.utils.transform import transform_power_2_int
>>> transform_power_2_int(3)
8
Source code in spotpython/utils/transform.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|