Skip to content

Bug: binning_table.plot() showing incorrect WoE value for "Missing" #376

@Danielwohlr

Description

@Danielwohlr

Hi,

first, thank you for maintaining this package -- I appreciate your work put into this :).

I found a bug in the OptimalBinning.binning_table.plot() method. If specifying add_special = False and show_bin_labels=True, the position of the plotted WoE value for category "Missing" is incorrect. See a reproducible example below:

from optbinning import OptimalBinning
from scipy.special import expit
import numpy as np
import pandas as pd


def main():

    np.random.seed(42)

    # 1) Generate continuous feature
    N = 500
    x1 = np.random.normal(loc=0, scale=1.5, size=N)

    # 2) True logistic model parameters
    beta0 = -0.5
    beta1 = 2.0

    # 3) Compute probabilities
    p = expit(beta0 + beta1 * x1)

    # 4) Generate binary target
    y = np.random.binomial(1, p)

    # 5) Insert missing values (to trigger the optbinning issue)
    x1[::25] = np.nan  # every 50th sample missing

    X = pd.DataFrame({"x1": x1})

    optb = OptimalBinning(
        user_splits=[-1,0,1],
        name="x1",
        dtype="numerical",
        solver="cp",  # simplest deterministic solver
        monotonic_trend=None,  # or "ascending"/"descending"
    )

    optb.fit(X["x1"], y)
    X_binned = optb.transform(X["x1"], metric="woe")
    binning_table = optb.binning_table.build()
    optb.binning_table.plot(
        show_bin_labels=True, add_special=False, add_missing=True, metric="woe"
    )
    optb.binning_table.plot(
        show_bin_labels=True, add_special=True, add_missing=True, metric="woe"
    )


if __name__ == "__main__":
    main()

If add_special = False:
Image
If add_special = True:

Image

Note that the WoE value for "Missing" is correct in the second case.

I will link a PR to this Issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions