eda
compare_two_tree_models(model1, model2, headers=['Parameter', 'Default', 'Spot'])
¶
Compares two tree models. Args: model1 (object): A tree model. model2 (object): A tree model. headers (list): A list with the headers of the table.
Returns:
Type | Description |
---|---|
str
|
A table with the comparison of the two models. |
Examples:
>>> from spotpython.utils.eda import compare_two_tree_models
>>> from spotpython.hyperparameters.values import get_default_values
>>> fun_control = {
... "x1": {"type": "int", "default": 1, "lower": 1, "upper": 10},
... "x2": {"type": "int", "default": 1, "lower": 1, "upper": 10},
... "x3": {"type": "int", "default": 1, "lower": 1, "upper": 10},
... "x4": {"type": "int", "default": 1, "lower": 1, "upper": 10},
... "x5": {"type": "int", "default": 1, "lower": 1, "upper": 10},
... "x6": {"type": "int", "default": 1, "lower": 1, "upper": 10},
... "x7": {"type": "int", "default": 1, "lower": 1, "upper": 10},
... "x8": {"type": "int", "default": 1, "lower": 1, "upper": 10},
... "x9": {"type": "int", "default": 1, "lower": 1, "upper": 10},
... "x10": {"type": "int", "default": 1, "lower": 1, "upper": 10},
... }
>>> default_values = get_default_values(fun_control)
>>> model1 = spot_tuner.get_model("rf", default_values)
>>> model2 = spot_tuner.get_model("rf", default_values)
>>> compare_two_tree_models(model1, model2)
Source code in spotpython/utils/eda.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
|
count_missing_data(df)
¶
Counts the number of missing values in each column of the given DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
DataFrame containing the data to be counted. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
DataFrame containing the number of missing values in each column. |
Example
import pandas as pd df = pd.DataFrame({‘A’: [1, 2, None], ‘B’: [4, None, 6], ‘C’: [7, 8, 9]}) count_missing_data(df) column_name missing_count 0 A 1 1 B 1
Source code in spotpython/utils/eda.py
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
|
filter_highly_correlated(df, sorted=True, threshold=1 - 1e-05)
¶
Return a new DataFrame with only those columns that are highly correlated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The input DataFrame. |
required |
threshold |
float
|
The correlation threshold. |
1 - 1e-05
|
sorted |
bool
|
If True, the columns are sorted by name. |
True
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
A new DataFrame with only highly correlated columns. |
Examples:
>>> df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
df = filter_highly_correlated(df, sorted=True, threshold=0.99)
Source code in spotpython/utils/eda.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
|
gen_design_table(fun_control, spot=None, tablefmt='github')
¶
Generates a table with the design variables and their bounds.
Args:
fun_control (dict):
A dictionary with function design variables.
spot (object):
A spot object. Defaults to None.
Returns:
(str):
a table with the design variables, their default values, and their bounds.
If a spot object is provided,
the table will also include the value and the importance of each hyperparameter.
Use the print
function to display the table.
Source code in spotpython/utils/eda.py
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 240 241 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 |
|
generate_config_id(config, hash=False, timestamp=False)
¶
Generates a unique id for a configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
dict
|
A dictionary with the configuration. |
required |
hash |
bool
|
If True, the id is hashed. |
False
|
timestamp |
bool
|
If True, the id is appended with a timestamp. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
str
|
A unique id for the configuration. |
Examples:
>>> from spotpython.hyperparameters.values import get_one_config_from_X
>>> X = spot_tuner.to_all_dim(spot_tuner.min_X.reshape(1,-1))
>>> config = get_one_config_from_X(X, fun_control)
>>> generate_config_id(config)
Source code in spotpython/utils/eda.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
|
get_stars(input_list)
¶
Converts a list of values to a list of stars, which can be used to visualize the importance of a variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_list |
list
|
A list of values. |
required |
Returns:
Type | Description |
---|---|
list
|
A list of strings. |
Examples:
>>> from spotpython.utils.eda import convert_list
>>> get_stars([100, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
[***, '', '', '', '', '', '', '', '']
Source code in spotpython/utils/eda.py
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 |
|
plot_missing_data(df, relative=False, figsize=(7, 5), color='grey', xlabel='Missing Data', title='Missing Data')
¶
Plots a horizontal bar chart of the number of missing values in each column of the given DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
DataFrame containing the data to be plotted. |
required |
relative |
bool
|
Whether to plot relative values (percentage) or absolute values. |
False
|
figsize |
tuple
|
Size of the figure to be plotted. |
(7, 5)
|
color |
str
|
Color of the bars in the bar chart. |
'grey'
|
xlabel |
str
|
Label for the x-axis. |
'Missing Data'
|
title |
str
|
Title for the plot. |
'Missing Data'
|
Returns:
Type | Description |
---|---|
NoneType
|
None |
Example
import pandas as pd df = pd.DataFrame({‘A’: [1, 2, np.nan], ‘B’: [4, np.nan, 6], ‘C’: [7, 8, 9]}) plot_missing_data(df)
Source code in spotpython/utils/eda.py
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
|
plot_sns_heatmap(df_heat, figsize=(16, 12), cmap='vlag', vmin=-1, vmax=1, annot=True, fmt='.5f', linewidths=0.5, annot_kws={'size': 8})
¶
Plots a heatmap of the correlation matrix of the given DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df_heat |
DataFrame
|
DataFrame containing the data to be plotted. |
required |
figsize |
tuple
|
Size of the figure to be plotted. |
(16, 12)
|
cmap |
str
|
Color map to be used for the heatmap. |
'vlag'
|
vmin |
int
|
Minimum value for the color scale. |
-1
|
vmax |
int
|
Maximum value for the color scale. |
1
|
annot |
bool
|
Whether to display annotations on the heatmap. |
True
|
fmt |
str
|
Format string for annotations. |
'.5f'
|
linewidths |
float
|
Width of lines separating cells in the heatmap. |
0.5
|
annot_kws |
dict
|
Keyword arguments for annotations. |
{'size': 8}
|
Returns:
Type | Description |
---|---|
NoneType
|
None |
Example
import pandas as pd df = pd.DataFrame({‘A’: [1, 2, 3], ‘B’: [4, 5, 6], ‘C’: [7, 8, 9]}) plot_heatmap(df)
Source code in spotpython/utils/eda.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
|
print_exp_table(fun_control, tablefmt='github', print_tab=True)
¶
Generates a table with the design variables and their bounds. Can be used for the experiment design, which was not run yet. Args: fun_control (dict): A dictionary with function design variables. tablefmt (str): The format of the table. Defaults to “github”. print_tab (bool): If True, the table is printed. Otherwise, the result code from tabulate is returned. Defaults to True.
Returns:
Type | Description |
---|---|
str
|
a table with the design variables, their default values, and their bounds.
Use the |
Examples:
>>> from spotpython.data.diabetes import Diabetes
from spotpython.hyperdict.light_hyper_dict import LightHyperDict
from spotpython.fun.hyperlight import HyperLight
from spotpython.utils.init import fun_control_init
from spotpython.spot import Spot
from spotpython.utils.eda import print_exp_table
fun_control = fun_control_init(
PREFIX="print_exp_table",
fun_evals=10,
max_time=1,
data_set = Diabetes(),
core_model_name="light.regression.NNLinearRegressor",
hyperdict=LightHyperDict,
_L_in=10,
_L_out=1)
fun = HyperLight().fun
print_exp_table(fun_control)
| name | type | default | lower | upper | transform |
|----------------|--------|-----------|---------|---------|-----------------------|
| l1 | int | 3 | 3 | 8 | transform_power_2_int |
| epochs | int | 4 | 4 | 9 | transform_power_2_int |
| batch_size | int | 4 | 1 | 4 | transform_power_2_int |
| act_fn | factor | ReLU | 0 | 5 | None |
| optimizer | factor | SGD | 0 | 11 | None |
| dropout_prob | float | 0.01 | 0 | 0.25 | None |
| lr_mult | float | 1.0 | 0.1 | 10 | None |
| patience | int | 2 | 2 | 6 | transform_power_2_int |
| batch_norm | factor | 0 | 0 | 1 | None |
| initialization | factor | Default | 0 | 4 | None |
Source code in spotpython/utils/eda.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
print_res_table(spot=None, tablefmt='github', print_tab=True)
¶
Generates a table with the design variables and their bounds, after the run was completed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spot |
object
|
A spot object. Defaults to None. |
None
|
tablefmt |
str
|
The format of the table. Defaults to “github”. |
'github'
|
print_tab |
bool
|
If True, the table is printed. Otherwise, the result code from tabulate is returned. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
str
|
a table with the design variables, their default values, their bounds,
the value and the importance of each hyperparameter.
Use the |
Examples:
from spotpython.data.diabetes import Diabetes from spotpython.hyperdict.light_hyper_dict import LightHyperDict from spotpython.fun.hyperlight import HyperLight from spotpython.utils.init import fun_control_init, design_control_init from spotpython.spot import Spot from spotpython.utils.eda import print_res_table from spotpython.hyperparameters.values import set_hyperparameter fun_control = fun_control_init( PREFIX=”print_res_table”, fun_evals=5, max_time=1, data_set = Diabetes(), core_model_name=”light.regression.NNLinearRegressor”, hyperdict=LightHyperDict, _L_in=10, _L_out=1) set_hyperparameter(fun_control, “optimizer”, [ “Adadelta”, “Adam”, “Adamax”]) set_hyperparameter(fun_control, “l1”, [1,2]) set_hyperparameter(fun_control, “epochs”, [2,2]) set_hyperparameter(fun_control, “batch_size”, [4,11]) set_hyperparameter(fun_control, “dropout_prob”, [0.0, 0.025]) set_hyperparameter(fun_control, “patience”, [1,2]) design_control = design_control_init(init_size=3) fun = HyperLight().fun S = Spot(fun=fun, fun_control=fun_control, design_control=design_control) S.run() print_res_table(S) | name | type | default | lower | upper | tuned | transform | importance | stars | |----------------|--------|-----------|---------|---------|----------------------|-----------------------|--------------|---------| | l1 | int | 3 | 1.0 | 2.0 | 2.0 | transform_power_2_int | 29.49 | * | | epochs | int | 4 | 2.0 | 2.0 | 2.0 | transform_power_2_int | 0.00 | | | batch_size | int | 4 | 4.0 | 11.0 | 5.0 | transform_power_2_int | 1.18 | * | | act_fn | factor | ReLU | 0.0 | 5.0 | ELU | None | 0.32 | . | | optimizer | factor | SGD | 0.0 | 2.0 | Adam | None | 0.08 | | | dropout_prob | float | 0.01 | 0.0 | 0.025 | 0.010464684336704316 | None | 0.27 | . | | lr_mult | float | 1.0 | 0.1 | 10.0 | 8.82569482726512 | None | 9.55 | * | | patience | int | 2 | 1.0 | 2.0 | 2.0 | transform_power_2_int | 100.00 | *** | | batch_norm | factor | 0 | 0.0 | 1.0 | 0 | None | 0.05 | | | initialization | factor | Default | 0.0 | 4.0 | kaiming_normal | None | 1.07 | * |
Source code in spotpython/utils/eda.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 185 186 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 |
|