|
27 | 27 | import joblib
|
28 | 28 | from numpy.testing import assert_allclose
|
29 | 29 |
|
| 30 | +from sklearn.dummy import DummyRegressor |
| 31 | +from sklearn.metrics import mean_poisson_deviance |
30 | 32 | from sklearn.utils._testing import assert_almost_equal
|
31 | 33 | from sklearn.utils._testing import assert_array_almost_equal
|
32 | 34 | from sklearn.utils._testing import assert_array_equal
|
@@ -185,6 +187,76 @@ def test_regression(name, criterion):
|
185 | 187 | check_regression_criterion(name, criterion)
|
186 | 188 |
|
187 | 189 |
|
| 190 | +def test_poisson_vs_mse(): |
| 191 | + """Test that random forest with poisson criterion performs better than |
| 192 | + mse for a poisson target.""" |
| 193 | + rng = np.random.RandomState(42) |
| 194 | + n_train, n_test, n_features = 500, 500, 10 |
| 195 | + X = datasets.make_low_rank_matrix(n_samples=n_train + n_test, |
| 196 | + n_features=n_features, random_state=rng) |
| 197 | + X = np.abs(X) |
| 198 | + X /= np.max(np.abs(X), axis=0) |
| 199 | + # We create a log-linear Poisson model |
| 200 | + coef = rng.uniform(low=-4, high=1, size=n_features) |
| 201 | + y = rng.poisson(lam=np.exp(X @ coef)) |
| 202 | + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=n_test, |
| 203 | + random_state=rng) |
| 204 | + |
| 205 | + forest_poi = RandomForestRegressor( |
| 206 | + criterion="poisson", |
| 207 | + min_samples_leaf=10, |
| 208 | + max_features="sqrt", |
| 209 | + random_state=rng) |
| 210 | + forest_mse = RandomForestRegressor( |
| 211 | + criterion="squared_error", |
| 212 | + min_samples_leaf=10, |
| 213 | + max_features="sqrt", |
| 214 | + random_state=rng) |
| 215 | + |
| 216 | + forest_poi.fit(X_train, y_train) |
| 217 | + forest_mse.fit(X_train, y_train) |
| 218 | + dummy = DummyRegressor(strategy="mean").fit(X_train, y_train) |
| 219 | + |
| 220 | + for X, y, val in [(X_train, y_train, "train"), (X_test, y_test, "test")]: |
| 221 | + metric_poi = mean_poisson_deviance(y, forest_poi.predict(X)) |
| 222 | + # squared_error forest might produce non-positive predictions => clip |
| 223 | + # If y = 0 for those, the poisson deviance gets too good. |
| 224 | + # If we drew more samples, we would eventually get y > 0 and the |
| 225 | + # poisson deviance would explode, i.e. be undefined. Therefore, we do |
| 226 | + # not clip to a tiny value like 1e-15, but to 0.1. This acts like a |
| 227 | + # mild penalty to the non-positive predictions. |
| 228 | + metric_mse = mean_poisson_deviance( |
| 229 | + y, |
| 230 | + np.clip(forest_mse.predict(X), 1e-6, None)) |
| 231 | + metric_dummy = mean_poisson_deviance(y, dummy.predict(X)) |
| 232 | + # As squared_error might correctly predict 0 in train set, its train |
| 233 | + # score can be better than Poisson. This is no longer the case for the |
| 234 | + # test set. But keep the above comment for clipping in mind. |
| 235 | + if val == "test": |
| 236 | + assert metric_poi < metric_mse |
| 237 | + assert metric_poi < metric_dummy |
| 238 | + |
| 239 | + |
| 240 | +@pytest.mark.parametrize('criterion', ('poisson', 'squared_error')) |
| 241 | +def test_balance_property_random_forest(criterion): |
| 242 | + """"Test that sum(y_pred)==sum(y_true) on the training set.""" |
| 243 | + rng = np.random.RandomState(42) |
| 244 | + n_train, n_test, n_fe
F438
atures = 500, 500, 10 |
| 245 | + X = datasets.make_low_rank_matrix(n_samples=n_train + n_test, |
| 246 | + n_features=n_features, random_state=rng) |
| 247 | + |
| 248 | + coef = rng.uniform(low=-2, high=2, size=n_features) / np.max(X, axis=0) |
| 249 | + y = rng.poisson(lam=np.exp(X @ coef)) |
| 250 | + |
| 251 | + reg = RandomForestRegressor(criterion=criterion, |
| 252 | + n_estimators=10, |
| 253 | + bootstrap=False, |
| 254 | + random_state=rng) |
| 255 | + reg.fit(X, y) |
| 256 | + |
| 257 | + assert np.sum(reg.predict(X)) == pytest.approx(np.sum(y)) |
| 258 | + |
| 259 | + |
188 | 260 | def check_regressor_attributes(name):
|
189 | 261 | # Regression models should not have a classes_ attribute.
|
190 | 262 | r = FOREST_REGRESSORS[name](random_state=0)
|
@@ -1367,6 +1439,23 @@ def test_min_impurity_decrease():
|
1367 | 1439 | assert tree.min_impurity_decrease == 0.1
|
1368 | 1440 |
|
1369 | 1441 |
|
| 1442 | +def test_poisson_y_positive_check(): |
| 1443 | + est = RandomForestRegressor(criterion="poisson") |
| 1444 | + X = np.zeros((3, 3)) |
| 1445 | + |
| 1446 | + y = [-1, 1, 3] |
| 1447 | + err_msg = (r"Some value\(s\) of y are negative which is " |
| 1448 | + r"not allowed for Poisson regression.") |
| 1449 | + with pytest.raises(ValueError, match=err_msg): |
| 1450 | + est.fit(X, y) |
| 1451 | + |
| 1452 | + y = [0, 0, 0] |
| 1453 | + err_msg = (r"Sum of y is not strictly positive which " |
| 1454 | + r"is necessary for Poisson regression.") |
| 1455 | + with pytest.raises(ValueError, match=err_msg): |
| 1456 | + est.fit(X, y) |
| 1457 | + |
| 1458 | + |
1370 | 1459 | # mypy error: Variable "DEFAULT_JOBLIB_BACKEND" is not valid type
|
1371 | 1460 | class MyBackend(DEFAULT_JOBLIB_BACKEND): # type: ignore
|
1372 | 1461 | def __init__(self, *args, **kwargs):
|
|
0 commit comments