API Reference
This page provides automated documentation for the core components of newt.
Binning
newt.features.binning.binner
Unified binning interface.
Provides a single entry point for binning features using various algorithms.
Classes
Binner
Bases: BinnerStatsMixin, BinnerIOMixin
Unified interface for multi-feature binning using various algorithms.
The Binner class manages the discretization of multiple features, handles missing values automatically, and stores WOE encoders for downstream modeling. It supports both supervised (ChiMerge, Decision Tree, Optimal) and unsupervised (K-Means, Equal Width, Equal Frequency) algorithms.
Supported methods
- 'chi': ChiMerge (Default)
- 'dt': Decision Tree
- 'opt': Optimal Binning
- 'kmean': K-Means
- 'quantile': Equal Frequency
- 'step': Equal Width
Examples:
>>> from newt.features.binning import Binner
>>> binner = Binner()
>>> binner.fit(X_train, y_train, method='chi', n_bins=5, monotonic=True)
>>> # Access results via item access
>>> print(binner['age'].stats)
>>> binner['age'].plot()
>>> # Transform new data
>>> X_binned = binner.transform(X_test)
Source code in src/newt/features/binning/binner.py
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 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 352 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 484 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 621 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 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 | |
Attributes
woe_encoders_
property
Get WOE encoders dictionary (for backward compatibility).
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Mapping of feature names to WOEEncoder objects. |
Functions
__contains__(feature)
Check if feature is in binner.
Source code in src/newt/features/binning/binner.py
950 951 952 | |
__getitem__(feature)
Get binning result proxy for a feature.
Parameters
feature : str Feature name.
Returns
BinningResult Proxy object with stats and plot methods.
Source code in src/newt/features/binning/binner.py
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 | |
__init__()
Initialize the Binner.
Source code in src/newt/features/binning/binner.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
__iter__()
Iterate over feature names.
Source code in src/newt/features/binning/binner.py
954 955 956 | |
__len__()
Number of binned features.
Source code in src/newt/features/binning/binner.py
958 959 960 | |
features()
Get list of binned feature names.
Source code in src/newt/features/binning/binner.py
962 963 964 | |
fit(X, y=None, method='chi', n_bins=BINNING.DEFAULT_N_BINS, min_samples=None, cols=None, monotonic=None, show_progress=True, **kwargs)
Fit the binning model to multiple features.
Initializes and fits specific binning algorithms for each selected feature, calculates binning statistics, and stores WOE mappings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Data to be binned. |
required |
y
|
Optional[Union[Series, str]]
|
Target data or target column name. Required for supervised methods. |
None
|
method
|
str
|
Binning algorithm name ('chi', 'dt', 'opt', 'kmean', etc.). |
'chi'
|
n_bins
|
int
|
Target number of bins. |
DEFAULT_N_BINS
|
min_samples
|
Union[int, float, None]
|
Minimum samples threshold. - For 'dt': minimum samples per leaf. - For 'chi': float in (0, 1] means minimum bin proportion, int means minimum absolute samples per bin. |
None
|
cols
|
Optional[List[str]]
|
List of columns to bin. If None, all numeric columns are selected. |
None
|
monotonic
|
Union[bool, str, None]
|
Enforce monotonic bad rate trend. - True/'auto': Enforce auto-detected trend. - 'ascending'/'descending': Enforce specific trend. |
None
|
show_progress
|
bool
|
Whether to show a progress bar. |
True
|
**kwargs
|
Additional parameters passed to the underlying binner. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Binner |
Binner
|
The fitted Binner instance. |
Examples:
>>> binner.fit(df, target='default', method='chi', monotonic=True)
Source code in src/newt/features/binning/binner.py
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 621 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 | |
fit_woe(X, y, show_progress=True)
Calculate and update WOE mappings for all features.
Applicable when rules are loaded or manually set. This method updates WOE and IV statistics without changing existing split points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input DataFrame. |
required |
y
|
Union[Series, str]
|
Target data or target column name. |
required |
show_progress
|
bool
|
Whether to show a progress bar. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
Binner |
Binner
|
Self. |
Source code in src/newt/features/binning/binner.py
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 | |
stats()
Get dictionary of statistics for all features.
Source code in src/newt/features/binning/binner.py
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 | |
stats_plot()
Display stats and plot for all features.
Source code in src/newt/features/binning/binner.py
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 | |
transform(X, labels=False, show_progress=False)
Discretizes values based on splits discovered during fitting. Missing values are automatically assigned to a 'Missing' bin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Data to transform. |
required |
labels
|
bool
|
If True, return bin intervals (str). If False, return bin indices (int). |
False
|
show_progress
|
bool
|
Whether to show a progress bar. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Binned data with original columns replaced by bin codes/labels. |
Source code in src/newt/features/binning/binner.py
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 802 803 804 805 806 807 808 809 810 811 812 813 814 | |
woe_map()
Get WOE maps for all features.
Source code in src/newt/features/binning/binner.py
942 943 944 945 946 947 948 | |
woe_transform(X)
Convenience method to bin and WOE-transform data in one pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Raw feature DataFrame. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: WOE-encoded DataFrame. |
Examples:
>>> X_woe = binner.woe_transform(X_raw)
Source code in src/newt/features/binning/binner.py
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 | |
newt.features.binning.supervised
Classes
ChiMergeBinner
Bases: BaseBinner
Discretizes continuous data using the ChiMerge algorithm.
ChiMerge is a bottom-up merging algorithm that starts with each unique value as a bin and iteratively merges adjacent bins if they are statistically similar (based on Chi-square test).
Examples:
>>> binner = ChiMergeBinner(n_bins=5, alpha=0.05)
>>> binner.fit(X_series, y_series)
Source code in src/newt/features/binning/supervised.py
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 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 352 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 | |
Functions
__init__(n_bins=5, monotonic=None, alpha=0.05, min_samples=0.05, **kwargs)
Initialize ChiMergeBinner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_bins
|
int
|
Target number of bins. |
5
|
monotonic
|
Union[bool, str, None]
|
Enforce monotonic trend. |
None
|
alpha
|
float
|
Significance level for Chi-square test (merges if p > alpha). |
0.05
|
min_samples
|
Union[int, float]
|
Minimum samples per bin. Float is treated as fraction in (0, 1], int as absolute count. |
0.05
|
**kwargs
|
Arguments passed to BaseBinner. |
{}
|
Source code in src/newt/features/binning/supervised.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
DecisionTreeBinner
Bases: BaseBinner
Discretizes continuous data using a Decision Tree to find optimal splits.
Uses a classification tree to split the feature based on its relationship with the target variable. This method naturally finds boundaries that maximize separation between classes.
Examples:
>>> binner = DecisionTreeBinner(n_bins=5, min_samples_leaf=0.1)
>>> binner.fit(X_series, y_series)
>>> print(binner.splits_)
Source code in src/newt/features/binning/supervised.py
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 | |
Functions
__init__(n_bins=5, monotonic=None, min_samples_leaf=0.05, **kwargs)
Initialize DecisionTreeBinner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_bins
|
int
|
Maximum number of bins (max_leaf_nodes). |
5
|
monotonic
|
Union[bool, str, None]
|
Enforce monotonic trend. |
None
|
min_samples_leaf
|
float
|
Minimum fraction of samples required in a leaf. |
0.05
|
**kwargs
|
Arguments passed to BaseBinner. |
{}
|
Source code in src/newt/features/binning/supervised.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
OptBinningBinner
Bases: BaseBinner
Discretizes continuous data using the optbinning library.
Provides a wrapper for the Optimal Binning algorithm which uses constrained programming to find splits that optimize information value (IV).
Note: Requires optbinning and is only available on Python < 3.12.
Examples:
>>> binner = OptBinningBinner(n_bins=5, monotonic='ascending')
>>> binner.fit(X, y)
Source code in src/newt/features/binning/supervised.py
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 | |
Functions
__init__(n_bins=5, monotonic=None, **kwargs)
Initialize OptBinningBinner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_bins
|
int
|
Maximum number of bins. |
5
|
monotonic
|
Union[bool, str, None]
|
Monotonic constraint setting. |
None
|
**kwargs
|
Arguments passed to |
{}
|
Source code in src/newt/features/binning/supervised.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
newt.features.binning.unsupervised
Classes
EqualWidthBinner
Bases: BaseBinner
Discretizes continuous data into intervals of equal width.
This method divides the range of values into 'n_bins' equal-sized intervals. Useful for uniform distributions or when the physical scale of the feature is the primary concern.
Examples:
>>> binner = EqualWidthBinner(n_bins=5)
>>> binner.fit(X_series)
Source code in src/newt/features/binning/unsupervised.py
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 | |
Functions
__init__(**kwargs)
Initialize EqualWidthBinner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Arguments passed to BaseBinner. |
{}
|
Source code in src/newt/features/binning/unsupervised.py
21 22 23 24 25 26 27 | |
EqualFrequencyBinner
Bases: BaseBinner
Discretizes continuous data into intervals with an equal number of samples.
Also known as quantile binning. This method ensures that each bin contains approximately the same number of observations.
Examples:
>>> binner = EqualFrequencyBinner(n_bins=5)
>>> binner.fit(X_series)
Source code in src/newt/features/binning/unsupervised.py
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 67 68 69 70 71 | |
Functions
__init__(**kwargs)
Initialize EqualFrequencyBinner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Arguments passed to BaseBinner. |
{}
|
Source code in src/newt/features/binning/unsupervised.py
53 54 55 56 57 58 59 | |
KMeansBinner
Bases: BaseBinner
Discretizes continuous data using K-Means clustering.
This method finds 'n_bins' clusters in the 1D space and chooses boundaries as the midpoints between adjacent cluster centers.
Examples:
>>> binner = KMeansBinner(n_bins=5)
>>> binner.fit(X_series)
Source code in src/newt/features/binning/unsupervised.py
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 | |
Functions
__init__(**kwargs)
Initialize KMeansBinner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Arguments passed to BaseBinner. |
{}
|
Source code in src/newt/features/binning/unsupervised.py
85 86 87 88 89 90 91 | |
Feature Selection
newt.features.selection.selector
Compatibility facade around feature analysis and feature filtering.
Classes
FeatureSelector
Unified tool for exploratory data analysis (EDA) and feature filtering.
The FeatureSelector calculates various feature-level metrics (IV, KS, correlation, missing rates) and provides a simple interface to filter features based on business thresholds.
Attributes:
| Name | Type | Description |
|---|---|---|
metrics |
Set[str]
|
The set of metrics calculated by the selector. |
eda_summary_ |
DataFrame
|
Summary table of calculated statistics. |
selected_features_ |
List[str]
|
List of column names that passed selection. |
removed_features_ |
Dict[str, str]
|
Mapping of removed features to the reason. |
corr_removed_ |
List[str]
|
List of features removed due to high correlation. |
Examples:
>>> from newt.features.selection import FeatureSelector
>>> selector = FeatureSelector(metrics=['iv', 'missing_rate', 'correlation'])
>>> selector.fit(X_train, y_train)
>>> selector.select(iv_threshold=0.02, corr_threshold=0.8)
>>> X_filtered = selector.transform(X_train)
Source code in src/newt/features/selection/selector.py
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 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 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 | |
Attributes
corr_matrix
property
Get the feature-to-feature correlation matrix.
Functions
__init__(metrics=None, iv_bins=BINNING.DEFAULT_BUCKETS, lift_k=0.1, corr_method='pearson', engine='auto')
Initialize the FeatureSelector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
Optional[List[str]]
|
Metrics to calculate. Options: 'iv', 'missing_rate', 'ks', 'correlation', 'lift'. If None, uses a default set. |
None
|
iv_bins
|
int
|
Number of bins for initial IV calculation. |
DEFAULT_BUCKETS
|
lift_k
|
float
|
Fraction of population to use for Lift calculation (e.g., top 10%). |
0.1
|
corr_method
|
str
|
Correlation method ('pearson', 'spearman', 'kendall'). |
'pearson'
|
engine
|
str
|
Execution engine ('auto', 'rust', 'python'). |
'auto'
|
Source code in src/newt/features/selection/selector.py
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 67 68 69 70 71 72 73 | |
fit(X, y=None)
Calculate feature statistics for the input DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input dataset. |
required |
y
|
Optional[Series]
|
Target binary labels. Required for supervised metrics like IV or KS. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
FeatureSelector |
FeatureSelector
|
The fitted selector instance. |
Source code in src/newt/features/selection/selector.py
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 | |
report()
Generate a report combining EDA stats and selection status.
Source code in src/newt/features/selection/selector.py
138 139 140 141 142 143 144 145 146 147 | |
select(iv_threshold=FILTERING.DEFAULT_IV_THRESHOLD, missing_threshold=FILTERING.DEFAULT_MISSING_THRESHOLD, corr_threshold=FILTERING.DEFAULT_CORR_THRESHOLD)
Filter features based on thresholds for IV, missing rate, and correlation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iv_threshold
|
float
|
Minimum Information Value (IV) to keep a feature. |
DEFAULT_IV_THRESHOLD
|
missing_threshold
|
float
|
Maximum missing rate (fraction) to keep a feature. |
DEFAULT_MISSING_THRESHOLD
|
corr_threshold
|
float
|
Maximum absolute correlation coefficient. If a pair exceeds this, the one with lower IV is removed. |
DEFAULT_CORR_THRESHOLD
|
Returns:
| Name | Type | Description |
|---|---|---|
FeatureSelector |
FeatureSelector
|
The selector instance after selection. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If called before fit(). |
Source code in src/newt/features/selection/selector.py
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 | |
transform(X)
Return X with only selected features.
Source code in src/newt/features/selection/selector.py
149 150 151 152 153 154 | |
newt.features.selection.stepwise
Stepwise regression feature selection.
Provides forward, backward, and bidirectional stepwise selection based on statistical significance (p-values) or information criteria (AIC/BIC).
Classes
StepwiseSelector
Stepwise regression feature selector.
Uses hypothesis testing to select optimal features for logistic regression. Supports forward selection, backward elimination, and bidirectional stepwise.
This is typically used after WOE transformation and before final model building.
Examples
selector = StepwiseSelector(direction='both', criterion='aic') selector.fit(X_woe, y) X_selected = selector.transform(X_woe) print(selector.selected_features_)
Source code in src/newt/features/selection/stepwise.py
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 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 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 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 352 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 484 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 621 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 | |
Functions
__init__(direction='both', criterion='aic', p_enter=MODELING.DEFAULT_P_ENTER, p_remove=MODELING.DEFAULT_P_REMOVE, max_iter=100, fit_intercept=True, exclude=None, engine='auto', verbose=True)
Initialize StepwiseSelector.
Parameters
direction : str Selection direction: - 'forward': Start with no features, add one at a time - 'backward': Start with all features, remove one at a time - 'both': Bidirectional stepwise (forward + backward) Default 'both'. criterion : str Selection criterion: - 'pvalue': Use p-value for selection - 'aic': Use Akaike Information Criterion - 'bic': Use Bayesian Information Criterion Default 'aic'. p_enter : float P-value threshold for entering a feature. Default 0.05. Used when direction='forward' or 'both'. p_remove : float P-value threshold for removing a feature. Default 0.10. Used when direction='backward' or 'both'. max_iter : int Maximum iterations. Default 100. fit_intercept : bool Whether to include intercept. Default True. exclude : List[str], optional Features to always keep in the model (force include). engine : str Computation engine: 'auto', 'rust', or 'python'. Defaults to 'auto' (prefer Rust, fallback to Python when Rust is unavailable). verbose : bool Whether to show progress bars during selection.
Source code in src/newt/features/selection/stepwise.py
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 | |
fit(X, y)
Fit the stepwise selector.
Parameters
X : pd.DataFrame Feature data (typically WOE transformed). y : pd.Series Binary target variable (0/1).
Returns
StepwiseSelector Fitted instance.
Source code in src/newt/features/selection/stepwise.py
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 213 214 215 216 217 | |
fit_transform(X, y)
Fit and transform in one step.
Source code in src/newt/features/selection/stepwise.py
722 723 724 725 | |
report()
Generate selection report.
Returns
pd.DataFrame Selection history with iterations, actions, and criteria values.
Source code in src/newt/features/selection/stepwise.py
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 | |
summary()
Get selection summary.
Returns
str Summary of stepwise selection results.
Source code in src/newt/features/selection/stepwise.py
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 | |
transform(X)
Filter columns based on fitted selection.
Parameters
X : pd.DataFrame Data to transform.
Returns
pd.DataFrame Filtered data with only selected features.
Source code in src/newt/features/selection/stepwise.py
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 | |
Modeling
newt.modeling.logistic
Logistic Regression model wrapper using statsmodels.
Provides a scikit-learn-like interface for statsmodels Logit.
Classes
LogisticModel
Logistic Regression model wrapper using statsmodels.
Provides a familiar fit/predict interface while leveraging statsmodels for detailed statistical output (p-values, confidence intervals, etc.).
Examples
model = LogisticModel() model.fit(X_woe, y) print(model.summary()) predictions = model.predict_proba(X_woe)
Source code in src/newt/modeling/logistic.py
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 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 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 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 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 352 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 484 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 621 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 | |
Functions
__init__(fit_intercept=True, method='bfgs', maxiter=100, regularization=None, alpha=0.0, **kwargs)
Initialize LogisticModel.
Parameters
fit_intercept : bool Whether to fit an intercept term. Default True. method : str Optimization method for statsmodels. Default 'bfgs'. Options: 'newton', 'bfgs', 'lbfgs', 'powell', 'cg', 'ncg'. maxiter : int Maximum iterations for optimization. Default 100. regularization : str, optional Regularization type: 'l1' or 'l2'. Default None (no regularization). alpha : float Regularization strength. Default 0.0. **kwargs Additional arguments passed to statsmodels fit method.
Source code in src/newt/modeling/logistic.py
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 67 68 69 70 71 72 73 74 75 76 77 78 | |
dump(path)
Dump the model payload to a JSON file.
Parameters
path : Union[str, Path] Output JSON path.
Source code in src/newt/modeling/logistic.py
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
fit(X, y, sample_weight=None)
Fit the logistic regression model.
Parameters
X : pd.DataFrame Feature data (typically WOE transformed). y : pd.Series Binary target variable (0/1). sample_weight : np.ndarray, optional Sample weights. Not directly supported by statsmodels Logit, but can be approximated using frequency weights.
Returns
LogisticModel Fitted instance.
Source code in src/newt/modeling/logistic.py
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 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 | |
from_dict(payload)
classmethod
Restore a fitted LogisticModel from serialized payload.
Parameters
payload : Dict[str, Any]
Dictionary exported by to_dict.
Returns
LogisticModel Restored fitted model instance.
Source code in src/newt/modeling/logistic.py
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 | |
get_coefficients()
Get coefficients DataFrame.
Returns
pd.DataFrame DataFrame with coefficient details.
Source code in src/newt/modeling/logistic.py
461 462 463 464 465 466 467 468 469 470 471 | |
get_significant_features(p_threshold=MODELING.DEFAULT_P_ENTER)
Get features with p-value below threshold.
Parameters
p_threshold : float P-value threshold. Default 0.05.
Returns
pd.DataFrame Significant coefficients.
Source code in src/newt/modeling/logistic.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |
load(path)
classmethod
Load a model payload from JSON file.
Parameters
path : Union[str, Path] Input JSON path.
Returns
LogisticModel Restored fitted model instance.
Source code in src/newt/modeling/logistic.py
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | |
predict(X, threshold=MODELING.DEFAULT_CLASSIFICATION_THRESHOLD)
Predict class labels.
Parameters
X : pd.DataFrame Feature data. threshold : float Classification threshold. Default 0.5.
Returns
np.ndarray Predicted class labels (0 or 1).
Source code in src/newt/modeling/logistic.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
predict_proba(X)
Predict probability of positive class.
Parameters
X : pd.DataFrame Feature data.
Returns
np.ndarray Predicted probabilities for positive class.
Source code in src/newt/modeling/logistic.py
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 | |
summary()
Get statsmodels summary.
Returns
str Model summary as string.
Source code in src/newt/modeling/logistic.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
to_dict()
Export model parameters as dictionary.
Returns
Dict Model parameters including coefficients.
Source code in src/newt/modeling/logistic.py
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 | |
newt.modeling.scorecard
Scorecard facade that builds and scores reusable specifications.
Classes
Scorecard
Scorecard generator from logistic regression model coefficients.
The Scorecard class converts the continuous probability output of a logistic regression model into an additive point-based scoring system. It manages scaliing parameters (base score, PDO) and provides methods for scoring new data, exporting definitions, and summarizing findings.
Attributes:
| Name | Type | Description |
|---|---|---|
base_score |
int
|
The target score at 'base_odds'. |
pdo |
int
|
Points to Double the Odds. |
base_odds |
float
|
The odds (Good:Bad) at 'base_score'. |
factor |
float
|
Calculated scaling factor. |
offset |
float
|
Calculated scaling offset. |
Source code in src/newt/modeling/scorecard.py
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 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 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 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 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 352 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 484 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 | |
Functions
__init__(base_score=SCORECARD.DEFAULT_BASE_SCORE, pdo=SCORECARD.DEFAULT_PDO, base_odds=SCORECARD.DEFAULT_BASE_ODDS, points_decimals=None)
Initialize the Scorecard instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_score
|
int
|
Target score at the given base_odds. |
DEFAULT_BASE_SCORE
|
pdo
|
int
|
Points to Double the Odds (PDO). |
DEFAULT_PDO
|
base_odds
|
float
|
Target odds at the given base_score. |
DEFAULT_BASE_ODDS
|
points_decimals
|
Optional[int]
|
Optional decimal precision for scorecard points. |
None
|
Source code in src/newt/modeling/scorecard.py
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 67 68 69 70 71 72 73 74 75 | |
dump(path)
Dump scorecard payload to a JSON file.
Source code in src/newt/modeling/scorecard.py
229 230 231 232 233 234 235 | |
export()
Export the scorecard as a single flat DataFrame.
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: A DataFrame containing bin ranges and corresponding points for all features. |
Source code in src/newt/modeling/scorecard.py
205 206 207 208 209 210 211 212 213 214 | |
from_dict(payload)
Restore a scorecard from a serialized specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
Dict[str, object]
|
A dictionary representing a serialized ScorecardSpec. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Scorecard |
Scorecard
|
The restored Scorecard instance. |
Source code in src/newt/modeling/scorecard.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
from_model(model, binner, *, keep_training_artifacts=False)
Build a scorecard from a fitted model and its binning/encoding artifacts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
ScorecardModelInput
|
A fitted model object (scikit-learn, statsmodels, or dict). |
required |
binner
|
BinnerArtifact
|
A fitted Binner instance. |
required |
keep_training_artifacts
|
bool
|
Whether to keep direct runtime references to the original model and binner objects. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Scorecard |
Scorecard
|
The built Scorecard instance. |
Examples:
>>> scorecard = Scorecard(base_score=600, pdo=20)
>>> scorecard.from_model(lr_model, binner)
Source code in src/newt/modeling/scorecard.py
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
load(path)
classmethod
Load scorecard from a JSON file.
Source code in src/newt/modeling/scorecard.py
237 238 239 240 241 242 | |
score(X)
Calculate scores for input raw data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input DataFrame containing raw (un-binned) features. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
pd.Series: Calculated scores for each row. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the scorecard has not been built. |
Source code in src/newt/modeling/scorecard.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
summary()
Generate a human-readable summary of the scorecard configuration and points.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The summary text. |
Source code in src/newt/modeling/scorecard.py
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 | |
to_dict()
Export the scorecard specification as a serializable dictionary.
Returns:
| Type | Description |
|---|---|
Dict[str, object]
|
Dict[str, object]: The scorecard definition payload. |
Source code in src/newt/modeling/scorecard.py
216 217 218 219 220 221 222 223 224 225 226 227 | |
to_sql(table_name='input_table', score_alias='score', include_breakdown=False)
Render the scorecard as an ANSI SQL scoring query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Source table name used in the FROM clause. |
'input_table'
|
score_alias
|
str
|
Alias of the output score column. |
'score'
|
include_breakdown
|
bool
|
Whether to include per-feature points columns. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
ANSI SQL query for score calculation. |
Source code in src/newt/modeling/scorecard.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
Pipeline
newt.pipeline.pipeline
Scorecard pipeline implemented as a thin coordinator around step objects.
Classes
ScorecardPipeline
Chainable pipeline for end-to-end credit scorecard development.
The ScorecardPipeline provides a fluent, high-level API to orchestrate the entire modeling workflow—from initial feature filtering to final scorecard generation. It manages internal state transitions and provides access to intermediate artifacts (e.g., binning results, WOE encoders) at each step.
Examples:
>>> from newt.pipeline import ScorecardPipeline
>>> pipeline = (
... ScorecardPipeline(X_train, y_train, X_test, y_test)
... .prefilter(iv_threshold=0.02)
... .bin(method='chi', n_bins=5)
... .woe_transform()
... .postfilter(psi_threshold=0.1)
... .build_model()
... .generate_scorecard(base_score=600, pdo=20)
... )
>>> scores = pipeline.score(X_val)
Source code in src/newt/pipeline/pipeline.py
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 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 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 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 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 352 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 484 485 486 487 488 489 490 491 492 493 | |
Attributes
X_binned_
property
writable
Training data after binning transformation.
X_current
property
Get the current training feature set (after transformations).
X_test_current
property
Get the current test feature set (after transformations).
X_woe_
property
writable
Training data after WOE transformation.
binner
property
Alias for binner_.
binner_
property
writable
The Binner instance from the bin step.
model
property
Alias for model_.
model_
property
writable
The fitted LogisticModel instance.
postfilter_
property
writable
The PostFilter instance from the postfilter step.
postfilter_result
property
Alias for postfilter_.
prefilter_
property
writable
The FeatureSelector instance from the prefilter step.
prefilter_result
property
Alias for prefilter_.
scorecard
property
Alias for scorecard_.
scorecard_
property
writable
The generated Scorecard instance.
selected_features
property
Get the list of features currently selected in the pipeline.
steps_
property
List of step names that have been executed.
stepwise_
property
writable
The StepwiseSelector instance from the stepwise step.
woe_encoders
property
Alias for woe_encoders_.
woe_encoders_
property
writable
Dictionary mapping feature names to WOEEncoder instances.
y_test
property
Get the test target series.
Functions
__init__(X, y, X_test=None, y_test=None)
Initialize the pipeline with training and optional testing data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Training feature DataFrame. |
required |
y
|
Series
|
Training target Series (binary 0/1). |
required |
X_test
|
Optional[DataFrame]
|
Optional testing feature DataFrame for validation and PSI calculation. |
None
|
y_test
|
Optional[Series]
|
Optional testing target Series. |
None
|
Source code in src/newt/pipeline/pipeline.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
bin(method='chi', n_bins=BINNING.DEFAULT_N_BINS, cols=None, **kwargs)
Discretize continuous variables into discrete bins.
Supported methods include 'chi' (ChiMerge), 'dt' (Decision Tree), 'opt' (Optimal), 'quantile' (Equal Frequency), 'step' (Equal Width), and 'kmean'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
Binning algorithm name. Defaults to 'chi'. |
'chi'
|
n_bins
|
int
|
Target number of bins for each feature. |
DEFAULT_N_BINS
|
cols
|
Optional[List[str]]
|
Optional list of features to bin. If None, all numeric features are used. |
None
|
**kwargs
|
Additional parameters for the chosen binner (e.g., monotonic=True). |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ScorecardPipeline |
'ScorecardPipeline'
|
The pipeline instance (self) for chaining. |
Examples:
>>> pipeline.bin(method='opt', n_bins=5, monotonic='auto')
Source code in src/newt/pipeline/pipeline.py
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 | |
build_model(fit_intercept=True, **kwargs)
Train the final logistic regression model on selected WOE features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fit_intercept
|
bool
|
Whether to calculate the intercept for this model. |
True
|
**kwargs
|
Additional parameters passed to LogisticModel. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ScorecardPipeline |
'ScorecardPipeline'
|
The pipeline instance (self) for chaining. |
Examples:
>>> pipeline.build_model(method='bfgs')
Source code in src/newt/pipeline/pipeline.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
generate_scorecard(base_score=SCORECARD.DEFAULT_BASE_SCORE, pdo=SCORECARD.DEFAULT_PDO, base_odds=SCORECARD.DEFAULT_BASE_ODDS, points_decimals=None, **kwargs)
Convert the fitted logistic model into a point-based scorecard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_score
|
int
|
The target score at 'base_odds'. |
DEFAULT_BASE_SCORE
|
pdo
|
int
|
Points to Double the Odds. |
DEFAULT_PDO
|
base_odds
|
float
|
The odds (Good:Bad) at 'base_score'. |
DEFAULT_BASE_ODDS
|
points_decimals
|
Optional[int]
|
Optional decimal precision for scorecard points. |
None
|
**kwargs
|
Additional parameters passed to Scorecard. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ScorecardPipeline |
'ScorecardPipeline'
|
The pipeline instance (self) for chaining. |
Examples:
>>> pipeline.generate_scorecard(base_score=600, pdo=20)
Source code in src/newt/pipeline/pipeline.py
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 | |
postfilter(psi_threshold=FILTERING.DEFAULT_PSI_THRESHOLD, vif_threshold=FILTERING.DEFAULT_VIF_THRESHOLD, X_test=None, **kwargs)
Apply post-transformation filters like PSI stability and VIF multicollinearity.
Typically run after WOE transformation to ensure the selected features are stable over time (PSI) and not redundant (VIF).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psi_threshold
|
float
|
Maximum allowed Population Stability Index between train/test. |
DEFAULT_PSI_THRESHOLD
|
vif_threshold
|
float
|
Maximum allowed Variance Inflation Factor. |
DEFAULT_VIF_THRESHOLD
|
X_test
|
Optional[DataFrame]
|
Optional override for the test set used for PSI compute. |
None
|
**kwargs
|
Additional parameters passed to PostFilter. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ScorecardPipeline |
'ScorecardPipeline'
|
The pipeline instance (self) for chaining. |
Examples:
>>> pipeline.postfilter(psi_threshold=0.1, vif_threshold=5.0)
Source code in src/newt/pipeline/pipeline.py
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 | |
prefilter(iv_threshold=FILTERING.DEFAULT_IV_THRESHOLD, missing_threshold=FILTERING.DEFAULT_MISSING_THRESHOLD, corr_threshold=FILTERING.DEFAULT_CORR_THRESHOLD, iv_bins=BINNING.DEFAULT_BUCKETS, **kwargs)
Apply pre-modeling filters based on EDA metrics.
Filters features using Information Value (IV), missing rate, and feature-to-feature correlation. This step is typically the first in the pipeline to reduce dimensionality before expensive operations like binning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iv_threshold
|
float
|
Minimum IV required to keep a feature. |
DEFAULT_IV_THRESHOLD
|
missing_threshold
|
float
|
Maximum allowed missing rate (0.0 to 1.0). |
DEFAULT_MISSING_THRESHOLD
|
corr_threshold
|
float
|
Maximum allowed correlation between feature pairs. |
DEFAULT_CORR_THRESHOLD
|
iv_bins
|
int
|
Number of buckets used for temporary auto-binning during IV compute. |
DEFAULT_BUCKETS
|
**kwargs
|
Additional arguments passed to FeatureSelector. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ScorecardPipeline |
'ScorecardPipeline'
|
The pipeline instance (self) for chaining. |
Examples:
>>> pipeline.prefilter(iv_threshold=0.05, corr_threshold=0.7)
Source code in src/newt/pipeline/pipeline.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 | |
score(X)
Apply the finished scorecard to new raw data to produce scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Raw feature DataFrame (un-binned, un-encoded). |
required |
Returns:
| Type | Description |
|---|---|
Series
|
pd.Series: Calculated scores for each row. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the scorecard has not been generated yet. |
Source code in src/newt/pipeline/pipeline.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
stepwise(direction='both', criterion='aic', p_enter=MODELING.DEFAULT_P_ENTER, p_remove=MODELING.DEFAULT_P_REMOVE, exclude=None, **kwargs)
Perform automated feature selection via stepwise regression.
Successively adds or removes features based on statistical significance or information criteria (AIC/BIC).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
direction
|
str
|
Search direction: 'forward', 'backward', or 'both'. |
'both'
|
criterion
|
str
|
Selection criterion: 'p-value', 'aic', or 'bic'. |
'aic'
|
p_enter
|
float
|
P-value threshold to enter the model (if using 'p-value'). |
DEFAULT_P_ENTER
|
p_remove
|
float
|
P-value threshold to be removed from the model. |
DEFAULT_P_REMOVE
|
exclude
|
Optional[List[str]]
|
Optional list of features to always keep in the model. |
None
|
**kwargs
|
Additional parameters passed to StepwiseSelector. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ScorecardPipeline |
'ScorecardPipeline'
|
The pipeline instance (self) for chaining. |
Examples:
>>> pipeline.stepwise(direction='both', criterion='aic')
Source code in src/newt/pipeline/pipeline.py
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 | |
summary()
Get pipeline summary.
Source code in src/newt/pipeline/pipeline.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | |
woe_transform(epsilon=BINNING.DEFAULT_EPSILON, **kwargs)
Apply Weight of Evidence (WOE) encoding to binned features.
Converts binned categorical/ordinal values into numeric WOE values based on the distribution of good and bad labels in each bin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epsilon
|
float
|
Small constant to prevent log(0) or division by zero. |
DEFAULT_EPSILON
|
**kwargs
|
Additional arguments passed to WOEEncoder. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ScorecardPipeline |
'ScorecardPipeline'
|
The pipeline instance (self) for chaining. |
Examples:
>>> pipeline.woe_transform(epsilon=1e-10)
Source code in src/newt/pipeline/pipeline.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
Reporting
newt.reporting.report
Public report orchestration API.
Classes
Report
dataclass
Orchestrator for generating multi-sheet Excel model reports.
The Report class serves as the primary entry point for creating professional, styled Excel workbooks that summarize model performance, variable distributions, and dimensional comparisons.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
DataFrame
|
The input dataset containing scores, labels, and features. |
model |
object
|
A fitted model object (scikit-learn, LightGBM, XGBoost, etc.) used to extract feature importance and parameters. |
tag |
str
|
Column name identifying sample segments (e.g., 'train', 'oot'). |
score_col |
str
|
Column name for the primary model score to be analyzed. |
date_col |
str
|
Column name for the observation date (used for monthly trends). |
label_list |
Sequence[str]
|
List of target column names (binary 0/1). |
score_list |
Sequence[str]
|
Optional list of secondary/benchmark scores. |
dim_list |
Sequence[str]
|
Optional list of columns for dimensional comparison. |
var_list |
Sequence[str]
|
Optional list of columns for portrait/feature analysis. |
sheet_list |
Sequence[object]
|
Optional list of sheets to include (names or indices). |
feature_df |
DataFrame
|
Feature dictionary DataFrame used for variable metadata mapping. |
report_out_path |
str
|
File path where the Excel workbook will be saved. |
engine |
str
|
Calculation engine: 'auto' (default), 'rust', or 'python'. |
max_workers |
int
|
Maximum parallel workers for computation. |
parallel_sheets |
bool
|
Whether to calculate different sheets in parallel. |
memory_mode |
str
|
Memory usage strategy: 'compact' (default) or 'standard'. |
metrics_mode |
str
|
Calculation mode: 'exact' (default) or 'binned' (approximate). |
prin_bal_amount_col |
str
|
Column name for principal-balance amount used by optional amount-based report metrics. |
loan_amount_col |
str
|
Column name for loan amount used by optional amount-based report metrics. |
Examples:
>>> from newt import Report
>>> report = Report(
... data=df,
... model=fitted_model,
... tag="segment",
... score_col="new_score",
... date_col="report_date",
... label_list=["target"],
... report_out_path="./final_report.xlsx"
... )
>>> report.generate()
Source code in src/newt/reporting/report.py
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 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 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 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 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 | |
Functions
generate()
Generate the report and return the output path.
Source code in src/newt/reporting/report.py
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 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 224 225 226 227 228 229 | |
newt.reporting.interactive
Interactive reporting wrappers for use in Jupyter Notebooks.
Functions
calculate_tag_metrics(data, tag_col, date_col, label_list, score_model_columns, metrics_mode='exact', score_type='auto', prin_bal_amount_col=None, loan_amount_col=None)
Calculate split performance metrics by tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Input DataFrame containing the predictions. |
required |
tag_col
|
str
|
Column name indicating sample set (e.g., 'train', 'oot'). |
required |
date_col
|
str
|
Date column name; used to build tag observation windows. |
required |
label_list
|
Sequence[str]
|
List of label column names. |
required |
score_model_columns
|
Sequence[Tuple[str, str]]
|
List of (model_name, score_column) tuples. |
required |
metrics_mode
|
str
|
'exact' or 'binned'. Default is 'exact'. |
'exact'
|
score_type
|
str
|
Score semantics: 'auto', 'probability' (higher=more risky), or 'score' (higher=less risky). |
'auto'
|
prin_bal_amount_col
|
Optional[str]
|
Optional principal-balance amount column. |
None
|
loan_amount_col
|
Optional[str]
|
Optional total-loan amount column. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame containing metrics grouped by tag. |
Source code in src/newt/reporting/interactive.py
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 | |
calculate_month_metrics(data, date_col, label_list, score_model_columns, metrics_mode='exact', score_type='auto', prin_bal_amount_col=None, loan_amount_col=None)
Calculate performance metrics by month using the first month as PSI base.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Input DataFrame containing the predictions. |
required |
date_col
|
str
|
Date column name; used to automatically generate month column. |
required |
label_list
|
Sequence[str]
|
List of label column names. |
required |
score_model_columns
|
Sequence[Tuple[str, str]]
|
List of (model_name, score_column) tuples. |
required |
metrics_mode
|
str
|
'exact' or 'binned'. Default is 'exact'. |
'exact'
|
score_type
|
str
|
Score semantics: 'auto', 'probability' (higher=more risky), or 'score' (higher=less risky). |
'auto'
|
prin_bal_amount_col
|
Optional[str]
|
Optional principal-balance amount column. |
None
|
loan_amount_col
|
Optional[str]
|
Optional total-loan amount column. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame containing metrics grouped by auto-derived month. |
Source code in src/newt/reporting/interactive.py
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 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 | |
calculate_split_metrics(data, tag_col, date_col, label_list, score_col=None, model_name=None, metrics_mode='exact', score_type='auto', prin_bal_amount_col=None, loan_amount_col=None, *, score_model_columns=None)
Calculate split performance metrics by tag and month.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Input DataFrame containing the predictions. |
required |
tag_col
|
str
|
Column name indicating sample set (e.g., 'train', 'oot'). |
required |
date_col
|
str
|
Date column name; used to automatically generate month column. |
required |
label_list
|
Sequence[str]
|
List of label column names. |
required |
score_col
|
Optional[str]
|
Legacy score column name. Use score_model_columns for new code. |
None
|
model_name
|
Optional[str]
|
Legacy model name. Use score_model_columns for new code. |
None
|
score_model_columns
|
Optional[Sequence[Tuple[str, str]]]
|
List of (model_name, score_column) tuples. |
None
|
metrics_mode
|
str
|
'exact' or 'binned'. Default is 'exact'. |
'exact'
|
score_type
|
str
|
Score semantics: 'auto', 'probability' (higher=more risky), or 'score' (higher=less risky). |
'auto'
|
prin_bal_amount_col
|
Optional[str]
|
Optional principal-balance amount column. |
None
|
loan_amount_col
|
Optional[str]
|
Optional total-loan amount column. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[DataFrame, DataFrame]
|
Tuple of two DataFrames: - tag_df: Metrics grouped by tag. - month_df: Metrics grouped by auto-derived month. |
Source code in src/newt/reporting/interactive.py
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 | |
calculate_dimensional_comparison(data, dim_list, label_list, score_model_columns, metrics_mode='exact', score_type='auto', prin_bal_amount_col=None, loan_amount_col=None)
Calculate dimensional comparison metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Input DataFrame. |
required |
dim_list
|
Sequence[str]
|
List of dimension column names to split by. |
required |
label_list
|
Sequence[str]
|
List of label column names. |
required |
score_model_columns
|
Sequence[Tuple[str, str]]
|
List of (model_name, score_column) tuples. |
required |
metrics_mode
|
str
|
'exact' or 'binned'. Default is 'exact'. |
'exact'
|
score_type
|
str
|
Score semantics: 'auto', 'probability' (higher=more risky), or 'score' (higher=less risky). |
'auto'
|
prin_bal_amount_col
|
Optional[str]
|
Optional principal-balance amount column. |
None
|
loan_amount_col
|
Optional[str]
|
Optional total-loan amount column. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame containing metrics grouped by dimensions. |
Source code in src/newt/reporting/interactive.py
613 614 615 616 617 618 619 620 621 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 | |
calculate_model_comparison(data, tag_col, date_col, label_list, model_columns, group_mode='month', metrics_mode='exact', score_type='auto', prin_bal_amount_col=None, loan_amount_col=None)
Compare multiple models directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Input DataFrame. |
required |
tag_col
|
str
|
Column name indicating sample set (e.g., 'train', 'oot'). |
required |
date_col
|
str
|
Date column name; used to generate month column. |
required |
label_list
|
Sequence[str]
|
List of label column names. |
required |
model_columns
|
Sequence[Tuple[str, str]]
|
List of (model_name, score_column) tuples. |
required |
group_mode
|
str
|
Mode to group by, either 'month' or 'tag'. Default is 'month'. |
'month'
|
metrics_mode
|
str
|
'exact' or 'binned'. Default is 'exact'. |
'exact'
|
score_type
|
str
|
Score semantics: 'auto', 'probability' (higher=more risky), or 'score' (higher=less risky). |
'auto'
|
prin_bal_amount_col
|
Optional[str]
|
Optional principal-balance amount column. |
None
|
loan_amount_col
|
Optional[str]
|
Optional total-loan amount column. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame containing model comparison metrics. |
Source code in src/newt/reporting/interactive.py
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 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 | |
calculate_bin_metrics(data, label_col, score_col, q=10, bins=None, prin_bal_amount_col=None, loan_amount_col=None)
Calculate bin-level sample and optional amount metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Input DataFrame. |
required |
label_col
|
str
|
Binary label column name. |
required |
score_col
|
str
|
Score column name. |
required |
q
|
int
|
Number of quantile bins when |
10
|
bins
|
Optional[Sequence[float]]
|
Optional custom split edges. |
None
|
prin_bal_amount_col
|
Optional[str]
|
Optional principal-balance amount column. |
None
|
loan_amount_col
|
Optional[str]
|
Optional total-loan amount column. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame containing per-bin sample metrics and optional amount metrics. |
Source code in src/newt/reporting/interactive.py
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 | |