category_encoders/supervised-encoder-without-target
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| Category Encoders | error | none, edit by hand | All versions |
A supervised encoder learns from the target, so fit(X) without it raises. Pass the target as the second argument.
X_encoded = ce.TargetEncoder(cols=["city"]).fit_transform(X)X_encoded = ce.TargetEncoder(cols=["city"]).fit_transform(X, y)What the rule reports
Section titled “What the rule reports”The rule reports fit(X) and fit_transform(X) without the target on a supervised encoder.
The rule also checks module-level assignments when the fit call is outside a function. It skips error-handling paths such as pytest.raises, assertRaises, and try blocks with an except clause.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
The supervised encoders are CatBoostEncoder, CountTargetEncoder, GLMMEncoder,
JamesSteinEncoder, LeaveOneOutEncoder, MEstimateEncoder, QuantileEncoder,
SummaryEncoder, TargetEncoder and WOEEncoder.
| Call | Error |
|---|---|
fit_transform(X), on each of the ten |
TypeError: fit_transform() missing argument: y |
fit(X), on nine of them |
ValueError: Supervised encoders need a target for the fitting. The target cannot be None |
fit(X), on SummaryEncoder, whose fit has no default for y |
TypeError |
A scikit-learn transformer takes fit(X), so the form is easy to carry over from an
OrdinalEncoder or a OneHotEncoder. The rule reports the call on an encoder built in the same
expression, and on a name that is assigned from a supervised class in the same function and from
nothing else there.
Diagnostic message
Section titled “Diagnostic message”$C learns from the target, and $M without it raises on category_encoders 2.11.2, with TypeError from fit_transform and ValueError from fit. Pass the target as the second argument.See all rules in the Category Encoders pack.