Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6833457

Browse files
author
Christian Lorentzen
committed
BUG fix and test fobidden zero node split for Poisson
1 parent 6ae5dee commit 6833457

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

sklearn/tree/_criterion.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ cdef class Poisson(RegressionCriterion):
13991399
# Poisson loss does not allow non-positive predictions. We
14001400
# therefore forbid splits that have child nodes withs
14011401
# sum(y_i) <= 0.
1402-
return INFINITY
1402+
return -INFINITY
14031403
else:
14041404
proxy_impurity_left += y_mean_left * log(y_mean_left)
14051405
proxy_impurity_right += y_mean_right * log(y_mean_right)

sklearn/tree/tests/test_tree.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,3 +1957,13 @@ def test_balance_property(criterion, tree):
19571957
reg = tree(criterion=criterion)
19581958
reg.fit(X, y)
19591959
assert np.sum(reg.predict(X)) == pytest.approx(np.sum(y))
1960+
1961+
1962+
def test_poisson_zero_nodes():
1963+
"""Test that sum(y)=0 and therefore y_pred=0 never happens on nodes."""
1964+
X = [[0, 0], [0, 1], [0, 2], [0, 3],
1965+
[1, 0], [1, 2], [1, 2], [1, 3]]
1966+
y = [0, 0, 0, 0, 1, 2, 3, 4]
1967+
reg = DecisionTreeRegressor(criterion="poisson", random_state=1)
1968+
reg.fit(X, y)
1969+
assert np.all(reg.predict(X) > 0)

0 commit comments

Comments
 (0)