|
2 | 2 |
|
3 | 3 | import numpy as np
|
4 | 4 | import pytest
|
5 |
| -import scipy.sparse as sp |
6 | 5 | from joblib import cpu_count
|
7 | 6 |
|
8 | 7 | from sklearn import datasets
|
|
49 | 48 | assert_array_almost_equal,
|
50 | 49 | assert_array_equal,
|
51 | 50 | )
|
| 51 | +from sklearn.utils.fixes import ( |
| 52 | + BSR_CONTAINERS, |
| 53 | + COO_CONTAINERS, |
| 54 | + CSC_CONTAINERS, |
| 55 | + CSR_CONTAINERS, |
| 56 | + DOK_CONTAINERS, |
| 57 | + LIL_CONTAINERS, |
| 58 | +) |
52 | 59 |
|
53 | 60 |
|
54 | 61 | def test_multi_target_regression():
|
@@ -101,25 +108,29 @@ def test_multi_target_regression_one_target():
|
101 | 108 | rgr.fit(X, y)
|
102 | 109 |
|
103 | 110 |
|
104 |
| -def test_multi_target_sparse_regression(): |
| 111 | +@pytest.mark.parametrize( |
| 112 | + "sparse_container", |
| 113 | + CSR_CONTAINERS |
| 114 | + + CSC_CONTAINERS |
| 115 | + + COO_CONTAINERS |
| 116 | + + LIL_CONTAINERS |
| 117 | + + DOK_CONTAINERS |
| 118 | + + BSR_CONTAINERS, |
| 119 | +) |
| 120 | +def test_multi_target_sparse_regression(sparse_container): |
105 | 121 | X, y = datasets.make_regression(n_targets=3, random_state=0)
|
106 | 122 | X_train, y_train = X[:50], y[:50]
|
107 | 123 | X_test = X[50:]
|
108 | 124 |
|
109 |
| - for sparse in [ |
110 |
| - sp.csr_matrix, |
111 |
| - sp.csc_matrix, |
112 |
| - sp.coo_matrix, |
113 |
| - sp.dok_matrix, |
114 |
| - sp.lil_matrix, |
115 |
| - ]: |
116 |
| - rgr = MultiOutputRegressor(Lasso(random_state=0)) |
117 |
| - rgr_sparse = MultiOutputRegressor(Lasso(random_state=0)) |
| 125 | + rgr = MultiOutputRegressor(Lasso(random_state=0)) |
| 126 | + rgr_sparse = MultiOutputRegressor(Lasso(random_state=0)) |
118 | 127 |
|
119 |
| - rgr.fit(X_train, y_train) |
120 |
| - rgr_sparse.fit(sparse(X_train), y_train) |
| 128 | + rgr.fit(X_train, y_train) |
| 129 | + rgr_sparse.fit(sparse_container(X_train), y_train) |
121 | 130 |
|
122 |
| - assert_almost_equal(rgr.predict(X_test), rgr_sparse.predict(sparse(X_test))) |
| 131 | + assert_almost_equal( |
| 132 | + rgr.predict(X_test), rgr_sparse.predict(sparse_container(X_test)) |
| 133 | + ) |
123 | 134 |
|
124 | 135 |
|
125 | 136 | def test_multi_target_sample_weights_api():
|
@@ -497,10 +508,11 @@ def test_classifier_chain_fit_and_predict_with_linear_svc():
|
497 | 508 | assert not hasattr(classifier_chain, "predict_proba")
|
498 | 509 |
|
499 | 510 |
|
500 |
| -def test_classifier_chain_fit_and_predict_with_sparse_data(): |
| 511 | +@pytest.mark.parametrize("csr_container", CSR_CONTAINERS) |
| 512 | +def test_classifier_chain_fit_and_predict_with_sparse_data(csr_container): |
501 | 513 | # Fit classifier chain with sparse data
|
502 | 514 | X, Y = generate_multilabel_dataset_with_correlations()
|
503 |
| - X_sparse = sp.csr_matrix(X) |
| 515 | + X_sparse = csr_container(X) |
504 | 516 |
|
505 | 517 | classifier_chain = ClassifierChain(LogisticRegression())
|
506 | 518 | classifier_chain.fit(X_sparse, Y)
|
@@ -555,10 +567,11 @@ def test_base_chain_fit_and_predict():
|
555 | 567 | assert isinstance(chains[1], ClassifierMixin)
|
556 | 568 |
|
557 | 569 |
|
558 |
| -def test_base_chain_fit_and_predict_with_sparse_data_and_cv(): |
| 570 | +@pytest.mark.parametrize("csr_container", CSR_CONTAINERS) |
| 571 | +def test_base_chain_fit_and_predict_with_sparse_data_and_cv(csr_container): |
559 | 572 | # Fit base chain with sparse data cross_val_predict
|
560 | 573 | X, Y = generate_multilabel_dataset_with_correlations()
|
561 |
| - X_sparse = sp.csr_matrix(X) |
| 574 | + X_sparse = csr_container(X) |
562 | 575 | base_chains = [
|
563 | 576 | ClassifierChain(LogisticRegression(), cv=3),
|
564 | 577 | RegressorChain(Ridge(), cv=3),
|
|
0 commit comments