|
39 | 39 | from sklearn.exceptions import DataConversionWarning
|
40 | 40 | from sklearn.exceptions import NotFittedError
|
41 | 41 | from sklearn.dummy import DummyClassifier, DummyRegressor
|
| 42 | +from sklearn.pipeline import make_pipeline |
| 43 | +from sklearn.linear_model import LinearRegression |
| 44 | +from sklearn.svm import NuSVR |
42 | 45 |
|
43 | 46 |
|
44 | 47 | GRADIENT_BOOSTING_ESTIMATORS = [GradientBoostingClassifier,
|
@@ -1378,6 +1381,33 @@ def test_gradient_boosting_with_init(gb, dataset_maker, init_estimator):
|
1378 | 1381 | gb(init=init_est).fit(X, y, sample_weight=sample_weight)
|
1379 | 1382 |
|
1380 | 1383 |
|
| 1384 | +def test_gradient_boosting_with_init_pipeline(): |
| 1385 | + # Check that the init estimator can be a pipeline (see issue #13466) |
| 1386 | + |
| 1387 | + X, y = make_regression(random_state=0) |
| 1388 | + init = make_pipeline(LinearRegression()) |
| 1389 | + gb = GradientBoostingRegressor(init=init) |
| 1390 | + gb.fit(X, y) # pipeline without sample_weight works fine |
| 1391 | + |
| 1392 | + with pytest.raises( |
| 1393 | + ValueError, |
| 1394 | + match='The initial estimator Pipeline does not support sample ' |
| 1395 | + 'weights'): |
| 1396 | + gb.fit(X, y, sample_weight=np.ones(X.shape[0])) |
| 1397 | + |
| 1398 | + # Passing sample_weight to a pipeline raises a ValueError. This test makes |
| 1399 | + # sure we make the distinction between ValueError raised by a pipeline that |
| 1400 | + # was passed sample_weight, and a ValueError raised by a regular estimator |
| 1401 | + # whose input checking failed. |
| 1402 | + with pytest.raises( |
| 1403 | + ValueError, |
| 1404 | + match='nu <= 0 or nu > 1'): |
| 1405 | + # Note that NuSVR properly supports sample_weight |
| 1406 | + init = NuSVR(gamma='auto', nu=1.5) |
| 1407 | + gb = GradientBoostingRegressor(init=init) |
| 1408 | + gb.fit(X, y, sample_weight=np.ones(X.shape[0])) |
| 1409 | + |
| 1410 | + |
1381 | 1411 | @pytest.mark.parametrize('estimator, missing_method', [
|
1382 | 1412 | (GradientBoostingClassifier(init=LinearSVC()), 'predict_proba'),
|
1383 | 1413 | (GradientBoostingRegressor(init=OneHotEncoder()), 'predict')
|
|
0 commit comments