Skip to content

Make it possible to specify interaction_cst and monotonic_cst with feature names. #24852

Closed

Description

@ogrisel

Describe the workflow you want to enable

Instead of passing an array of monotonicity constraints (-1 for a decrease constraint, +1 for an increase constraint or 0 for no constraint) specified by feature positions in the training set, it would be more convenient to pass a dict to pass constraints spec only for the required feature names (when those are available as str values in the dataset columns). For instance

from sklearn.datasets import load_diabetes
from sklearn.ensemble import HistGradientBoostingRegressor

X, y = load_diabetes(return_X_y=True, as_frame=True)

reg = HistGradientBoostingRegressor(
    monotonic_cst={"bmi": +1, "s3": -1}
)
reg.fit(X, y)

Not that here X has column names because it is a pandas dataframe.

See #24845 for a similar feature for interaction_cst by passing a list of tuple of str names instead.

Describe your proposed solution

This requires updating the fit method, docstring and examples to accept a dict of constraints with feature names as keys.

If feature_names_in_ is not defined in fit, then a value error with a helpful error message must be raised.

Describe alternatives you've considered, if relevant

No response

Additional context

Once #13649 is finalized and merged, a similar treatment should be adapted to it for the sake of consistency.

Activity

  1. self-assigned this
    on Nov 7, 2022
  2. ogrisel commented on Nov 7, 2022

    @ogrisel
    MemberAuthor

    I will give it a quick shot.

  3. ogrisel commented on Nov 10, 2022

    @ogrisel
    MemberAuthor

    I am working on a similar UX improvement for categorical_features.

  4. lorentzenchr commented on Nov 11, 2022

    @lorentzenchr
    Member

    If feature_names_in_ is defined in fit, then a value error with a helpful error message must be raised.

    Did you mean „if … is NOT defined, …“?

  5. ogrisel commented on Nov 15, 2022

    @ogrisel
    MemberAuthor

    monotonic_cst was implemented in:

    and something similar for categorical_features in:

    but we still need to do the same for interaction_cst, maybe after #24849 has been reviewed and merged.

  6. lorentzenchr commented on Nov 30, 2022

    @lorentzenchr
    Member

    I guess this can be closed with #24855, #24889 and #24849 merged.

  7. lorentzenchr commented on Mar 28, 2026

    @lorentzenchr
    Member

    And #21962 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions