Skip to content

enh: support aggregation/bagging functions other than mean #30986

Closed as not planned

Description

@AhmedThahir

Describe the workflow you want to enable

Currently KNNRegressor, BaggingRegressor and ForestRegressor only support mean

if weights is None:
y_pred = np.mean(_y[neigh_ind], axis=1)
else:
y_pred = np.empty((neigh_dist.shape[0], _y.shape[1]), dtype=np.float64)
denom = np.sum(weights, axis=1)
for j in range(_y.shape[1]):
num = np.sum(_y[neigh_ind, j] * weights, axis=1)
y_pred[:, j] = num / denom

y_hat = sum(all_y_hat) / self.n_estimators

Parallel(n_jobs=n_jobs, verbose=self.verbose, require="sharedmem")(
delayed(_accumulate_prediction)(e.predict, X, [y_hat], lock)
for e in self.estimators_
)
y_hat /= len(self.estimators_)

Describe your proposed solution

BaggingRegressor and ForestRegressor could also support median and custom aggregation function that user specifies, but will be mean by default to ensure no breaking change.

For eg:

RandomForestRegressor(..., agg="median", ...)
RandomForestRegressor(..., agg=foo, ...) # where foo is user-defined function
RandomForestRegressor(..., ...) # defaults to mean

Proposed implementation

if type(agg) == str:
     agg = getattr(np, agg) # where np is numpy
# else agg is a function, so no transformation required

y_hat = agg(all_y_hat)

Describe alternatives you've considered, if relevant

No response

Additional context

Why?
Robustness to inaccuracies of individual estimators.

Consider individual predictions are 101, 102, 103, 104, 150.

  • With mean aggregation, the output will be 112 (current implementation)
  • With median aggregation, the output will be 103

Activity

  1. changed the title [-]enh(BaggingRegressor,ForestRegressor): support aggregation functions other than mean[/-] [+]enh: support aggregation functions other than mean[/+] on Mar 13, 2025
  2. changed the title [-]enh: support aggregation functions other than mean[/-] [+]enh: support aggregation/bagging functions other than mean[/+] on Mar 13, 2025
  3. lesteve commented on Mar 13, 2025

    @lesteve
    Member

    Thanks for the issue, could you give more context and in particular why you think this is important and/or give some links to scientific articles that indicate this is useful in practice?

  4. removed
    Needs Triage
    on Mar 13, 2025
  5. AhmedThahir commented on Mar 13, 2025

    @AhmedThahir
    Author

    Usecase

    Why?
    Robustness to inaccuracies of individual estimators.

    Consider individual predictions are 101, 102, 103, 104, 150.

    With mean aggregation, the output will be 112 (current implementation)
    With median aggregation, the output will be 103

    This is similar to #9553, but it was only for RandomForest. I'm requesting for all the above mentioned estimators.

    Scientific Papers

    I did not make this request referring to any paper. If I find any, I'll cite here ASAP.

  6. AhmedThahir commented on Jul 2, 2025

    @AhmedThahir
    Author

    Hi @lesteve, hope you are doing well.

    I was not able to find any papers for this.

    If it is not a good idea to implement this in sciki-learn, we can close this issue to keep the issues list clean.

    Those interested can create a custom estimator on their own using my proposed logic.

  7. lesteve commented on Jul 10, 2025

    @lesteve
    Member

    Thanks, let's close this then.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions