# 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``.


```python
import numpy as np
import pandas as pd

from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import GradientBoostingClassifier
```


```python
data = load_breast_cancer()
df = pd.DataFrame(data.data, columns=data.feature_names)
```

We choose a variable to discretize and the binary target.


```python
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.


```python
from optbinning import OptimalPWBinning
```


```python
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]$.


```python
optb.fit(x, y, lb=0.001, ub=0.999)
```




    OptimalPWBinning(estimator=LogisticRegression(), name='mean radius')



You can check if an optimal solution has been found via the ``status`` attribute:


```python
optb.status
```




    'OPTIMAL'



You can also retrieve the optimal split points via the ``splits`` attribute:


```python
optb.splits
```




    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.


```python
binning_table = optb.binning_table
```


```python
type(binning_table)
```




    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``.


```python
binning_table.build()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Bin</th>
      <th>Count</th>
      <th>Count (%)</th>
      <th>Non-event</th>
      <th>Event</th>
      <th>c0</th>
      <th>c1</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>(-inf, 11.43)</td>
      <td>118</td>
      <td>0.207381</td>
      <td>3</td>
      <td>115</td>
      <td>1.032648</td>
      <td>-0.004820</td>
    </tr>
    <tr>
      <th>1</th>
      <td>[11.43, 12.33)</td>
      <td>79</td>
      <td>0.138840</td>
      <td>3</td>
      <td>76</td>
      <td>1.644837</td>
      <td>-0.058403</td>
    </tr>
    <tr>
      <th>2</th>
      <td>[12.33, 13.09)</td>
      <td>68</td>
      <td>0.119508</td>
      <td>7</td>
      <td>61</td>
      <td>2.140569</td>
      <td>-0.098609</td>
    </tr>
    <tr>
      <th>3</th>
      <td>[13.09, 13.70)</td>
      <td>49</td>
      <td>0.086116</td>
      <td>10</td>
      <td>39</td>
      <td>2.914961</td>
      <td>-0.157745</td>
    </tr>
    <tr>
      <th>4</th>
      <td>[13.70, 15.05)</td>
      <td>83</td>
      <td>0.145870</td>
      <td>28</td>
      <td>55</td>
      <td>4.105273</td>
      <td>-0.244597</td>
    </tr>
    <tr>
      <th>5</th>
      <td>[15.05, 16.93)</td>
      <td>54</td>
      <td>0.094903</td>
      <td>44</td>
      <td>10</td>
      <td>3.562494</td>
      <td>-0.208520</td>
    </tr>
    <tr>
      <th>6</th>
      <td>[16.93, inf)</td>
      <td>118</td>
      <td>0.207381</td>
      <td>117</td>
      <td>1</td>
      <td>0.082142</td>
      <td>-0.002887</td>
    </tr>
    <tr>
      <th>7</th>
      <td>Special</td>
      <td>0</td>
      <td>0.000000</td>
      <td>0</td>
      <td>0</td>
      <td>0.000000</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>8</th>
      <td>Missing</td>
      <td>0</td>
      <td>0.000000</td>
      <td>0</td>
      <td>0</td>
      <td>0.000000</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>Totals</th>
      <td></td>
      <td>569</td>
      <td>1.000000</td>
      <td>212</td>
      <td>357</td>
      <td>-</td>
      <td>-</td>
    </tr>
  </tbody>
</table>
</div>



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.


```python
binning_table.plot(metric="woe")
```


```python
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.


```python
x_transform_woe = optb.transform(x, metric="woe")
```


```python
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``.


```python
optb = OptimalPWBinning(name=variable)
optb.fit(x, y)
```




    OptimalPWBinning(estimator=LogisticRegression(), name='mean radius')



If ``print_level=0``, a minimal output including the header, variable name, status, and total time are printed.


```python
optb.information(print_level=0)
```

    optbinning (Version 0.13.1)
    Copyright (c) 2019-2022 Guillermo Navas-Palencia, Apache License 2.0
    
      Name    : mean radius                     
      Status  : OPTIMAL                         
    
      Time    : 0.1114  sec
    


If ``print_level>=1``, statistics on the pre-binning phase and the solver are printed. More detailed timing statistics are also included.


```python
optb.information(print_level=1)
```

    optbinning (Version 0.13.1)
    Copyright (c) 2019-2022 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.11 sec
        Pre-processing                      0.00 sec   (  0.73%)
        Estimator                           0.01 sec   ( 10.63%)
        Pre-binning                         0.05 sec   ( 40.58%)
        Solver                              0.04 sec   ( 35.02%)
        Post-processing                     0.01 sec   ( 12.37%)
    


If ``print_level=2``, the list of all options of the ``OptimalBinning`` are displayed.


```python
optb.information(print_level=2)
```

    optbinning (Version 0.13.1)
    Copyright (c) 2019-2022 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.11 sec
        Pre-processing                      0.00 sec   (  0.73%)
        Estimator                           0.01 sec   ( 10.63%)
        Pre-binning                         0.05 sec   ( 40.58%)
        Solver                              0.04 sec   ( 35.02%)
        Post-processing                     0.01 sec   ( 12.37%)
    


#### 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.


```python
binning_table.analysis(pvalue_test="chi2")
```

    ---------------------------------------------
    OptimalBinning: Binary Binning Table Analysis
    ---------------------------------------------
    
      General metrics
    
        Gini index               0.87503303
        IV (Jeffrey)             4.47808263
        JS (Jensen-Shannon)      0.37566002
        Hellinger                0.44267970
        Triangular               1.20862637
        KS                       0.72390466
        Avg precision            0.95577179
        Brier score              0.08956351
        HHI                      0.15727342
        HHI (normalized)         0.05193260
        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.898159e-11
            5      6    18.624518 1.591604e-05  0.999997 3.106835e-06
    



```python
binning_table.analysis(pvalue_test="fisher")
```

    ---------------------------------------------
    OptimalBinning: Binary Binning Table Analysis
    ---------------------------------------------
    
      General metrics
    
        Gini index               0.87503303
        IV (Jeffrey)             4.47808263
        JS (Jensen-Shannon)      0.37566002
        Hellinger                0.44267970
        Triangular               1.20862637
        KS                       0.72390466
        Avg precision            0.95577179
        Brier score              0.08956351
        HHI                      0.15727342
        HHI (normalized)         0.05193260
        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.


```python
variable = "mean texture"
x = df[variable].values
y = data.target
```


```python
optb = OptimalPWBinning(name=variable, estimator=GradientBoostingClassifier())
optb.fit(x, y, lb=0.001, ub=0.999)
```




    OptimalPWBinning(estimator=GradientBoostingClassifier(), name='mean texture')




```python
binning_table = optb.binning_table
binning_table.build()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Bin</th>
      <th>Count</th>
      <th>Count (%)</th>
      <th>Non-event</th>
      <th>Event</th>
      <th>c0</th>
      <th>c1</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>(-inf, 15.05)</td>
      <td>92</td>
      <td>0.161687</td>
      <td>4</td>
      <td>88</td>
      <td>0.931559</td>
      <td>-0.000000</td>
    </tr>
    <tr>
      <th>1</th>
      <td>[15.05, 16.39)</td>
      <td>61</td>
      <td>0.107206</td>
      <td>8</td>
      <td>53</td>
      <td>2.075863</td>
      <td>-0.076059</td>
    </tr>
    <tr>
      <th>2</th>
      <td>[16.39, 17.03)</td>
      <td>29</td>
      <td>0.050967</td>
      <td>6</td>
      <td>23</td>
      <td>2.149342</td>
      <td>-0.080541</td>
    </tr>
    <tr>
      <th>3</th>
      <td>[17.03, 18.46)</td>
      <td>79</td>
      <td>0.138840</td>
      <td>17</td>
      <td>62</td>
      <td>1.113793</td>
      <td>-0.019751</td>
    </tr>
    <tr>
      <th>4</th>
      <td>[18.46, 19.47)</td>
      <td>55</td>
      <td>0.096661</td>
      <td>20</td>
      <td>35</td>
      <td>4.139964</td>
      <td>-0.183682</td>
    </tr>
    <tr>
      <th>5</th>
      <td>[19.47, 20.20)</td>
      <td>36</td>
      <td>0.063269</td>
      <td>18</td>
      <td>18</td>
      <td>4.417902</td>
      <td>-0.197957</td>
    </tr>
    <tr>
      <th>6</th>
      <td>[20.20, 21.71)</td>
      <td>72</td>
      <td>0.126538</td>
      <td>43</td>
      <td>29</td>
      <td>0.933414</td>
      <td>-0.025415</td>
    </tr>
    <tr>
      <th>7</th>
      <td>[21.71, 22.74)</td>
      <td>40</td>
      <td>0.070299</td>
      <td>27</td>
      <td>13</td>
      <td>2.611206</td>
      <td>-0.102697</td>
    </tr>
    <tr>
      <th>8</th>
      <td>[22.74, 24.00)</td>
      <td>29</td>
      <td>0.050967</td>
      <td>24</td>
      <td>5</td>
      <td>0.275868</td>
      <td>-0.000000</td>
    </tr>
    <tr>
      <th>9</th>
      <td>[24.00, 26.98)</td>
      <td>43</td>
      <td>0.075571</td>
      <td>30</td>
      <td>13</td>
      <td>-1.111300</td>
      <td>0.057799</td>
    </tr>
    <tr>
      <th>10</th>
      <td>[26.98, inf)</td>
      <td>33</td>
      <td>0.057996</td>
      <td>15</td>
      <td>18</td>
      <td>0.448108</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>11</th>
      <td>Special</td>
      <td>0</td>
      <td>0.000000</td>
      <td>0</td>
      <td>0</td>
      <td>0.000000</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>12</th>
      <td>Missing</td>
      <td>0</td>
      <td>0.000000</td>
      <td>0</td>
      <td>0</td>
      <td>0.000000</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>Totals</th>
      <td></td>
      <td>569</td>
      <td>1.000000</td>
      <td>212</td>
      <td>357</td>
      <td>-</td>
      <td>-</td>
    </tr>
  </tbody>
</table>
</div>




```python
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.


```python
optb = OptimalPWBinning(name=variable, estimator=GradientBoostingClassifier(),
                        monotonic_trend="convex")
optb.fit(x, y, lb=0.001, ub=0.999)
```




    OptimalPWBinning(estimator=GradientBoostingClassifier(),
                     monotonic_trend='convex', name='mean texture')




```python
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:


```python
user_splits =       [  14,    15,    16,    17,    20,    21,    22,    27]
user_splits_fixed = [False, True,  True, False, False, False, False, False]
```


```python
optb = OptimalPWBinning(name=variable, user_splits=user_splits,
                        user_splits_fixed=user_splits_fixed)

optb.fit(x, y)
```




    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])




```python
binning_table = optb.binning_table
binning_table.build()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Bin</th>
      <th>Count</th>
      <th>Count (%)</th>
      <th>Non-event</th>
      <th>Event</th>
      <th>c0</th>
      <th>c1</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>(-inf, 14.00)</td>
      <td>54</td>
      <td>0.094903</td>
      <td>2</td>
      <td>52</td>
      <td>1.153974</td>
      <td>-0.020565</td>
    </tr>
    <tr>
      <th>1</th>
      <td>[14.00, 15.00)</td>
      <td>37</td>
      <td>0.065026</td>
      <td>2</td>
      <td>35</td>
      <td>1.354515</td>
      <td>-0.034890</td>
    </tr>
    <tr>
      <th>2</th>
      <td>[15.00, 16.00)</td>
      <td>43</td>
      <td>0.075571</td>
      <td>7</td>
      <td>36</td>
      <td>1.244455</td>
      <td>-0.027552</td>
    </tr>
    <tr>
      <th>3</th>
      <td>[16.00, 20.00)</td>
      <td>210</td>
      <td>0.369069</td>
      <td>59</td>
      <td>151</td>
      <td>1.570900</td>
      <td>-0.047955</td>
    </tr>
    <tr>
      <th>4</th>
      <td>[20.00, 21.00)</td>
      <td>45</td>
      <td>0.079086</td>
      <td>26</td>
      <td>19</td>
      <td>1.874636</td>
      <td>-0.063142</td>
    </tr>
    <tr>
      <th>5</th>
      <td>[21.00, 22.00)</td>
      <td>49</td>
      <td>0.086116</td>
      <td>30</td>
      <td>19</td>
      <td>1.788369</td>
      <td>-0.059034</td>
    </tr>
    <tr>
      <th>6</th>
      <td>[22.00, 27.00)</td>
      <td>99</td>
      <td>0.173989</td>
      <td>71</td>
      <td>28</td>
      <td>1.708444</td>
      <td>-0.055401</td>
    </tr>
    <tr>
      <th>7</th>
      <td>[27.00, inf)</td>
      <td>32</td>
      <td>0.056239</td>
      <td>15</td>
      <td>17</td>
      <td>0.789092</td>
      <td>-0.021351</td>
    </tr>
    <tr>
      <th>8</th>
      <td>Special</td>
      <td>0</td>
      <td>0.000000</td>
      <td>0</td>
      <td>0</td>
      <td>0.000000</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>9</th>
      <td>Missing</td>
      <td>0</td>
      <td>0.000000</td>
      <td>0</td>
      <td>0</td>
      <td>0.000000</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>Totals</th>
      <td></td>
      <td>569</td>
      <td>1.000000</td>
      <td>212</td>
      <td>357</td>
      <td>-</td>
      <td>-</td>
    </tr>
  </tbody>
</table>
</div>




```python
optb.information()
```

    optbinning (Version 0.13.1)
    Copyright (c) 2019-2022 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.15 sec
        Pre-processing                      0.00 sec   (  0.47%)
        Estimator                           0.02 sec   ( 11.16%)
        Pre-binning                         0.10 sec   ( 67.15%)
        Solver                              0.02 sec   ( 15.12%)
        Post-processing                     0.01 sec   (  5.40%)
    


#### 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


```python
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) 


```python
special_codes = [-9, -8, -7]
```


```python
variable = "AverageMInFile"
x = df[variable].values
y = df.RiskPerformance.values
```


```python
df.RiskPerformance.unique()
```




    array(['Bad', 'Good'], dtype=object)



Target is a categorical dichotomic variable, which can be easily transform into numerical.


```python
mask = y == "Bad"
y[mask] = 1
y[~mask] = 0
y = y.astype(int)
```

For the sake of completeness, we include a few missing values


```python
idx = np.random.randint(0, len(x), 500)
x = x.astype(float)
x[idx] = np.nan
```


```python
optb = OptimalPWBinning(name=variable, special_codes=special_codes, solver="osqp")

optb.fit(x, y, lb=0, ub=1)
```




    OptimalPWBinning(estimator=LogisticRegression(), name='AverageMInFile',
                     solver='osqp', special_codes=[-9, -8, -7])




```python
optb.information(print_level=1)
```

    optbinning (Version 0.13.1)
    Copyright (c) 2019-2022 Guillermo Navas-Palencia, Apache License 2.0
    
      Name    : AverageMInFile                  
      Status  : OPTIMAL                         
    
      Pre-binning statistics
        Number of bins                        13
    
      Solver statistics
        Type                                osqp
        Number of variables                   26
        Number of constraints              18852
    
      Timing
        Total time                          0.46 sec
        Pre-processing                      0.00 sec   (  0.50%)
        Estimator                           0.04 sec   (  8.24%)
        Pre-binning                         0.13 sec   ( 28.37%)
        Solver                              0.27 sec   ( 58.64%)
        Post-processing                     0.02 sec   (  3.86%)
    



```python
binning_table = optb.binning_table
binning_table.build()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Bin</th>
      <th>Count</th>
      <th>Count (%)</th>
      <th>Non-event</th>
      <th>Event</th>
      <th>c0</th>
      <th>c1</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>(-inf, 30.50)</td>
      <td>558</td>
      <td>0.053351</td>
      <td>98</td>
      <td>460</td>
      <td>0.791741</td>
      <td>-0.003083</td>
    </tr>
    <tr>
      <th>1</th>
      <td>[30.50, 41.50)</td>
      <td>551</td>
      <td>0.052682</td>
      <td>148</td>
      <td>403</td>
      <td>0.803858</td>
      <td>-0.003480</td>
    </tr>
    <tr>
      <th>2</th>
      <td>[41.50, 48.50)</td>
      <td>502</td>
      <td>0.047997</td>
      <td>141</td>
      <td>361</td>
      <td>0.807896</td>
      <td>-0.003577</td>
    </tr>
    <tr>
      <th>3</th>
      <td>[48.50, 54.50)</td>
      <td>526</td>
      <td>0.050292</td>
      <td>186</td>
      <td>340</td>
      <td>0.813452</td>
      <td>-0.003692</td>
    </tr>
    <tr>
      <th>4</th>
      <td>[54.50, 59.50)</td>
      <td>486</td>
      <td>0.046467</td>
      <td>197</td>
      <td>289</td>
      <td>0.817065</td>
      <td>-0.003758</td>
    </tr>
    <tr>
      <th>5</th>
      <td>[59.50, 64.50)</td>
      <td>595</td>
      <td>0.056889</td>
      <td>257</td>
      <td>338</td>
      <td>0.820498</td>
      <td>-0.003816</td>
    </tr>
    <tr>
      <th>6</th>
      <td>[64.50, 70.50)</td>
      <td>782</td>
      <td>0.074768</td>
      <td>359</td>
      <td>423</td>
      <td>0.822974</td>
      <td>-0.003854</td>
    </tr>
    <tr>
      <th>7</th>
      <td>[70.50, 75.50)</td>
      <td>676</td>
      <td>0.064633</td>
      <td>336</td>
      <td>340</td>
      <td>0.828829</td>
      <td>-0.003937</td>
    </tr>
    <tr>
      <th>8</th>
      <td>[75.50, 81.50)</td>
      <td>766</td>
      <td>0.073238</td>
      <td>405</td>
      <td>361</td>
      <td>0.818921</td>
      <td>-0.003806</td>
    </tr>
    <tr>
      <th>9</th>
      <td>[81.50, 95.50)</td>
      <td>1533</td>
      <td>0.146572</td>
      <td>872</td>
      <td>661</td>
      <td>0.840311</td>
      <td>-0.004068</td>
    </tr>
    <tr>
      <th>10</th>
      <td>[95.50, 101.50)</td>
      <td>471</td>
      <td>0.045033</td>
      <td>270</td>
      <td>201</td>
      <td>0.614140</td>
      <td>-0.001700</td>
    </tr>
    <tr>
      <th>11</th>
      <td>[101.50, 116.50)</td>
      <td>869</td>
      <td>0.083086</td>
      <td>539</td>
      <td>330</td>
      <td>1.157173</td>
      <td>-0.007050</td>
    </tr>
    <tr>
      <th>12</th>
      <td>[116.50, inf)</td>
      <td>1092</td>
      <td>0.104408</td>
      <td>709</td>
      <td>383</td>
      <td>0.482622</td>
      <td>-0.001260</td>
    </tr>
    <tr>
      <th>13</th>
      <td>Special</td>
      <td>562</td>
      <td>0.053734</td>
      <td>251</td>
      <td>311</td>
      <td>0.553381</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>14</th>
      <td>Missing</td>
      <td>490</td>
      <td>0.046850</td>
      <td>232</td>
      <td>258</td>
      <td>0.526531</td>
      <td>0.000000</td>
    </tr>
    <tr>
      <th>Totals</th>
      <td></td>
      <td>10459</td>
      <td>1.000000</td>
      <td>5000</td>
      <td>5459</td>
      <td>-</td>
      <td>-</td>
    </tr>
  </tbody>
</table>
</div>



Note the dashed bins 10 and 11, corresponding to the special codes bin and the missing bin, respectively.


```python
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.


```python
optb = OptimalPWBinning(name=variable, solver="osqp", verbose=True)
optb.fit(x, y, lb=0, ub=1)
```

    2022-02-18 11:13:05,394 | INFO : Optimal piecewise binning started.
    2022-02-18 11:13:05,396 | INFO : Options: check parameters.
    2022-02-18 11:13:05,397 | INFO : Pre-processing started.
    2022-02-18 11:13:05,398 | INFO : Pre-processing: number of samples: 10459
    2022-02-18 11:13:05,402 | INFO : Pre-processing: number of clean samples: 9969
    2022-02-18 11:13:05,403 | INFO : Pre-processing: number of missing samples: 490
    2022-02-18 11:13:05,404 | INFO : Pre-processing: number of special samples: 0
    2022-02-18 11:13:05,405 | INFO : Pre-processing terminated. Time: 0.0009s
    2022-02-18 11:13:05,406 | INFO : Pre-binning: set logistic regression as an estimator.
    2022-02-18 11:13:05,408 | INFO : Pre-binning: estimator fitting started.
    2022-02-18 11:13:05,452 | INFO : Pre-binning: estimator terminated. Time 0.0466s.
    2022-02-18 11:13:05,454 | INFO : Pre-binning: optimal binning started.
    2022-02-18 11:13:05,641 | INFO : Pre-binning: number of splits: 9.
    2022-02-18 11:13:05,643 | INFO : Pre-binning: no need for subsamples.
    2022-02-18 11:13:05,644 | INFO : Pre-binning: optimal binning terminated. Time 0.1893s.
    2022-02-18 11:13:05,645 | INFO : Optimizer started.
    2022-02-18 11:13:05,649 | INFO : Optimizer: descending monotonic trend.
    ===============================================================================
                                         CVXPY                                     
                                        v1.1.12                                    
    ===============================================================================
    (CVXPY) Feb 18 11:13:05 AM: Your problem has 20 variables, 4 constraints, and 0 parameters.
    (CVXPY) Feb 18 11:13:05 AM: It is compliant with the following grammars: DCP, DQCP
    (CVXPY) Feb 18 11:13:05 AM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
    (CVXPY) Feb 18 11:13:05 AM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
    -------------------------------------------------------------------------------
                                      Compilation                                  
    -------------------------------------------------------------------------------
    (CVXPY) Feb 18 11:13:05 AM: Compiling problem (target solver=OSQP).
    (CVXPY) Feb 18 11:13:05 AM: Reduction chain: CvxAttr2Constr -> Qp2SymbolicQp -> QpMatrixStuffing -> OSQP
    (CVXPY) Feb 18 11:13:05 AM: Applying reduction CvxAttr2Constr
    (CVXPY) Feb 18 11:13:05 AM: Applying reduction Qp2SymbolicQp
    (CVXPY) Feb 18 11:13:05 AM: Applying reduction QpMatrixStuffing
    (CVXPY) Feb 18 11:13:05 AM: Applying reduction OSQP
    (CVXPY) Feb 18 11:13:05 AM: Finished problem compilation (took 9.970e-02 seconds).
    -------------------------------------------------------------------------------
                                    Numerical solver                               
    -------------------------------------------------------------------------------
    (CVXPY) Feb 18 11:13:05 AM: Invoking solver OSQP  to obtain a solution.
    -----------------------------------------------------------------
               OSQP v0.6.0  -  Operator Splitting QP Solver
                  (c) Bartolomeo Stellato,  Goran Banjac
            University of Oxford  -  Stanford University 2019
    -----------------------------------------------------------------
    problem:  variables n = 20, constraints m = 19967
              nnz(P) + nnz(A) = 39952
    settings: linear system solver = qdldl,
              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),
              sigma = 1.00e-06, alpha = 1.60, max_iter = 10000
              check_termination: on (interval 25),
              scaling: on, scaled_termination: off
              warm start: on, polish: on, time_limit: off
    
    iter   objective    pri res    dua res    rho        time
       1  -4.3045e+01   1.33e-03   1.77e+05   1.00e-01   2.15e-02s
     200  -2.4215e+03   7.80e-03   1.68e+04   1.22e-02   1.16e-01s
     400  -2.7207e+03   1.03e-02   6.09e+03   1.22e-02   2.04e-01s
     600  -2.8224e+03   7.81e-06   2.84e-03   4.69e-05   3.01e-01s
    plsh  -2.8224e+03   1.41e-16   7.67e-11   --------   3.02e-01s
    
    status:               solved
    solution polish:      successful
    number of iterations: 600
    optimal objective:    -2822.3989
    run time:             3.02e-01s
    optimal rho estimate: 2.81e-04
    
    -------------------------------------------------------------------------------
                                        Summary                                    
    -------------------------------------------------------------------------------
    (CVXPY) Feb 18 11:13:06 AM: Problem status: optimal
    (CVXPY) Feb 18 11:13:06 AM: Optimal value: -2.822e+03
    (CVXPY) Feb 18 11:13:06 AM: Compilation took 9.970e-02 seconds
    (CVXPY) Feb 18 11:13:06 AM: Solver (including time spent in interface) took 3.100e-01 seconds
    2022-02-18 11:13:06,113 | INFO : Optimizer terminated. Time: 0.4628s
    2022-02-18 11:13:06,115 | INFO : Post-processing started.
    2022-02-18 11:13:06,116 | INFO : Post-processing: compute binning information.
    2022-02-18 11:13:06,123 | INFO : Post-processing: compute performance metrics.
    2022-02-18 11:13:06,153 | INFO : Post-processing terminated. Time: 0.0362s
    2022-02-18 11:13:06,155 | INFO : Optimal piecewise binning terminated. Status: OPTIMAL. Time: 0.7606s





    OptimalPWBinning(estimator=LogisticRegression(), name='AverageMInFile',
                     solver='osqp', verbose=True)



58a1a1d42bd1d7b5235ba7efc7ff4cffa25bd0e4
ipynb
Jupyter Notebook
doc/source/tutorials/tutorial_piecewise_binary.ipynb
jensgk/optbinning
5ccd892fa4ee0a745ab539cee10a2069b35de6da
Apache-2.0
doc/source/tutorials/tutorial_piecewise_binary.ipynb
jensgk/optbinning
5ccd892fa4ee0a745ab539cee10a2069b35de6da
Apache-2.0
doc/source/tutorials/tutorial_piecewise_binary.ipynb
jensgk/optbinning
5ccd892fa4ee0a745ab539cee10a2069b35de6da
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES