Preprocess
This is the reference to the functions contained in
preprocess
. For now, they are all accesible directly
through machine-learning-datasets
and you don't
need to use the preprocess
namespace.
Common Utility Functions
apply_cmap(img, cmap, cmap_norm=None, alwaysscale=False, overlay_bg=None, **kwargs)
Apply a colormap to an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img |
ndarray
|
The input image. |
required |
cmap |
Union[str, Colormap]
|
The colormap to apply. Can be a string representing the name of the colormap or a Colormap object. |
required |
cmap_norm |
Optional[Union[str, Colormap]]
|
The normalization to apply to the image before applying the colormap. Can be a string representing the name of the normalization or a Colormap object. Defaults to None. |
None
|
alwaysscale |
Optional[bool]
|
Whether to always scale the image before applying the colormap. Defaults to False. |
False
|
overlay_bg |
Optional[ndarray]
|
The background image to overlay on the colormap. Defaults to None. |
None
|
**kwargs |
Any
|
Additional keyword arguments to pass to the normalize_heatmap function. |
{}
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The image with the applied colormap. |
Note
- If the input image has 3 channels, it will be converted to grayscale before applying the colormap.
- If cmap_norm is provided, the image will be normalized using the normalize_heatmap function before applying the colormap.
- If alwaysscale is True or the image values are outside the range [0, 1], the image will be scaled using MinMaxScaler before applying the colormap.
- The alpha channel of the colormap image will be removed.
- If overlay_bg is provided, it will be overlaid on the colormap image using the heatmap_overlay function.
Source code in machine_learning_datasets/preprocess.py
115 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 |
|
cumulative_sum_threshold(values, percentile)
Calculate the cumulative sum threshold.
This function calculates the cumulative sum threshold of a given array of values based on a specified percentile.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values |
ndarray
|
The array of values. |
required |
percentile |
int
|
The percentile for thresholding. Must be between 0 and 100 inclusive. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The threshold value. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the percentile is not between 0 and 100 inclusive. |
Source code in machine_learning_datasets/preprocess.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
discretize(v, v_intervals, use_quantiles=False, use_continuous_bins=False)
Discretize a variable into intervals.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v |
Union[str, Series, ndarray]
|
The variable to be discretized. |
required |
v_intervals |
Union[int, list, ndarray]
|
The intervals to discretize the variable into. |
required |
use_quantiles |
Optional[bool], default=False
|
Whether to use quantiles for discretization. |
False
|
use_continuous_bins |
Optional[bool], default=False
|
Whether to use continuous bins for discretization. |
False
|
Returns:
Type | Description |
---|---|
Tuple[Union[str, Series, ndarray], ndarray]
|
Tuple[Union[str, pd.Series, np.ndarray], np.ndarray]: The discretized variable and the bins. |
Raises:
Type | Description |
---|---|
ValueError
|
If the length of the interval does not match the number of unique items in the array. |
Note
- If
v
is a string andv_intervals
is a list or array, the function returnsv
andv_intervals
as is. - If
v
is numeric andv_intervals
is an integer, the function discretizesv
intov_intervals
bins. - If
v
is an object or a category, the function convertsv
into a string and assigns a numerical value to each unique item.
Examples:
>>> v = [1, 2, 3, 4, 5]
>>> v_intervals = 2
>>> discretize(v, v_intervals)
([0, 0, 1, 1, 1], array([1, 3, 5]))
>>> v = pd.Series(['A', 'B', 'C', 'A', 'B'])
>>> v_intervals = ['A', 'B', 'C']
>>> discretize(v, v_intervals)
(0 0
1 1
2 2
3 0
4 1
dtype: object, array(['A', 'B', 'C'], dtype=object))
Source code in machine_learning_datasets/preprocess.py
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 270 271 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 |
|
find_closest_datapoint_idx(point, points, metric_or_fn='euclidean', find_exact_first=0, distargs=None, scaler=None)
Find the index of the closest datapoint to a given point.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
point |
ArrayLike
|
The point for which to find the closest datapoint index. |
required |
points |
ArrayLike
|
The array of datapoints to search for the closest index. |
required |
metric_or_fn |
Optional[Union[str, Callable]], default='euclidean'
|
The distance metric or function to use for calculating distances between points. |
'euclidean'
|
find_exact_first |
Optional[int], default=0
|
Determines the behavior when multiple closest datapoints are found. - 0: Return the index of the last closest datapoint found. - 1: Return the index of the last closest datapoint found where the sum of the features of the datapoint matches the sum of the features of the point. - 2: Return the index of the last closest datapoint found where all the features of the datapoint match all the features of the point. |
0
|
distargs |
Optional[Dict], default=None
|
Additional arguments to pass to the distance metric or function. |
None
|
scaler |
Optional[BaseTransformerProtocol], default=None
|
A scaler object to transform the point and points before calculating distances. |
None
|
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The index of the closest datapoint to the given point. |
Raises:
Type | Description |
---|---|
ValueError
|
If the point is not 1-dimensional, the points are not 2-dimensional, or the number of features in the point and points do not match. |
ValueError
|
If |
Note
- If
find_exact_first
is set to 1, the function will first check for datapoints where the sum of the features matches the sum of the features of the point. - If
find_exact_first
is set to 2, the function will check for datapoints where all the features match all the features of the point. - If
scaler
is provided, the point and points will be transformed before calculating distances. - If
metric_or_fn
is a string, the function will use the specified distance metric from thescipy.spatial.distance
module. - If
metric_or_fn
is a callable object, the function will use the provided distance function to calculate distances.
Source code in machine_learning_datasets/preprocess.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
|
heatmap_overlay(bg_img, overlay_img, cmap='jet')
Overlay a heatmap on top of an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bg_img |
ndarray
|
The background image. |
required |
overlay_img |
ndarray
|
The heatmap image to overlay. |
required |
cmap |
Optional[Union[str, Colormap]]
|
The colormap to use for the heatmap. Defaults to 'jet'. |
'jet'
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The resulting image with the heatmap overlay. |
Source code in machine_learning_datasets/preprocess.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
|
img_np_from_fig(fig, dpi=14)
Converts a matplotlib figure to a NumPy array representing an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fig |
Figure
|
The matplotlib figure to convert. |
required |
dpi |
Optional[int]
|
The resolution of the image in dots per inch. Default is 14. |
14
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The NumPy array representing the image. |
Example
fig = plt.figure()
... create and modify the figure ...
img = img_np_from_fig(fig)
Source code in machine_learning_datasets/preprocess.py
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 |
|
make_dummies_from_dict(df, colname, match_dict, drop_orig=True, nospacechr='_')
Creates dummy variables based on a dictionary or list of values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The input DataFrame. |
required |
colname |
str
|
The name of the column to create dummies from. |
required |
match_dict |
Union[Dict, List]
|
A dictionary or list of values to match against in the column. |
required |
drop_orig |
Optional[bool]
|
Whether to drop the original column after creating dummies. Defaults to True. |
True
|
nospacechr |
Optional[str]
|
The character to replace spaces with in the dummy variable names. Defaults to '_'. |
'_'
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: The DataFrame with dummy variables created. |
Example
df = pd.DataFrame({'col1': ['apple', 'banana', 'orange']}) match_dict = {'apple': 'fruit', 'banana': 'fruit'} make_dummies_from_dict(df, 'col1', match_dict) col1_fruit col1_orange 0 1 0 1 1 0 2 0 1
Source code in machine_learning_datasets/preprocess.py
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 |
|
make_dummies_with_limits(df_, colname, min_recs=0.005, max_dummies=20, defcatname='Other', nospacechr='_')
Make dummies with limits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df_ |
DataFrame
|
The input DataFrame. |
required |
colname |
str
|
The name of the column to create dummies for. |
required |
min_recs |
Optional[Union[int, float]], default=0.005
|
The minimum number of repeated records. |
0.005
|
max_dummies |
Optional[int], default=20
|
The maximum number of dummies to create. |
20
|
defcatname |
Optional[str], default='Other'
|
The name for the 'Other' category. |
'Other'
|
nospacechr |
Optional[str], default='_'
|
The character to replace spaces in the column name. |
'_'
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: The DataFrame with dummies created. |
Note
- If min_recs is less than 1, it is interpreted as a fraction of the total number of records.
- Dummies are created for the top values in the specified column, up to the maximum number of dummies.
- Values that do not meet the minimum number of records or are beyond the maximum number of dummies are grouped into the 'Other' category.
- Spaces in the column name are replaced with the specified character.
Source code in machine_learning_datasets/preprocess.py
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
minmax_scale_img(img)
Scales the input image to the range [0, 1].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img |
ndarray
|
The input image. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The scaled image. |
Source code in machine_learning_datasets/preprocess.py
437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
minmax_scale_img_posneg(img)
Scales the input image to the range [0, 1] by performing min-max scaling separately for positive and negative values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img |
ndarray
|
The input image. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The scaled image. |
Source code in machine_learning_datasets/preprocess.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
|
normalize_heatmap(heatmap, sign, outlier_perc=2, reduction_axis=None)
Normalize the heatmap based on the given sign type and outlier percentage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
heatmap |
ndarray
|
The input heatmap. |
required |
sign |
str
|
The sign type for normalization. Possible values are "all", "positive", "negative", and "absolute_value". |
required |
outlier_perc |
Optional[int]
|
The percentage of outliers to remove. Default is 2. |
2
|
reduction_axis |
Optional[int]
|
The axis along which to reduce the heatmap. Default is None. |
None
|
Returns:
Type | Description |
---|---|
Tuple[ndarray, Union[str, Colormap], int, int]
|
Tuple[np.ndarray, Union[str,Colormap], int, int]: A tuple containing the normalized heatmap, the colormap, vmin, and vmax. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the sign type is not valid. |
Source code in machine_learning_datasets/preprocess.py
313 314 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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
|
normalize_scale(heatmap, scale_factor)
Normalize the given heatmap by dividing it by the specified scale factor.
Parameters: - heatmap (np.ndarray): The input heatmap to be normalized. - scale_factor (float): The scale factor to divide the heatmap by.
Returns: - np.ndarray: The normalized heatmap.
- UserWarning: If the scale_factor is equal to 0, a warning is raised indicating that normalization is not possible.
- UserWarning: If the absolute value of the scale_factor is less than 1e-5, a warning is raised indicating that the heatmap values are close to 0 and the visualized results may be misleading.
Note: - The normalized heatmap is obtained by dividing the input heatmap by the scale_factor. - The resulting normalized heatmap is clipped between -1 and 1 using np.clip() function.
Source code in machine_learning_datasets/preprocess.py
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 |
|
tensor_to_img(tensor, norm_std=None, norm_mean=None, to_numpy=False, cmap_norm=None, cmap=None, cmap_alwaysscale=False, overlay_bg=None, **kwargs)
Converts a tensor to an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor |
Tensor
|
The input tensor. |
required |
norm_std |
Optional[Tuple]
|
The standard deviation for normalization. Default is None. |
None
|
norm_mean |
Optional[Tuple]
|
The mean for normalization. Default is None. |
None
|
to_numpy |
Optional[bool]
|
Whether to convert the tensor to a numpy array. Default is False. |
False
|
cmap_norm |
Optional[Union[str, Colormap]]
|
The normalization method for the colormap. Default is None. |
None
|
cmap |
Optional[Union[str, Colormap]]
|
The colormap to apply to the image. Default is None. |
None
|
cmap_alwaysscale |
Optional[bool]
|
Whether to always scale the colormap. Default is False. |
False
|
overlay_bg |
Optional[ndarray]
|
The background image to overlay. Default is None. |
None
|
**kwargs |
Any
|
Additional keyword arguments. |
{}
|
Returns:
Type | Description |
---|---|
Optional[ndarray]
|
Optional[np.ndarray]: The converted image as a numpy array, or None if the conversion fails. |
Source code in machine_learning_datasets/preprocess.py
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 213 214 215 216 217 218 219 220 221 222 223 |
|