Repository navigation
Closed as not planned
Labels
Description
Describe the workflow you want to enable
Currently KNNRegressor, BaggingRegressor and ForestRegressor only support mean
scikit-learn/sklearn/neighbors/_regression.py
Lines 254 to 262 in 98ed9dc
| 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 |
scikit-learn/sklearn/ensemble/_bagging.py
Line 1295 in 98ed9dc
| y_hat = sum(all_y_hat) / self.n_estimators |
scikit-learn/sklearn/ensemble/_forest.py
Lines 1079 to 1084 in 98ed9dc
| 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 meanProposed 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
[-]enh(BaggingRegressor,ForestRegressor): support aggregation functions other than mean[/-][+]enh: support aggregation functions other than mean[/+][-]enh: support aggregation functions other than mean[/-][+]enh: support aggregation/bagging functions other than mean[/+]lesteve commentedon Mar 13, 2025
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?
AhmedThahir commentedon Mar 13, 2025
Usecase
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.
AhmedThahir commentedon Jul 2, 2025
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.
lesteve commentedon Jul 10, 2025
Thanks, let's close this then.