Skip to content

Commit

Permalink
tests: use fixed initialization of x in sparse-dense test
Browse files Browse the repository at this point in the history
  • Loading branch information
jolars committed Feb 7, 2024
1 parent 0fec57f commit ee072fa
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,40 @@ def test_simple_problem():

def test_sparse_dense_standardized():
"""Test case for checking standardization works for both sparse and dense."""
n = 10
p = 3
x_dense = np.array(
[
[0.0, 0.13339576, 0.49361983],
[0.17769259, 0.66565742, 0.36972579],
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.94280368],
[0.0, 0.0, 0.3499374],
[0.0, 0.22377115, 0.0],
[0.0, 0.96893287, 0.95858229],
[0.70486475, 0.60885162, 0.0],
[0.0, 0.92902639, 0.0],
[0.4978676, 0.0, 0.50022619],
]
)

x_sparse = csc_array(x_dense)

rng = default_rng(4)
x = csc_array(random(n, p, density=0.5, random_state=rng))
beta = np.array([1.0, 2, -0.9])
y = x @ beta
y = x_dense @ beta

lam = np.array([0.5, 0.5, 0.2])
alpha = 1.0

model = Slope(lam, alpha, standardize=True)
model = Slope(lam, alpha, standardize=True, fit_intercept=False)

coef_true = np.array([[0.04258934], [0.74274634], [-0.02910647]])

model.fit(x, y)
model.fit(x_dense, y)
coef_sparse = model.coef_

model.fit(x.toarray(), y)
coef_dense = model.coef_
model2 = Slope(lam, alpha, standardize=True, fit_intercept=False)

model2.fit(x_sparse, y)
coef_dense = model2.coef_

np.testing.assert_array_almost_equal(coef_sparse, coef_dense)
np.testing.assert_array_almost_equal(coef_dense, coef_true)

0 comments on commit ee072fa

Please sign in to comment.