Skip to content

Add metadata routing params support in the predict method of BaggingClassifier/Regressor #30808

@aperezlebel

Description

@aperezlebel

Describe the workflow you want to enable

Hello! I'm trying to use metadata routing with BaggingClassifier and BaggingRegressor however it is implemented for the fit method, not the predict one. I am wondering if there is a particular reason for not doing it on the predict function or if this is a feature that could be added. This would enable situations like the following, which currently gives an error:

import numpy as np
import sklearn
from sklearn import ensemble
from sklearn.base import BaseEstimator

sklearn.set_config(enable_metadata_routing=True)


class CustomEstimator(BaseEstimator):
    def fit(self, X, y, foo):
        return self

    def predict(self, X, bar):
        return np.zeros(X.shape[0])


estimator = CustomEstimator()
estimator.set_fit_request(foo=True)
estimator.set_predict_request(bar=True)
model = ensemble.BaggingRegressor(estimator)

n, p = 10, 2
rng = np.random.default_rng(0)
x = rng.random((n, p))
y = rng.integers(0, 2, n)

model.fit(x, y, foo=True)
model.predict(x, bar=True). # TypeError: BaggingRegressor.predict() got an unexpected keyword argument 'bar'

Describe your proposed solution

Similar to the fit method, something like:

if _routing_enabled():
    routed_params = process_routing(self, "predict", **predict_params)

However, I don't have enough understanding of the metadata routing implementation to know exactly what should be done.

Describe alternatives you've considered, if relevant

No response

Additional context

I tried to have a look at the history of PRs/Issues to find a discussion around this point, but could not find it in the PR introducing the metadata routing to these estimators (#28432).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Metadata Routingall issues related to metadata routing, slep006, sample propsNew Feature

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions