Skip to content

Commit 0b6f442

Browse files
rprkhglemaitre
andauthored
TST Extend tests for scipy.sparse.*array in sklearn/model_selection/tests/test_search.py (scikit-learn#27286)
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
1 parent 3f11069 commit 0b6f442

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

sklearn/model_selection/tests/test_search.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import numpy as np
1313
import pytest
14-
import scipy.sparse as sp
1514
from scipy.stats import bernoulli, expon, uniform
1615

1716
from sklearn.base import BaseEstimator, ClassifierMixin, is_classifier
@@ -77,6 +76,7 @@
7776
assert_array_equal,
7877
ignore_warnings,
7978
)
79+
from sklearn.utils.fixes import CSR_CONTAINERS
8080
from sklearn.utils.validation import _num_samples
8181

8282

@@ -489,7 +489,8 @@ def test_grid_search_bad_param_grid():
489489
search.fit(X, y)
490490

491491

492-
def test_grid_search_sparse():
492+
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
493+
def test_grid_search_sparse(csr_container):
493494
# Test that grid search works with both dense and sparse matrices
494495
X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0)
495496

@@ -499,7 +500,7 @@ def test_grid_search_sparse():
499500
y_pred = cv.predict(X_[180:])
500501
C = cv.best_estimator_.C
501502

502-
X_ = sp.csr_matrix(X_)
503+
X_ = csr_container(X_)
503504
clf = LinearSVC(dual="auto")
504505
cv = GridSearchCV(clf, {"C": [0.1, 1.0]})
505506
cv.fit(X_[:180].tocoo(), y_[:180])
@@ -510,7 +511,8 @@ def test_grid_search_sparse():
510511
assert C == C2
511512

512513

513-
def test_grid_search_sparse_scoring():
514+
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
515+
def test_grid_search_sparse_scoring(csr_container):
514516
X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0)
515517

516518
clf = LinearSVC(dual="auto")
@@ -519,7 +521,7 @@ def test_grid_search_sparse_scoring():
519521
y_pred = cv.predict(X_[180:])
520522
C = cv.best_estimator_.C
521523

522-
X_ = sp.csr_matrix(X_)
524+
X_ = csr_container(X_)
523525
clf = LinearSVC(dual="auto")
524526
cv = GridSearchCV(clf, {"C": [0.1, 1.0]}, scoring="f1")
525527
cv.fit(X_[:180], y_[:180])

0 commit comments

Comments
 (0)