8000 TST Extend tests for `scipy.sparse/*array` in `sklearn/ensemble/tests… · scikit-learn/scikit-learn@a504e68 · GitHub
[go: up one dir, main page]

Skip to content

Commit a504e68

Browse files
TST Extend tests for scipy.sparse/*array in sklearn/ensemble/tests/test_gradient_boosting (#27217)
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
1 parent fd2e5c2 commit a504e68

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

sklearn/ensemble/tests/test_gradient_boosting.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import numpy as np
88
import pytest
99
from numpy.testing import assert_allclose
10-
from scipy.sparse import coo_matrix, csc_matrix, csr_matrix
1110

1211
from sklearn import datasets
1312
from sklearn.base import clone
@@ -31,6 +30,7 @@
3130
assert_array_equal,
3231
skip_if_32bit,
3332
)
33+
from sklearn.utils.fixes import COO_CONTAINERS, CSC_CONTAINERS, CSR_CONTAINERS
3434

3535
GRADIENT_BOOSTING_ESTIMATORS = [GradientBoostingClassifier, GradientBoostingRegressor]
3636

@@ -288,11 +288,12 @@ def test_single_class_with_sample_weight():
288288
clf.fit(X, y, sample_weight=sample_weight)
289289

290290

291-
def test_check_inputs_predict_stages():
291+
@pytest.mark.parametrize("csc_container", CSC_CONTAINERS)
292+
def test_check_inputs_predict_stages(csc_container):
292293
# check that predict_stages through an error if the type of X is not
293294
# supported
294295
x, y = datasets.make_hastie_10_2(n_samples=100, random_state=1)
295-
x_sparse_csc = csc_matrix(x)
296+
x_sparse_csc = csc_container(x)
296297
clf = GradientBoostingClassifier(n_estimators=100, random_state=1)
297298
clf.fit(x, y)
298299
score = np.zeros((y.shape)).reshape(-1, 1)
@@ -913,10 +914,12 @@ def test_warm_start_oob(Cls):
913914

914915

915916
@pytest.mark.parametrize("Cls", GRADIENT_BOOSTING_ESTIMATORS)
916-
def test_warm_start_sparse(Cls):
917+
@pytest.mark.parametrize(
918+
"sparse_container", COO_CONTAINERS + CSC_CONTAINERS + CSR_CONTAINERS
919+
)
920+
def test_warm_start_sparse(Cls, sparse_container):
917921
# Test that all sparse matrix types are supported
918922
X, y = datasets.make_hastie_10_2(n_samples=100, random_state=1)
919-
sparse_matrix_type = [csr_matrix, csc_matrix, coo_matrix]
920923
est_dense = Cls(
921924
n_estimators=100, max_depth=1, subsample=0.5, random_state=1, warm_start=True
922925
)
@@ -926,31 +929,28 @@ def test_warm_start_sparse(Cls):
926929
est_dense.fit(X, y)
927930
y_pred_dense = est_dense.predict(X)
928931

929-
for sparse_constructor in sparse_matrix_type:
930-
X_sparse = sparse_constructor(X)
932+
X_sparse = sparse_container(X)
931933

932-
est_sparse = Cls(
933-
n_estimators=100,
934-
max_depth=1,
935-
subsample=0.5,
936-
random_state=1,
937-
warm_start=True,
938-
)
939-
est_sparse.fit(X_sparse, y)
940-
est_sparse.predict(X)
941-
est_sparse.set_params(n_estimators=200)
942-
est_sparse.fit(X_sparse, y)
943-
y_pred_sparse = est_sparse.predict(X)
934+
est_sparse = Cls(
935+
n_estimators=100,
936+
max_depth=1,
937+
subsample=0.5,
938+
random_state=1,
939+
warm_start=True,
940+
)
941+
est_sparse.fit(X_sparse, y)
942+
est_sparse.predict(X)
943+
est_sparse.set_params(n_estimators=200)
944+
est_sparse.fit(X_sparse, y)
945+
y_pred_sparse = est_sparse.predict(X)
944946

945-
assert_array_almost_equal(
946-
est_dense.oob_improvement_[:100], est_sparse.oob_improvement_[:100]
947-
)
948-
assert est_dense.oob_scores_[-1] == pytest.approx(est_dense.oob_score_)
949-
assert_array_almost_equal(
950-
est_dense.oob_scores_[:100], est_sparse.oob_scores_[:100]
951-
)
952-
assert est_sparse.oob_scores_[-1] == pytest.approx(est_sparse.oob_score_)
953-
assert_array_almost_equal(y_pred_dense, y_pred_sparse)
947+
assert_array_almost_equal(
948+
est_dense.oob_improvement_[:100], est_sparse.oob_improvement_[:100]
949+
)
950+
assert est_dense.oob_scores_[-1] == pytest.approx(est_dense.oob_score_)
951+
assert_array_almost_equal(est_dense.oob_scores_[:100], est_sparse.oob_scores_[:100])
952+
assert est_sparse.oob_scores_[-1] == pytest.approx(est_sparse.oob_score_)
953+
assert_array_almost_equal(y_pred_dense, y_pred_sparse)
954954

955955

956956
@pytest.mark.parametrize("Cls", GRADIENT_BOOSTING_ESTIMATORS)
@@ -1173,13 +1173,15 @@ def test_non_uniform_weights_toy_edge_case_clf():
11731173
@pytest.mark.parametrize(
11741174
"EstimatorClass", (GradientBoostingClassifier, GradientBoostingRegressor)
11751175
)
1176-
@pytest.mark.parametrize("sparse_matrix", (csr_matrix, csc_matrix, coo_matrix))
1177-
def test_sparse_input(EstimatorClass, sparse_matrix):
1176+
@pytest.mark.parametrize(
1177+
"sparse_container", COO_CONTAINERS + CSC_CONTAINERS + CSR_CONTAINERS
1178+
)
1179+
def test_sparse_input(EstimatorClass, sparse_container):
11781180
y, X = datasets.make_multilabel_classification(
11791181
random_state=0, n_samples=50, n_features=1, n_classes=20
11801182
)
11811183
y = y[:, 0]
1182-
X_sparse = sparse_matrix(X)
1184+
X_sparse = sparse_container(X)
11831185

11841186
dense = EstimatorClass(
11851187
n_estimators=10, random_state=0, max_depth=2, min_impurity_decrease=1e-7

0 commit comments

Comments
 (0)
0