diff --git a/sklearn/ensemble/tests/test_gradient_boosting.py b/sklearn/ensemble/tests/test_gradient_boosting.py index 9d61464d063f0..768063286e46d 100644 --- a/sklearn/ensemble/tests/test_gradient_boosting.py +++ b/sklearn/ensemble/tests/test_gradient_boosting.py @@ -7,7 +7,6 @@ import numpy as np import pytest from numpy.testing import assert_allclose -from scipy.sparse import coo_matrix, csc_matrix, csr_matrix from sklearn import datasets from sklearn.base import clone @@ -31,6 +30,7 @@ assert_array_equal, skip_if_32bit, ) +from sklearn.utils.fixes import COO_CONTAINERS, CSC_CONTAINERS, CSR_CONTAINERS GRADIENT_BOOSTING_ESTIMATORS = [GradientBoostingClassifier, GradientBoostingRegressor] @@ -288,11 +288,12 @@ def test_single_class_with_sample_weight(): clf.fit(X, y, sample_weight=sample_weight) -def test_check_inputs_predict_stages(): +@pytest.mark.parametrize("csc_container", CSC_CONTAINERS) +def test_check_inputs_predict_stages(csc_container): # check that predict_stages through an error if the type of X is not # supported x, y = datasets.make_hastie_10_2(n_samples=100, random_state=1) - x_sparse_csc = csc_matrix(x) + x_sparse_csc = csc_container(x) clf = GradientBoostingClassifier(n_estimators=100, random_state=1) clf.fit(x, y) score = np.zeros((y.shape)).reshape(-1, 1) @@ -913,10 +914,12 @@ def test_warm_start_oob(Cls): @pytest.mark.parametrize("Cls", GRADIENT_BOOSTING_ESTIMATORS) -def test_warm_start_sparse(Cls): +@pytest.mark.parametrize( + "sparse_container", COO_CONTAINERS + CSC_CONTAINERS + CSR_CONTAINERS +) +def test_warm_start_sparse(Cls, sparse_container): # Test that all sparse matrix types are supported X, y = datasets.make_hastie_10_2(n_samples=100, random_state=1) - sparse_matrix_type = [csr_matrix, csc_matrix, coo_matrix] est_dense = Cls( n_estimators=100, max_depth=1, subsample=0.5, random_state=1, warm_start=True ) @@ -926,31 +929,28 @@ def test_warm_start_sparse(Cls): est_dense.fit(X, y) y_pred_dense = est_dense.predict(X) - for sparse_constructor in sparse_matrix_type: - X_sparse = sparse_constructor(X) + X_sparse = sparse_container(X) - est_sparse = Cls( - n_estimators=100, - max_depth=1, - subsample=0.5, - random_state=1, - warm_start=True, - ) - est_sparse.fit(X_sparse, y) - est_sparse.predict(X) - est_sparse.set_params(n_estimators=200) - est_sparse.fit(X_sparse, y) - y_pred_sparse = est_sparse.predict(X) + est_sparse = Cls( + n_estimators=100, + max_depth=1, + subsample=0.5, + random_state=1, + warm_start=True, + ) + est_sparse.fit(X_sparse, y) + est_sparse.predict(X) + est_sparse.set_params(n_estimators=200) + est_sparse.fit(X_sparse, y) + y_pred_sparse = est_sparse.predict(X) - assert_array_almost_equal( - est_dense.oob_improvement_[:100], est_sparse.oob_improvement_[:100] - ) - assert est_dense.oob_scores_[-1] == pytest.approx(est_dense.oob_score_) - assert_array_almost_equal( - est_dense.oob_scores_[:100], est_sparse.oob_scores_[:100] - ) - assert est_sparse.oob_scores_[-1] == pytest.approx(est_sparse.oob_score_) - assert_array_almost_equal(y_pred_dense, y_pred_sparse) + assert_array_almost_equal( + est_dense.oob_improvement_[:100], est_sparse.oob_improvement_[:100] + ) + assert est_dense.oob_scores_[-1] == pytest.approx(est_dense.oob_score_) + assert_array_almost_equal(est_dense.oob_scores_[:100], est_sparse.oob_scores_[:100]) + assert est_sparse.oob_scores_[-1] == pytest.approx(est_sparse.oob_score_) + assert_array_almost_equal(y_pred_dense, y_pred_sparse) @pytest.mark.parametrize("Cls", GRADIENT_BOOSTING_ESTIMATORS) @@ -1173,13 +1173,15 @@ def test_non_uniform_weights_toy_edge_case_clf(): @pytest.mark.parametrize( "EstimatorClass", (GradientBoostingClassifier, GradientBoostingRegressor) ) -@pytest.mark.parametrize("sparse_matrix", (csr_matrix, csc_matrix, coo_matrix)) -def test_sparse_input(EstimatorClass, sparse_matrix): +@pytest.mark.parametrize( + "sparse_container", COO_CONTAINERS + CSC_CONTAINERS + CSR_CONTAINERS +) +def test_sparse_input(EstimatorClass, sparse_container): y, X = datasets.make_multilabel_classification( random_state=0, n_samples=50, n_features=1, n_classes=20 ) y = y[:, 0] - X_sparse = sparse_matrix(X) + X_sparse = sparse_container(X) dense = EstimatorClass( n_estimators=10, random_state=0, max_depth=2, min_impurity_decrease=1e-7