Skip to content

optbinning/binning-before-split

Pack Severity Fix Versions
optbinning error none, edit by hand All versions

Supervised optbinning transformers (OptimalBinning, ContinuousOptimalBinning, MulticlassOptimalBinning, BinningProcess, MDLP, OptimalPWBinning, SBOptimalBinning) use target y to choose bin cutpoints and Weight-of-Evidence or bin-mean values. Fitting before train_test_split or on test rows leaks holdout targets into bin boundaries and encodings.

optb = OptimalBinning(monotonic_trend="ascending").fit(x, y)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
optb = OptimalBinning(monotonic_trend="ascending").fit(x_train, y_train)
x_test_woe = optb.transform(x_test, metric="woe")

Calls to .fit(x, y) or .fit_transform(x, y) on an optbinning binner before train_test_split(...) in the same block, or on test-named inputs such as X_test.

Split the data first (or place BinningProcess inside a cross-validated scikit-learn Pipeline), fit on training rows only, and call .transform() on validation and test rows.

Supervised optbinning ($B) learns cutpoints and WoE/mean encodings from the target on every row before train_test_split, leaking holdout label distributions into bin edges. Split first, fit on training rows only, and call transform on validation or test rows.

See all rules in the optbinning pack.