Skip to content

ENH Add Poisson criterion to RandomForestRegressor #19464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions sklearn/ensemble/_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ def fit(self, X, y, sample_weight=None):
# Remap output
self.n_features_ = X.shape[1]

# add input validation for poisson criterion
if (self.criterion == "poisson" and np.any(y < 0)):
raise ValueError("Some value(s) of y are negative which is"
" not allowed for Poisson regression."
)
Comment on lines +319 to +322
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (self.criterion == "poisson" and np.any(y < 0)):
raise ValueError("Some value(s) of y are negative which is"
" not allowed for Poisson regression."
)
if self.criterion == "poisson":
if np.any(y < 0):
raise ValueError("Some value(s) of y are negative which is"
" not allowed for Poisson regression.")
if np.sum(y) <= 0:
raise ValueError("Sum of y is not positive which is "
"necessary for Poisson regression.")

Copy link
Member

@ogrisel ogrisel Feb 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the later error message would make more sense if we emphasized that the sum of y needs to be strictly positive. Alternatively we can could say that y cannot be uniformly zero.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointing out strict positiveness is good. Note that the above is the exact same error message as in the tree module. If we improve it here, the same change should be applied there.


y = np.atleast_1d(y)
if y.ndim == 2 and y.shape[1] == 1:
warn("A column-vector y was passed when a 1d array was"
Expand Down Expand Up @@ -1298,14 +1304,16 @@ class RandomForestRegressor(ForestRegressor):
The default value of ``n_estimators`` changed from 10 to 100
in 0.22.

criterion : {"mse", "mae"}, default="mse"
criterion : {"mse", "mae", "poisson"}, default="mse"
The function to measure the quality of a split. Supported criteria
are "mse" for the mean squared error, which is equal to variance
reduction as feature selection criterion, and "mae" for the mean
absolute error.
reduction as feature selection criterion, "mae" for the mean
absolute error, and "poisson" which uses reduction in
Poisson deviance to find splits.

.. versionadded:: 0.18
Mean Absolute Error (MAE) criterion.
Comment on lines -1307 to -1308
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this change, i.e. let this versionadded untouched as is.


.. versionadded:: 1.0
Added criterion "poisson".

max_depth : int, default=None
The maximum depth of the tree. If None, then nodes are expanded until
Expand Down