Imputers¶
imputers ¶
Assembly: mathy Filename: imputers.py Author: Terry D. Eppler Created: 05-31-2022
Last Modified By: Terry D. Eppler Last Modified On: 05-01-2025
imputers.py
Copyright © 2023 Terry Eppler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
You can contact me at: terryeppler@gmail.com or eppler.terry@epa.gov
Imputer ¶
Define the imputer interface.
Purpose
Defines the shared imputation contract used by Mathy preprocessing class/object. The base class establishes a common transformed-data attribute and requires concrete subclasses to implement fit, transform, fit-transform, and inverse-transform behavior where the underlying imputer supports it.
Attributes:
| Name | Type | Description |
|---|---|---|
transformed_data |
Optional[ndarray]
|
Most recent imputed matrix produced by a concrete imputer class/object. |
Source code in imputers.py
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 114 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 172 173 174 175 176 177 178 179 180 181 182 | |
train ¶
Fit an imputer.
Purpose
Defines the required training contract for concrete imputer class/object. Subclasses must fit their underlying imputer to the supplied feature matrix and return a fitted class/object or compatible result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix supplied to the concrete imputer implementation. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
object | None
|
object | None: Fitted concrete imputer class/object or implementation-specific training result. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Raised when the base method is called directly. |
Source code in imputers.py
transform ¶
Transform missing values.
Purpose
Defines the required transformation contract for concrete imputer class/object. Subclasses must replace missing values in the supplied feature matrix with values learned during fitting and return the imputed matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix supplied to the concrete imputer implementation. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Imputed feature matrix produced by the concrete imputer class/object. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Raised when the base method is called directly. |
Source code in imputers.py
train_transform ¶
Fit and transform missing values.
Purpose
Defines the required combined fit-transform contract for concrete imputer class/object. Subclasses must fit their underlying imputer and return the imputed matrix in one operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix supplied to the concrete imputer implementation. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Imputed feature matrix produced after fitting the concrete imputer class/object. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Raised when the base method is called directly. |
Source code in imputers.py
inverse_transform ¶
Invert an imputed matrix.
Purpose
Defines the inverse-transformation contract for concrete imputer class/object. Subclasses must reconstruct a pre-imputation representation when the underlying imputer supports inverse transformation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Transformed feature matrix supplied to the concrete imputer implementation. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Reconstructed feature matrix produced by the concrete imputer class/object. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Raised when the base method is called directly. |
Source code in imputers.py
MeanImputer ¶
Bases: Imputer
Wrap mean-based SimpleImputer.
Purpose
Replaces missing values using the arithmetic mean of each fitted feature column. The class/object stores the SimpleImputer instance, selected strategy, missing-value indicator setting, and most recent transformed output.
Attributes:
| Name | Type | Description |
|---|---|---|
strategy |
Optional[str]
|
Imputation strategy passed to SimpleImputer. |
add_indicator |
bool
|
Flag indicating whether missing-value indicator columns are appended. |
imputer |
SimpleImputer
|
Underlying imputer. |
transformed_data |
Optional[ndarray]
|
Most recent imputed matrix produced by the class/object. |
Source code in imputers.py
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 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 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 312 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 | |
train ¶
Fit MeanImputer.
Purpose
Fits the underlying SimpleImputer to the supplied feature matrix and returns the current class/object for consistent preprocessing chains.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix used to learn imputation statistics. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
MeanImputer | None
|
MeanImputer | None: Fitted mean-imputer class/object. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or fitting fails. |
Source code in imputers.py
transform ¶
Transform with MeanImputer.
Purpose
Replaces missing values in the supplied feature matrix using mean statistics learned during fitting and stores the imputed matrix on the class/object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix transformed by the fitted imputer. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Imputed feature matrix produced by the fitted mean imputer. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or transformation fails. |
Source code in imputers.py
train_transform ¶
Fit and transform with MeanImputer.
Purpose
Fits the underlying SimpleImputer to the supplied feature matrix, imputes the same matrix immediately, and stores the imputed result on the class/object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix used for fitting and imputation. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Imputed feature matrix produced after fitting the mean imputer. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or fit-transform execution fails. |
Source code in imputers.py
inverse_transform ¶
Invert MeanImputer output.
Purpose
Reconstructs a feature matrix toward its pre-imputed form using inverse transformation when missing-value indicator columns were included during fitting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Transformed feature matrix to invert. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Reconstructed feature matrix produced by the imputer. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation fails, indicator columns are unavailable, or inverse transformation fails. |
ValueError
|
Raised when the |
Source code in imputers.py
NearestImputer ¶
Bases: Imputer
Wrap KNNImputer.
Purpose
Replaces missing values using values from nearest neighboring samples. The class/object stores the KNNImputer instance, neighbor count, and most recent transformed output while exposing a consistent Mathy imputation interface.
Attributes:
| Name | Type | Description |
|---|---|---|
n_neighbors |
Optional[int]
|
Number of neighboring samples used for imputation. |
imputer |
KNNImputer
|
Underlying nearest-neighbor imputer. |
transformed_data |
Optional[ndarray]
|
Most recent imputed matrix produced by the class/object. |
Source code in imputers.py
353 354 355 356 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 391 392 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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | |
train ¶
Fit NearestImputer.
Purpose
Fits the underlying KNNImputer to the supplied feature matrix and returns the current class/object for consistent preprocessing chains.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix used to learn nearest-neighbor imputation structure. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
NearestImputer | None
|
NearestImputer | None: Fitted nearest-neighbor imputer class/object. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or fitting fails. |
Source code in imputers.py
transform ¶
Transform with NearestImputer.
Purpose
Replaces missing values in the supplied feature matrix using nearest-neighbor statistics learned during fitting and stores the imputed matrix on the class/object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix transformed by the fitted imputer. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Imputed feature matrix produced by the fitted nearest-neighbor imputer. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or transformation fails. |
Source code in imputers.py
train_transform ¶
Fit and transform with NearestImputer.
Purpose
Fits the underlying KNNImputer to the supplied feature matrix, imputes the same matrix immediately, and stores the imputed result on the class/object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix used for fitting and imputation. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Imputed feature matrix produced after fitting the nearest-neighbor imputer. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or fit-transform execution fails. |
Source code in imputers.py
IterativeImputer ¶
Bases: Imputer
Wrap IterativeImputer.
Purpose
Replaces missing values by modeling each feature with missing observations as a function of the other features and iterating through round-robin imputation rounds. The class/object stores iteration configuration, random state, the sklearn IterativeImputer instance, and the most recent transformed output.
Attributes:
| Name | Type | Description |
|---|---|---|
imputer |
IterativeImputer
|
Underlying iterative imputer. |
max_iter |
Optional[int]
|
Maximum number of imputation rounds. |
random_state |
Optional[int]
|
Random seed used by the imputer. |
transformed_data |
Optional[ndarray]
|
Most recent imputed matrix produced by the class/object. |
Source code in imputers.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 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 619 620 | |
train ¶
Fit IterativeImputer.
Purpose
Fits the underlying IterativeImputer to the supplied feature matrix and returns the current class/object for consistent preprocessing chains.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix used to learn iterative imputation models. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
IterativeImputer | None
|
IterativeImputer | None: Fitted iterative-imputer class/object. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or fitting fails. |
Source code in imputers.py
transform ¶
Transform with IterativeImputer.
Purpose
Replaces missing values in the supplied feature matrix using the fitted iterative imputation model and stores the imputed matrix on the class/object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix transformed by the fitted imputer. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Imputed feature matrix produced by the fitted iterative imputer. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or transformation fails. |
Source code in imputers.py
train_transform ¶
Fit and transform with IterativeImputer.
Purpose
Fits the underlying IterativeImputer to the supplied feature matrix, imputes the same matrix immediately, and stores the imputed result on the class/object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix used for fitting and imputation. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Imputed feature matrix produced after fitting the iterative imputer. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or fit-transform execution fails. |
Source code in imputers.py
SimpleImputer ¶
Bases: Imputer
Wrap strategy-based SimpleImputer.
Purpose
Replaces missing values using SimpleImputer strategies such as mean, median, most-frequent, or constant replacement. The class/object stores imputation configuration, missing-value indicator behavior, empty-feature behavior, and most recent transformed output.
Attributes:
| Name | Type | Description |
|---|---|---|
imputer |
SimpleImputer
|
Underlying simple imputer. |
transformed_data |
Optional[ndarray]
|
Most recent imputed matrix produced by the class/object. |
strategy |
Optional[str]
|
Imputation strategy used by the imputer. |
fill_value |
Optional[object]
|
Replacement value used for constant imputation. |
add_indicator |
bool
|
Flag indicating whether missing-value indicator columns are appended. |
keep_empty_features |
bool
|
Flag indicating whether entirely missing fitted features are preserved. |
Source code in imputers.py
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 | |
train ¶
Fit SimpleImputer.
Purpose
Fits the underlying SimpleImputer to the supplied feature matrix and returns the current class/object for consistent preprocessing chains.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix used to learn simple imputation statistics. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
SimpleImputer | None
|
SimpleImputer | None: Fitted simple-imputer class/object. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or fitting fails. |
Source code in imputers.py
transform ¶
Transform with SimpleImputer.
Purpose
Replaces missing values in the supplied feature matrix using fitted strategy-based statistics or constants and stores the imputed matrix on the class/object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix transformed by the fitted imputer. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Imputed feature matrix produced by the fitted simple imputer. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or transformation fails. |
Source code in imputers.py
train_transform ¶
Fit and transform with SimpleImputer.
Purpose
Fits the underlying SimpleImputer to the supplied feature matrix, imputes the same matrix immediately, and stores the imputed result on the class/object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature matrix used for fitting and imputation. |
required |
y
|
Optional[ndarray]
|
Optional target array accepted for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Imputed feature matrix produced after fitting the simple imputer. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or fit-transform execution fails. |
Source code in imputers.py
inverse_transform ¶
Invert SimpleImputer output.
Purpose
Reconstructs a feature matrix toward its pre-imputed form using inverse transformation when missing-value indicator columns were included during fitting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Transformed feature matrix to invert. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Reconstructed feature matrix produced by the imputer. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation fails, indicator columns are unavailable, or inverse transformation fails. |
ValueError
|
Raised when the |
Source code in imputers.py
throw_if ¶
Validate a required clustering argument.
Purpose
Enforces the presence of required clustering inputs before estimator execution. The validation accepts populated NumPy arrays and standard Python containers while rejecting null values and empty collections that would otherwise cause downstream operations to fail or produce undefined clustering results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Argument name used in the validation error message. |
required |
value
|
object
|
Argument value checked for a null or empty state. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
This function performs its work through side effects and does not return a value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
Raised when |