Skip to content
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

[enhancement] WIP new finite checking in LinearRegression, Ridge and incremental variants #2206

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update test_linear.py
icfaust authored Dec 4, 2024
commit 86d7bfa9603e9f80e15809fec670e2d586c34e9a
56 changes: 56 additions & 0 deletions sklearnex/linear_model/tests/test_linear.py
Original file line number Diff line number Diff line change
@@ -140,3 +140,59 @@ def test_sklearnex_reconstruct_model(dataframe, queue, dtype):

tol = 1e-5 if _as_numpy(y_pred).dtype == np.float32 else 1e-7
assert_allclose(gtr, _as_numpy(y_pred), rtol=tol)


@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
@pytest.mark.parametrize("fit_intercept", [False, True])
@pytest.mark.parametrize("problem_type", ["regular", "overdetermined", "singular"])
@pytest.mark.skipif(
not daal_check_version((2025, "P", 1)),
reason="Functionality introduced in the versions >= 2025.0",
)
def test_multioutput_regression(dataframe, queue, dtype, fit_intercept, problem_type):
if (
problem_type != "regular"
and queue
and queue.sycl_device.is_gpu
and not daal_check_version((2025, "P", 200))
):
pytest.skip("Functionality introduced in later versions")
gen = np.random.default_rng(seed=123)
if problem_type == "regular":
X_0 = gen.standard_normal(size=(20, 5))
elif problem_type == "singular":
X_0 = gen.standard_normal(size=(20, 4))
X_0[:, 3] = X_0[:, 2]
else:
X_0 = gen.standard_normal(size=(10, 20))
y_0 = gen.standard_normal(size=(X.shape[0], 3), dtype=dtype)

X = _convert_to_dataframe(X_0, sycl_queue=queue, target_df=dataframe)
y = _convert_to_dataframe(y_0, sycl_queue=queue, target_df=dataframe)

model = LinearRegression(fit_intercept=fit_intercept).fit(X, y)
if not fit_intercept:
A = X.T @ X
b = X.T @ y
x = model.coef_.T
else:
Xi = np.c_[X, np.ones((X.shape[0], 1))]
A = Xi.T @ Xi
b = Xi.T @ y
x = np.r_[model.coef_.T, model.intercept_.reshape((1, -1))]
residual = A @ x - b
assert np.all(np.abs(residual) < 1e-5)

pred = model.predict(X)
expected_pred = X @ model.coef_.T + model.intercept_.reshape((1, -1))
tol = 1e-5 if pred.dtype == np.float32 else 1e-7
assert_allclose(pred, expected_pred, rtol=tol)

# check that it also works when 'y' is a list of lists
if dataframe == "numpy":
y_lists = y_0.tolist()
model_lists = LinearRegression(fit_intercept=fit_intercept).fit(X_0, y_lists)
assert_allclose(model.coef_, model_lists.coef_)
if fit_intercept:
assert_allclose(model.intercept_, model_lists.intercept_)