8000 TST Extend tests for `scipy.sparse.*array` in `sklearn/tests/test_kernel_ridge.py` by kianelbo · Pull Request #27270 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

TST Extend tests for scipy.sparse.*array in sklearn/tests/test_kernel_ridge.py #27270

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 2 commits into from
Sep 11, 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
25 changes: 8 additions & 17 deletions sklearn/tests/test_kernel_ridge.py
8000
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import numpy as np
import scipy.sparse as sp
import pytest

from sklearn.datasets import make_regression
from sklearn.kernel_ridge import KernelRidge
from sklearn.linear_model import Ridge
from sklearn.metrics.pairwise import pairwise_kernels
from sklearn.utils._testing import assert_array_almost_equal, ignore_warnings
from sklearn.utils.fixes import CSC_CONTAINERS, CSR_CONTAINERS

X, y = make_regression(n_features=10, random_state=0)
Xcsr = sp.csr_matrix(X)
Xcsc = sp.csc_matrix(X)
Y = np.array([y, y]).T


Expand All @@ -19,23 +18,15 @@ def test_kernel_ridge():
assert_array_almost_equal(pred, pred2)


def test_kernel_ridge_csr():
@pytest.mark.parametrize("sparse_container", [*CSR_CONTAINERS, *CSC_CONTAINERS])
def test_kernel_ridge_sparse(sparse_container):
X_sparse = sparse_container(X)
pred = (
Ridge(alpha=1, fit_intercept=False, solver="cholesky")
.fit(Xcsr, y)
.predict(Xcsr)
.fit(X_sparse, y)
.predict(X_sparse)
)
pred2 = KernelRidge(kernel="linear", alpha=1).fit(Xcsr, y).predict(Xcsr)
assert_array_almost_equal(pred, pred2)


def test_kernel_ridge_csc():
pred = (
Ridge(alpha=1, fit_intercept=False, solver="cholesky")
.fit(Xcsc, y)
.predict(Xcsc)
)
pred2 = KernelRidge(kernel="linear", alpha=1).fit(Xcsc, y).predict(Xcsc)
pred2 = KernelRidge(kernel="linear", alpha=1).fit(X_sparse, y).predict(X_sparse)
assert_array_almost_equal(pred, pred2)


Expand Down
0