Tutorial: optimal piecewise binning with binary target
Basic
To get us started, let’s load a well-known dataset from the UCI repository and transform the data into a pandas.DataFrame.
[1]:
import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import GradientBoostingClassifier
[2]:
data = load_breast_cancer()
df = pd.DataFrame(data.data, columns=data.feature_names)
We choose a variable to discretize and the binary target.
[3]:
variable = "mean radius"
x = df[variable].values
y = data.target
Import and instantiate an OptimalPWBinning object class and we pass the variable name. The OptimalPWBinning can ONLY handle numerical variables. This differs from the OptimalBinning object class.
[4]:
from optbinning import OptimalPWBinning
[5]:
optb = OptimalPWBinning(name=variable)
We fit the optimal binning object with arrays x and y. We set a minimum (lower bound) and maximum (upper bound) probability using arguments lb and ub. Note that these bounds are optional, but guarantee an event rate between \([0, 1]\) as any probability distribution. As shown later, settings these bounds will increase the problem size thus leading to higher solution times. An alternative approach to bypass these bounds is implemented when transforming to event rate or WoE
using functions fit and fit_transform, where values outside the interval \([0, 1]\) are clipped.
Note that the Weight-of-Evidence is not defined for event rate equal 0 or 1. Therefore, for small datasets, we recommend modifying the interval \([0, 1]\) with a small value, thus using \([\epsilon, 1 - \epsilon]\).
[6]:
optb.fit(x, y, lb=0.001, ub=0.999)
[6]:
OptimalPWBinning(estimator=LogisticRegression(), name='mean radius')In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
| name | 'mean radius' | |
| estimator | LogisticRegression() | |
| objective | 'l2' | |
| degree | 1 | |
| continuous | True | |
| continuous_deriv | True | |
| prebinning_method | 'cart' | |
| max_n_prebins | 20 | |
| min_prebin_size | 0.05 | |
| min_n_bins | None | |
| max_n_bins | None | |
| min_bin_size | None | |
| max_bin_size | None | |
| monotonic_trend | 'auto' | |
| n_subsamples | None | |
| max_pvalue | None | |
| max_pvalue_policy | 'consecutive' | |
| outlier_detector | None | |
| outlier_params | None | |
| user_splits | None | |
| user_splits_fixed | None | |
| special_codes | None | |
| split_digits | None | |
| solver | 'auto' | |
| h_epsilon | 1.35 | |
| quantile | 0.5 | |
| regularization | None | |
| reg_l1 | 1.0 | |
| reg_l2 | 1.0 | |
| random_state | None | |
| verbose | False |
LogisticRegression()
Parameters
You can check if an optimal solution has been found via the status attribute:
[7]:
optb.status
[7]:
'OPTIMAL'
You can also retrieve the optimal split points via the splits attribute:
[8]:
optb.splits
[8]:
array([11.42500019, 12.32999992, 13.09499979, 13.70499992, 15.04500008,
16.92500019])
The binning table
The optimal binning algorithms return a binning table; a binning table displays the binned data and several metrics for each bin. Class OptimalPWBinning returns an object PWBinningTable via the binning_table attribute.
[9]:
binning_table = optb.binning_table
[10]:
type(binning_table)
[10]:
optbinning.binning.piecewise.binning_statistics.PWBinningTable
The binning_table is instantiated, but not built. Therefore, the first step is to call the method build, which returns a pandas.DataFrame.
[11]:
binning_table.build()
[11]:
| Bin | Count | Count (%) | Non-event | Event | c0 | c1 | |
|---|---|---|---|---|---|---|---|
| 0 | (-inf, 11.43) | 118 | 0.207381 | 3 | 115 | 1.032597 | -0.004813 |
| 1 | [11.43, 12.33) | 79 | 0.138840 | 3 | 76 | 1.644465 | -0.058368 |
| 2 | [12.33, 13.09) | 68 | 0.119508 | 7 | 61 | 2.140581 | -0.098604 |
| 3 | [13.09, 13.70) | 49 | 0.086116 | 10 | 39 | 2.915209 | -0.157759 |
| 4 | [13.70, 15.05) | 83 | 0.145870 | 28 | 55 | 4.107004 | -0.244719 |
| 5 | [15.05, 16.93) | 54 | 0.094903 | 44 | 10 | 3.562027 | -0.208496 |
| 6 | [16.93, inf) | 118 | 0.207381 | 117 | 1 | 0.081993 | -0.002881 |
| 7 | Special | 0 | 0.000000 | 0 | 0 | 0.0 | 0.0 |
| 8 | Missing | 0 | 0.000000 | 0 | 0 | 0.0 | 0.0 |
| Totals | 569 | 1.000000 | 212 | 357 | - | - |
Let’s describe the columns of this binning table:
Bin: the intervals delimited by the optimal split points.
Count: the number of records for each bin.
Count (%): the percentage of records for each bin.
Non-event: the number of non-event records \((y = 0)\) for each bin.
Event: the number of event records \((y = 1)\) for each bin.
\(c_0\): the first coefficient of the event rate polynomial.
\(c_1\): the second coefficient of the event rate polynomial.
The event rate for bin \(i\) is defined as \(ER_i = c_0 + c_1 x_i\), where \(x_i \in \text{Bin}_{i}\). In general, \begin{equation} ER_i = \sum_{j=0}^d c_j x_i^j, \end{equation} where \(d\) is the degree of the event rate polynomial.
The last row shows the total number of records, non-event records, event records.
You can use the method plot to visualize the number of non-event and event records by bin, and WoE or event rate curve.
[12]:
binning_table.plot(metric="woe")
[13]:
binning_table.plot(metric="event_rate")
Event rate / WoE transformation
Now that we have checked the binned data, we can transform our original data into WoE or event rate values.
[14]:
x_transform_woe = optb.transform(x, metric="woe")
[15]:
x_transform_event_rate = optb.transform(x, metric="event_rate")
Advanced
Optimal binning Information
The OptimalPWBinning can print overview information about the options settings, problem statistics, and the solution of the computation. By default, print_level=1.
[16]:
optb = OptimalPWBinning(name=variable)
optb.fit(x, y)
[16]:
OptimalPWBinning(estimator=LogisticRegression(), name='mean radius')In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
| name | 'mean radius' | |
| estimator | LogisticRegression() | |
| objective | 'l2' | |
| degree | 1 | |
| continuous | True | |
| continuous_deriv | True | |
| prebinning_method | 'cart' | |
| max_n_prebins | 20 | |
| min_prebin_size | 0.05 | |
| min_n_bins | None | |
| max_n_bins | None | |
| min_bin_size | None | |
| max_bin_size | None | |
| monotonic_trend | 'auto' | |
| n_subsamples | None | |
| max_pvalue | None | |
| max_pvalue_policy | 'consecutive' | |
| outlier_detector | None | |
| outlier_params | None | |
| user_splits | None | |
| user_splits_fixed | None | |
| special_codes | None | |
| split_digits | None | |
| solver | 'auto' | |
| h_epsilon | 1.35 | |
| quantile | 0.5 | |
| regularization | None | |
| reg_l1 | 1.0 | |
| reg_l2 | 1.0 | |
| random_state | None | |
| verbose | False |
LogisticRegression()
Parameters
If print_level=0, a minimal output including the header, variable name, status, and total time are printed.
[17]:
optb.information(print_level=0)
optbinning (Version 1.1.0)
Copyright (c) 2019-2026 Guillermo Navas-Palencia, Apache License 2.0
Name : mean radius
Status : OPTIMAL
Time : 0.2496 sec
If print_level>=1, statistics on the pre-binning phase and the solver are printed. More detailed timing statistics are also included.
[18]:
optb.information(print_level=1)
optbinning (Version 1.1.0)
Copyright (c) 2019-2026 Guillermo Navas-Palencia, Apache License 2.0
Name : mean radius
Status : OPTIMAL
Pre-binning statistics
Number of bins 7
Solver statistics
Type auto
Number of variables 14
Number of constraints 20
Timing
Total time 0.25 sec
Pre-processing 0.00 sec ( 0.06%)
Estimator 0.04 sec ( 14.12%)
Pre-binning 0.17 sec ( 67.62%)
Solver 0.02 sec ( 9.81%)
Post-processing 0.02 sec ( 8.13%)
If print_level=2, the list of all options of the OptimalBinning are displayed.
[19]:
optb.information(print_level=2)
optbinning (Version 1.1.0)
Copyright (c) 2019-2026 Guillermo Navas-Palencia, Apache License 2.0
Begin options
name mean radius * U
estimator yes * U
objective l2 * d
degree 1 * d
continuous True * d
prebinning_method cart * d
max_n_prebins 20 * d
min_prebin_size 0.05 * d
min_n_bins no * d
max_n_bins no * d
min_bin_size no * d
max_bin_size no * d
monotonic_trend auto * d
n_subsamples no * d
max_pvalue no * d
max_pvalue_policy consecutive * d
outlier_detector no * d
outlier_params no * d
user_splits no * d
special_codes no * d
split_digits no * d
solver auto * d
h_epsilon 1.35 * d
quantile 0.5 * d
regularization no * d
reg_l1 1.0 * d
reg_l2 1.0 * d
random_state no * d
verbose False * d
End options
Name : mean radius
Status : OPTIMAL
Pre-binning statistics
Number of bins 7
Solver statistics
Type auto
Number of variables 14
Number of constraints 20
Timing
Total time 0.25 sec
Pre-processing 0.00 sec ( 0.06%)
Estimator 0.04 sec ( 14.12%)
Pre-binning 0.17 sec ( 67.62%)
Solver 0.02 sec ( 9.81%)
Post-processing 0.02 sec ( 8.13%)
Binning table statistical analysis
The analysis method performs a statistical analysis of the binning table, computing the statistics Gini index, Information Value (IV), Jensen-Shannon divergence, and the quality score. Additionally, several statistical significance tests between consecutive bins of the contingency table are performed: a frequentist test using the Chi-square test or the Fisher’s exact test, and a Bayesian A/B test using the beta distribution as a conjugate prior of the Bernoulli distribution. The piecewise
binning also includes two performance metrics: the average precision and the Brier score.
[20]:
binning_table.analysis(pvalue_test="chi2")
---------------------------------------------
OptimalBinning: Binary Binning Table Analysis
---------------------------------------------
General metrics
Gini index 0.87503303
IV (Jeffrey) 4.48034999
JS (Jensen-Shannon) 0.37576960
Hellinger 0.44284048
Triangular 1.20890940
KS 0.72390466
Avg precision 0.95577179
Brier score 0.08956359
HHI 0.15727342
HHI (normalized) 0.01681899
Cramer's V 0.80066760
Quality score 0.00000000
Significance tests
Bin A Bin B t-statistic p-value P[A > B] P[B > A]
0 1 0.289789 5.903557e-01 0.697563 3.024368e-01
1 2 2.285355 1.306002e-01 0.941469 5.853092e-02
2 3 2.317546 1.279217e-01 0.936661 6.333919e-02
3 4 2.539025 1.110634e-01 0.950869 4.913141e-02
4 5 29.418844 5.830790e-08 1.000000 1.898148e-11
5 6 18.624518 1.591604e-05 0.999997 3.106835e-06
[21]:
binning_table.analysis(pvalue_test="fisher")
---------------------------------------------
OptimalBinning: Binary Binning Table Analysis
---------------------------------------------
General metrics
Gini index 0.87503303
IV (Jeffrey) 4.48034999
JS (Jensen-Shannon) 0.37576960
Hellinger 0.44284048
Triangular 1.20890940
KS 0.72390466
Avg precision 0.95577179
Brier score 0.08956359
HHI 0.15727342
HHI (normalized) 0.01681899
Cramer's V 0.80066760
Quality score 0.00000000
Significance tests
Bin A Bin B odd ratio p-value P[A > B] P[B > A]
0 1 0.660870 6.855009e-01 0.697563 3.024368e-01
1 2 0.343985 1.879769e-01 0.941469 5.853092e-02
2 3 0.447541 1.829850e-01 0.936661 6.333919e-02
3 4 0.503663 1.153088e-01 0.950869 4.913141e-02
4 5 0.115702 3.464724e-08 1.000000 1.898148e-11
5 6 0.037607 4.144258e-05 0.999997 3.106835e-06
Event rate / WoE monotonicity
The monotonic_trend option permits forcing a monotonic trend to the event rate curve. The default setting “auto” should be the preferred option, however, some business constraints might require to impose different trends. The default setting “auto” chooses the monotonic trend most likely to maximize the information value from the options “ascending”, “descending”, “peak” and “valley” using a machine-learning-based classifier. For this example, we choose Gradient Boosting as an estimator.
[22]:
variable = "mean texture"
x = df[variable].values
y = data.target
[23]:
optb = OptimalPWBinning(name=variable, estimator=GradientBoostingClassifier())
optb.fit(x, y, lb=0.001, ub=0.999)
[23]:
OptimalPWBinning(estimator=GradientBoostingClassifier(), name='mean texture')In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
| name | 'mean texture' | |
| estimator | GradientBoostingClassifier() | |
| objective | 'l2' | |
| degree | 1 | |
| continuous | True | |
| continuous_deriv | True | |
| prebinning_method | 'cart' | |
| max_n_prebins | 20 | |
| min_prebin_size | 0.05 | |
| min_n_bins | None | |
| max_n_bins | None | |
| min_bin_size | None | |
| max_bin_size | None | |
| monotonic_trend | 'auto' | |
| n_subsamples | None | |
| max_pvalue | None | |
| max_pvalue_policy | 'consecutive' | |
| outlier_detector | None | |
| outlier_params | None | |
| user_splits | None | |
| user_splits_fixed | None | |
| special_codes | None | |
| split_digits | None | |
| solver | 'auto' | |
| h_epsilon | 1.35 | |
| quantile | 0.5 | |
| regularization | None | |
| reg_l1 | 1.0 | |
| reg_l2 | 1.0 | |
| random_state | None | |
| verbose | False |
GradientBoostingClassifier()
Parameters
[24]:
binning_table = optb.binning_table
binning_table.build()
[24]:
| Bin | Count | Count (%) | Non-event | Event | c0 | c1 | |
|---|---|---|---|---|---|---|---|
| 0 | (-inf, 15.05) | 92 | 0.161687 | 4 | 88 | 0.931559 | -0.0 |
| 1 | [15.05, 16.39) | 61 | 0.107206 | 8 | 53 | 2.075855 | -0.076058 |
| 2 | [16.39, 17.03) | 29 | 0.050967 | 6 | 23 | 2.149387 | -0.080543 |
| 3 | [17.03, 18.46) | 79 | 0.138840 | 17 | 62 | 1.113762 | -0.019749 |
| 4 | [18.46, 19.47) | 55 | 0.096661 | 20 | 35 | 4.139988 | -0.183684 |
| 5 | [19.47, 20.20) | 36 | 0.063269 | 18 | 18 | 4.417909 | -0.197958 |
| 6 | [20.20, 21.71) | 72 | 0.126538 | 43 | 29 | 0.933399 | -0.025415 |
| 7 | [21.71, 22.74) | 40 | 0.070299 | 27 | 13 | 2.611221 | -0.102698 |
| 8 | [22.74, 24.00) | 29 | 0.050967 | 24 | 5 | 0.275868 | -0.0 |
| 9 | [24.00, 26.98) | 43 | 0.075571 | 30 | 13 | -1.111301 | 0.057799 |
| 10 | [26.98, inf) | 33 | 0.057996 | 15 | 18 | 0.448108 | 0.0 |
| 11 | Special | 0 | 0.000000 | 0 | 0 | 0.0 | 0.0 |
| 12 | Missing | 0 | 0.000000 | 0 | 0 | 0.0 | 0.0 |
| Totals | 569 | 1.000000 | 212 | 357 | - | - |
[25]:
binning_table.plot(metric="event_rate")
For example, we can force the variable mean texture to be convex with respect to the probability of having breast cancer.
[26]:
optb = OptimalPWBinning(name=variable, estimator=GradientBoostingClassifier(),
monotonic_trend="convex")
optb.fit(x, y, lb=0.001, ub=0.999)
[26]:
OptimalPWBinning(estimator=GradientBoostingClassifier(),
monotonic_trend='convex', name='mean texture')In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
| name | 'mean texture' | |
| estimator | GradientBoostingClassifier() | |
| monotonic_trend | 'convex' | |
| objective | 'l2' | |
| degree | 1 | |
| continuous | True | |
| continuous_deriv | True | |
| prebinning_method | 'cart' | |
| max_n_prebins | 20 | |
| min_prebin_size | 0.05 | |
| min_n_bins | None | |
| max_n_bins | None | |
| min_bin_size | None | |
| max_bin_size | None | |
| n_subsamples | None | |
| max_pvalue | None | |
| max_pvalue_policy | 'consecutive' | |
| outlier_detector | None | |
| outlier_params | None | |
| user_splits | None | |
| user_splits_fixed | None | |
| special_codes | None | |
| split_digits | None | |
| solver | 'auto' | |
| h_epsilon | 1.35 | |
| quantile | 0.5 | |
| regularization | None | |
| reg_l1 | 1.0 | |
| reg_l2 | 1.0 | |
| random_state | None | |
| verbose | False |
GradientBoostingClassifier()
Parameters
[27]:
binning_table = optb.binning_table
binning_table.build()
binning_table.plot(metric="event_rate")
User-defined split points
In some situations, we have defined split points or bins required to satisfy a priori belief, knowledge or business constraint. The OptimalPWBinning permits to pass user-defined split points. The supplied information is used as a pre-binning, disallowing any pre-binning method set by the user. Furthermore, version 0.5.0 introduces user_splits_fixed parameter, to allow the user to fix some user-defined splits, so these must appear in the solution.
Example numerical variable:
[28]:
user_splits = [ 14, 15, 16, 17, 20, 21, 22, 27]
user_splits_fixed = [False, True, True, False, False, False, False, False]
[29]:
optb = OptimalPWBinning(name=variable, user_splits=user_splits,
user_splits_fixed=user_splits_fixed)
optb.fit(x, y)
[29]:
OptimalPWBinning(estimator=LogisticRegression(), name='mean texture',
user_splits=[14, 15, 16, 17, 20, 21, 22, 27],
user_splits_fixed=[False, True, True, False, False, False,
False, False])In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
| name | 'mean texture' | |
| estimator | LogisticRegression() | |
| user_splits | [14, 15, ...] | |
| user_splits_fixed | [False, True, ...] | |
| objective | 'l2' | |
| degree | 1 | |
| continuous | True | |
| continuous_deriv | True | |
| prebinning_method | 'cart' | |
| max_n_prebins | 20 | |
| min_prebin_size | 0.05 | |
| min_n_bins | None | |
| max_n_bins | None | |
| min_bin_size | None | |
| max_bin_size | None | |
| monotonic_trend | 'auto' | |
| n_subsamples | None | |
| max_pvalue | None | |
| max_pvalue_policy | 'consecutive' | |
| outlier_detector | None | |
| outlier_params | None | |
| special_codes | None | |
| split_digits | None | |
| solver | 'auto' | |
| h_epsilon | 1.35 | |
| quantile | 0.5 | |
| regularization | None | |
| reg_l1 | 1.0 | |
| reg_l2 | 1.0 | |
| random_state | None | |
| verbose | False |
LogisticRegression()
Parameters
[30]:
binning_table = optb.binning_table
binning_table.build()
[30]:
| Bin | Count | Count (%) | Non-event | Event | c0 | c1 | |
|---|---|---|---|---|---|---|---|
| 0 | (-inf, 14.00) | 54 | 0.094903 | 2 | 52 | 1.154009 | -0.020568 |
| 1 | [14.00, 15.00) | 37 | 0.065026 | 2 | 35 | 1.353911 | -0.034847 |
| 2 | [15.00, 16.00) | 43 | 0.075571 | 7 | 36 | 1.247073 | -0.027724 |
| 3 | [16.00, 20.00) | 210 | 0.369069 | 59 | 151 | 1.569012 | -0.047846 |
| 4 | [20.00, 21.00) | 45 | 0.079086 | 26 | 19 | 1.937899 | -0.06629 |
| 5 | [21.00, 22.00) | 49 | 0.086116 | 30 | 19 | 1.54374 | -0.047521 |
| 6 | [22.00, 27.00) | 99 | 0.173989 | 71 | 28 | 1.893766 | -0.063431 |
| 7 | [27.00, inf) | 32 | 0.056239 | 15 | 17 | 0.181134 | 0.0 |
| 8 | Special | 0 | 0.000000 | 0 | 0 | 0.0 | 0.0 |
| 9 | Missing | 0 | 0.000000 | 0 | 0 | 0.0 | 0.0 |
| Totals | 569 | 1.000000 | 212 | 357 | - | - |
[31]:
optb.information()
optbinning (Version 1.1.0)
Copyright (c) 2019-2026 Guillermo Navas-Palencia, Apache License 2.0
Name : mean texture
Status : OPTIMAL
Pre-binning statistics
Number of bins 8
Solver statistics
Type auto
Number of variables 16
Number of constraints 23
Timing
Total time 0.09 sec
Pre-processing 0.00 sec ( 0.10%)
Estimator 0.01 sec ( 10.72%)
Pre-binning 0.04 sec ( 46.53%)
Solver 0.02 sec ( 23.34%)
Post-processing 0.02 sec ( 18.30%)
Performance: choosing a solver
OptimalPWBinning uses the RoPWR library to solving the piecewise regression problem. See https://github.com/guillermo-navas-palencia/ropwr for details.
Robustness: choosing an objective function and regularization
OptimalPWBinning uses the RoPWR library to solving the piecewise regression problem. See https://github.com/guillermo-navas-palencia/ropwr for details.
Missing data and special codes
For this example, let’s load data from the FICO Explainable Machine Learning Challenge: https://community.fico.com/s/explainable-machine-learning-challenge
[32]:
df = pd.read_csv("data/FICO_challenge/heloc_dataset_v1.csv", sep=",")
The data dictionary of this challenge includes three special values/codes:
-9 No Bureau Record or No Investigation
-8 No Usable/Valid Trades or Inquiries
-7 Condition not Met (e.g. No Inquiries, No Delinquencies)
Version 0.16.0 added support to treat special codes separately.
[33]:
special_codes = {'special_9': -9, 'special_8': -8, 'special_7': -7}
[34]:
variable = "AverageMInFile"
x = df[variable].values.copy()
y = df.RiskPerformance.values
[35]:
df.RiskPerformance.unique()
[35]:
<ArrowStringArray>
['Bad', 'Good']
Length: 2, dtype: str
Target is a categorical dichotomic variable, which can be easily transform into numerical.
[36]:
mask = y == "Bad"
y = mask.astype(int)
[37]:
x[10:20] = -8
x[100:105] = -7
For the sake of completeness, we include a few missing values
[38]:
idx = np.random.randint(0, len(x), 500)
x = x.astype(float)
x[idx] = np.nan
[39]:
optb = OptimalPWBinning(name=variable, special_codes=special_codes, solver="osqp")
optb.fit(x, y, lb=0, ub=1)
[39]:
OptimalPWBinning(estimator=LogisticRegression(), name='AverageMInFile',
solver='osqp',
special_codes={'special_7': -7, 'special_8': -8,
'special_9': -9})In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
| name | 'AverageMInFile' | |
| estimator | LogisticRegression() | |
| special_codes | {'special_7': -7, 'special_8': -8, 'special_9': -9} | |
| solver | 'osqp' | |
| objective | 'l2' | |
| degree | 1 | |
| continuous | True | |
| continuous_deriv | True | |
| prebinning_method | 'cart' | |
| max_n_prebins | 20 | |
| min_prebin_size | 0.05 | |
| min_n_bins | None | |
| max_n_bins | None | |
| min_bin_size | None | |
| max_bin_size | None | |
| monotonic_trend | 'auto' | |
| n_subsamples | None | |
| max_pvalue | None | |
| max_pvalue_policy | 'consecutive' | |
| outlier_detector | None | |
| outlier_params | None | |
| user_splits | None | |
| user_splits_fixed | None | |
| split_digits | None | |
| h_epsilon | 1.35 | |
| quantile | 0.5 | |
| regularization | None | |
| reg_l1 | 1.0 | |
| reg_l2 | 1.0 | |
| random_state | None | |
| verbose | False |
LogisticRegression()
Parameters
[40]:
optb.information(print_level=1)
optbinning (Version 1.1.0)
Copyright (c) 2019-2026 Guillermo Navas-Palencia, Apache License 2.0
Name : AverageMInFile
Status : OPTIMAL
Pre-binning statistics
Number of bins 11
Solver statistics
Type osqp
Number of variables 22
Number of constraints 36
Timing
Total time 0.28 sec
Pre-processing 0.03 sec ( 10.55%)
Estimator 0.04 sec ( 12.63%)
Pre-binning 0.10 sec ( 35.28%)
Solver 0.07 sec ( 25.16%)
Post-processing 0.04 sec ( 15.65%)
[41]:
binning_table = optb.binning_table
binning_table.build()
[41]:
| Bin | Count | Count (%) | Non-event | Event | c0 | c1 | |
|---|---|---|---|---|---|---|---|
| 0 | (-inf, 30.50) | 557 | 0.053256 | 99 | 458 | 0.793007 | -0.003079 |
| 1 | [30.50, 48.50) | 1059 | 0.101253 | 292 | 767 | 0.806698 | -0.003528 |
| 2 | [48.50, 56.50) | 696 | 0.066546 | 253 | 443 | 0.81698 | -0.00374 |
| 3 | [56.50, 64.50) | 921 | 0.088058 | 387 | 534 | 0.821052 | -0.003812 |
| 4 | [64.50, 69.50) | 652 | 0.062339 | 303 | 349 | 0.825269 | -0.003877 |
| 5 | [69.50, 74.50) | 663 | 0.063390 | 320 | 343 | 0.82943 | -0.003937 |
| 6 | [74.50, 81.50) | 899 | 0.085955 | 470 | 429 | 0.824119 | -0.003866 |
| 7 | [81.50, 95.50) | 1514 | 0.144756 | 864 | 650 | 0.839333 | -0.004053 |
| 8 | [95.50, 101.50) | 477 | 0.045607 | 276 | 201 | 0.664386 | -0.002221 |
| 9 | [101.50, 116.50) | 871 | 0.083278 | 536 | 335 | 1.080937 | -0.006325 |
| 10 | [116.50, inf) | 1091 | 0.104312 | 708 | 383 | 0.539214 | -0.001675 |
| 11 | special_9 | 562 | 0.053734 | 255 | 307 | 0.546263 | 0.0 |
| 12 | special_8 | 9 | 0.000861 | 1 | 8 | 0.888889 | 0.0 |
| 13 | special_7 | 5 | 0.000478 | 2 | 3 | 0.6 | 0.0 |
| 14 | Missing | 483 | 0.046180 | 234 | 249 | 0.515528 | 0.0 |
| Totals | 10459 | 1.000000 | 5000 | 5459 | - | - |
Note the dashed bins 10 and 11, corresponding to the special codes bin and the missing bin, respectively.
[42]:
binning_table.plot(metric="event_rate")
Verbosity option
For debugging purposes, we can print information on each step of the computation by triggering the verbose option.
[43]:
optb = OptimalPWBinning(name=variable, solver="osqp", verbose=True)
optb.fit(x, y, lb=0, ub=1)
2026-11-22 19:30:56,494 | INFO : Optimal piecewise binning started.
2026-11-22 19:30:56,496 | INFO : Options: check parameters.
2026-11-22 19:30:56,498 | INFO : Pre-processing started.
2026-11-22 19:30:56,499 | INFO : Pre-processing: number of samples: 10459
2026-11-22 19:30:56,500 | INFO : Pre-processing: number of clean samples: 9976
2026-11-22 19:30:56,501 | INFO : Pre-processing: number of missing samples: 483
2026-11-22 19:30:56,501 | INFO : Pre-processing: number of special samples: 0
2026-11-22 19:30:56,502 | INFO : Pre-processing terminated. Time: 0.0003s
2026-11-22 19:30:56,503 | INFO : Pre-binning: set logistic regression as an estimator.
2026-11-22 19:30:56,504 | INFO : Pre-binning: estimator fitting started.
2026-11-22 19:30:56,529 | INFO : Pre-binning: estimator terminated. Time 0.0262s.
2026-11-22 19:30:56,532 | INFO : Pre-binning: optimal binning started.
2026-11-22 19:30:56,642 | INFO : Pre-binning: number of splits: 9.
2026-11-22 19:30:56,643 | INFO : Pre-binning: no need for subsamples.
2026-11-22 19:30:56,644 | INFO : Pre-binning: optimal binning terminated. Time 0.1104s.
2026-11-22 19:30:56,645 | INFO : Optimizer started.
2026-11-22 19:30:56,648 | INFO : Optimizer: descending monotonic trend.
(CVXPY) Nov 22 07:30:56 PM: Your problem has 20 variables, 33 constraints, and 0 parameters.
(CVXPY) Nov 22 07:30:56 PM: It is compliant with the following grammars: DCP, DQCP
(CVXPY) Nov 22 07:30:56 PM: DCP verification time: 0.0002 seconds.
(CVXPY) Nov 22 07:30:56 PM: Expression tree has 4 nodes.
(CVXPY) Nov 22 07:30:56 PM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) Nov 22 07:30:56 PM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
(CVXPY) Nov 22 07:30:56 PM: Your problem is compiled with the CPP canonicalization backend.
(CVXPY) Nov 22 07:30:56 PM: Compiling problem (target solver=OSQP).
(CVXPY) Nov 22 07:30:56 PM: Reduction chain: Dcp2Cone -> CvxAttr2Constr -> EliminateZeroSized -> ConeMatrixStuffing -> OSQP
(CVXPY) Nov 22 07:30:56 PM: Applying reduction Dcp2Cone
(CVXPY) Nov 22 07:30:56 PM: Applying reduction CvxAttr2Constr
(CVXPY) Nov 22 07:30:56 PM: Applying reduction EliminateZeroSized
(CVXPY) Nov 22 07:30:56 PM: Applying reduction ConeMatrixStuffing
(CVXPY) Nov 22 07:30:56 PM: Applying reduction OSQP
===============================================================================
CVXPY
v1.9.2
===============================================================================
-------------------------------------------------------------------------------
Compilation
-------------------------------------------------------------------------------
(CVXPY) Nov 22 07:30:56 PM: Finished problem compilation (took 3.554e-02 seconds).
(CVXPY) Nov 22 07:30:56 PM: Invoking solver OSQP to obtain a solution.
-------------------------------------------------------------------------------
Numerical solver
-------------------------------------------------------------------------------
(CVXPY) Nov 22 07:30:56 PM: Problem status: optimal
(CVXPY) Nov 22 07:30:56 PM: Optimal value: -2.831e+03
(CVXPY) Nov 22 07:30:56 PM: Compilation took 3.554e-02 seconds
(CVXPY) Nov 22 07:30:56 PM: Solver (including time spent in interface) took 7.116e-03 seconds
-----------------------------------------------------------------
OSQP v1.0.0 - Operator Splitting QP Solver
(c) The OSQP Developer Team
-----------------------------------------------------------------
problem: variables n = 20, constraints m = 33
nnz(P) + nnz(A) = 84
settings: algebra = Built-in,
OSQPInt = 4 bytes, OSQPFloat = 8 bytes,
linear system solver = QDLDL v0.1.8,
eps_abs = 1.0e-05, eps_rel = 1.0e-05,
eps_prim_inf = 1.0e-04, eps_dual_inf = 1.0e-04,
rho = 1.00e-01 (adaptive: 50 iterations),
sigma = 1.00e-06, alpha = 1.60, max_iter = 10000
check_termination: on (interval 25, duality gap: on),
time_limit: 1.00e+10 sec,
scaling: on (10 iterations), scaled_termination: off
warm starting: on, polishing: on,
iter objective prim res dual res gap rel kkt rho time
1 -1.9783e+03 7.67e-03 1.10e+05 4.34e+03 1.10e+05 1.00e-01 6.02e-04s
50 -2.8306e+03 9.86e-06 1.53e+00 -1.24e-03 1.53e+00 1.00e-01 7.29e-04s
plsh -2.8306e+03 1.30e-16 3.46e-11 0.00e+00 3.46e-11 -------- 8.70e-04s
status: solved
solution polishing: successful
number of iterations: 50
optimal objective: -2830.6347
dual objective: -2830.6347
duality gap: 0.0000e+00
primal-dual integral: 4.3437e+03
run time: 8.70e-04s
optimal rho estimate: 4.77e-02
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
2026-11-22 19:30:56,719 | INFO : Optimizer terminated. Time: 0.0696s
2026-11-22 19:30:56,720 | INFO : Post-processing started.
2026-11-22 19:30:56,721 | INFO : Post-processing: compute binning information.
2026-11-22 19:30:56,731 | INFO : Post-processing: compute performance metrics.
2026-11-22 19:30:56,761 | INFO : Post-processing terminated. Time: 0.0402s
2026-11-22 19:30:56,763 | INFO : Optimal piecewise binning terminated. Status: OPTIMAL. Time: 0.2688s
[43]:
OptimalPWBinning(estimator=LogisticRegression(), name='AverageMInFile',
solver='osqp', verbose=True)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
| name | 'AverageMInFile' | |
| estimator | LogisticRegression() | |
| solver | 'osqp' | |
| verbose | True | |
| objective | 'l2' | |
| degree | 1 | |
| continuous | True | |
| continuous_deriv | True | |
| prebinning_method | 'cart' | |
| max_n_prebins | 20 | |
| min_prebin_size | 0.05 | |
| min_n_bins | None | |
| max_n_bins | None | |
| min_bin_size | None | |
| max_bin_size | None | |
| monotonic_trend | 'auto' | |
| n_subsamples | None | |
| max_pvalue | None | |
| max_pvalue_policy | 'consecutive' | |
| outlier_detector | None | |
| outlier_params | None | |
| user_splits | None | |
| user_splits_fixed | None | |
| special_codes | None | |
| split_digits | None | |
| h_epsilon | 1.35 | |
| quantile | 0.5 | |
| regularization | None | |
| reg_l1 | 1.0 | |
| reg_l2 | 1.0 | |
| random_state | None |
LogisticRegression()