8000 TST Extend tests for `scipy.sparse/*array` in `sklearn/ensemble/tests/test_gradient_boosting` by Charlie-XIAO · Pull Request #27217 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

TST Extend tests for scipy.sparse/*array in sklearn/ensemble/tests/test_gradient_boosting #27217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 33 additions & 31 deletions sklearn/ensemble/tests/test_gradient_boosting.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
0