-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathtest_logreg.py
More file actions
executable file
·702 lines (596 loc) · 27.1 KB
/
test_logreg.py
File metadata and controls
executable file
·702 lines (596 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# ===============================================================================
# Copyright 2021 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================
import os
import warnings
import numpy as np
import pytest
from numpy.testing import assert_allclose
from scipy.sparse import csr_matrix
from sklearn.datasets import load_breast_cancer, load_iris, make_classification
from sklearn.exceptions import ConvergenceWarning
from sklearn.linear_model import LogisticRegression as _sklearn_LogisticRegression
from sklearn.metrics import accuracy_score, log_loss
from sklearn.model_selection import train_test_split
from daal4py.sklearn._utils import daal_check_version, sklearn_check_version
from onedal.tests.utils._dataframes_support import (
_as_numpy,
_convert_to_dataframe,
get_dataframes_and_queues,
get_queues,
)
from sklearnex import config_context
def prepare_input(X, y, dataframe, queue):
X_train, X_test, y_train, y_test = train_test_split(
X, y, train_size=0.8, random_state=42
)
X_train = _convert_to_dataframe(X_train, sycl_queue=queue, target_df=dataframe)
y_train = _convert_to_dataframe(y_train, sycl_queue=queue, target_df=dataframe)
X_test = _convert_to_dataframe(X_test, sycl_queue=queue, target_df=dataframe)
return X_train, X_test, y_train, y_test
def check_preview_is_enabled() -> bool:
return "SKLEARNEX_PREVIEW" in os.environ
@pytest.mark.parametrize(
"dataframe,queue", get_dataframes_and_queues(device_filter_="cpu")
)
def test_sklearnex_multiclass_classification(dataframe, queue):
from sklearnex.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = prepare_input(X, y, dataframe, queue=queue)
logreg = LogisticRegression(fit_intercept=True, solver="lbfgs", max_iter=200).fit(
X_train, y_train
)
if daal_check_version((2024, "P", 1)):
assert "sklearnex" in logreg.__module__
else:
assert "daal4py" in logreg.__module__
y_pred = _as_numpy(logreg.predict(X_test))
assert accuracy_score(y_test, y_pred) > 0.99
@pytest.mark.parametrize(
"dataframe,queue",
get_dataframes_and_queues(),
)
def test_sklearnex_binary_classification(dataframe, queue):
if (not queue or queue.sycl_device.is_cpu) and not check_preview_is_enabled():
pytest.skip("Functionality in preview")
from sklearnex.linear_model import LogisticRegression
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = prepare_input(X, y, dataframe, queue=queue)
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=ConvergenceWarning)
logreg = LogisticRegression(
fit_intercept=True, solver="newton-cg", max_iter=100
).fit(X_train, y_train)
if daal_check_version((2024, "P", 1)):
assert "sklearnex" in logreg.__module__
else:
assert "daal4py" in logreg.__module__
if (
dataframe != "numpy"
and queue is not None
and queue.sycl_device.is_gpu
and daal_check_version((2024, "P", 1))
):
# fit was done on gpu
assert hasattr(logreg, "_onedal_estimator")
y_pred = _as_numpy(logreg.predict(X_test))
assert accuracy_score(y_test, y_pred) > 0.95
if daal_check_version((2024, "P", 700)):
@pytest.mark.parametrize("queue", get_queues("gpu"))
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
@pytest.mark.parametrize(
"dims", [(3007, 17, 0.05), (50000, 100, 0.01), (512, 10, 0.5)]
)
@pytest.mark.allow_sklearn_fallback
def test_csr(queue, dtype, dims):
from sklearnex.linear_model import LogisticRegression
n, p, density = dims
# Create sparse dataset for classification
X, y = make_classification(n, p, random_state=42)
X = X.astype(dtype)
y = y.astype(dtype)
np.random.seed(2007 + n + p)
mask = np.random.binomial(1, density, (n, p))
X = X * mask
X_sp = csr_matrix(X)
model = LogisticRegression(fit_intercept=True, solver="newton-cg")
model_sp = LogisticRegression(fit_intercept=True, solver="newton-cg")
with config_context(target_offload="gpu:0"):
model.fit(X, y)
pred = model.predict(X)
prob = model.predict_proba(X)
raw = model.decision_function(X)
model_sp.fit(X_sp, y)
pred_sp = model_sp.predict(X_sp)
prob_sp = model_sp.predict_proba(X_sp)
raw_sp = model.decision_function(X_sp)
rtol = 2e-4
assert_allclose(pred, pred_sp, rtol=rtol)
assert_allclose(prob, prob_sp, rtol=rtol)
assert_allclose(raw, raw_sp, rtol=rtol)
assert_allclose(model.coef_, model_sp.coef_, rtol=rtol)
assert_allclose(model.intercept_, model_sp.intercept_, rtol=rtol)
# Note: this is adapted from a test in scikit-learn:
# https://github.com/scikit-learn/scikit-learn/blob/9b7a86fb6d45905eec7b7afd01d3ae32643c8180/sklearn/linear_model/tests/test_logistic.py#L1494
# Here we don't always expect them to match exactly due to differences in numerical precision
# and how each library deals with large/small numbers, but oftentimes the results from oneDAL
# end up being better in terms of resulting function values (for the objective function being
# minimized), hence this test will try to look at function values if coefficients aren't
# sufficiently similar.
def test_logistic_regression_is_correct():
from sklearnex.linear_model import LogisticRegression
X = np.array([[-1, 0], [0, 1], [1, 1]])
y = np.array([0, 1, 1])
C = 3.0
model_sklearn = _sklearn_LogisticRegression(C=C).fit(X, y)
model_sklearnex = LogisticRegression(C=C).fit(X, y)
try:
np.testing.assert_allclose(model_sklearnex.coef_, model_sklearn.coef_)
np.testing.assert_allclose(model_sklearnex.intercept_, model_sklearn.intercept_)
except AssertionError:
def logistic_model_function(predicted_probabilities, coefs):
neg_log_likelihood = X.shape[0] * log_loss(y, predicted_probabilities)
sum_squares_coefs = np.dot(coefs.reshape(-1), coefs.reshape(-1))
return C * neg_log_likelihood + 0.5 * sum_squares_coefs
fn_sklearn = logistic_model_function(
model_sklearn.predict_proba(X)[:, 1], model_sklearn.coef_
)
fn_sklearnex = logistic_model_function(
model_sklearnex.predict_proba(X)[:, 1], model_sklearnex.coef_
)
assert fn_sklearnex <= fn_sklearn
def test_multinomial_logistic_regression_is_correct():
from sklearnex.linear_model import LogisticRegression
X = np.array([[-1, 0], [0, 1], [1, 1]])
y = np.array([2, 1, 0])
params = {"C": 3.0}
# for sklearn 1.8 and onwards, non-binary class datasets will multinomial
if not sklearn_check_version("1.8"):
params["multi_class"] = "multinomial"
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=FutureWarning)
model_sklearn = _sklearn_LogisticRegression(**params).fit(X, y)
model_sklearnex = LogisticRegression(**params).fit(X, y)
try:
np.testing.assert_allclose(model_sklearnex.coef_, model_sklearn.coef_)
np.testing.assert_allclose(model_sklearnex.intercept_, model_sklearn.intercept_)
except AssertionError:
def logistic_model_function(predicted_probabilities, coefs):
neg_log_likelihood = X.shape[0] * log_loss(y, predicted_probabilities)
sum_squares_coefs = np.dot(coefs.reshape(-1), coefs.reshape(-1))
return params["C"] * neg_log_likelihood + 0.5 * sum_squares_coefs
fn_sklearn = logistic_model_function(
model_sklearn.predict_proba(X), model_sklearn.coef_
)
fn_sklearnex = logistic_model_function(
model_sklearnex.predict_proba(X), model_sklearnex.coef_
)
assert fn_sklearnex <= fn_sklearn
# Here, scikit-learn does a theoretically incorrect calculation in which
# they set the predictions for the 'negative' class as the negative of the
# predictions for the positive class instead of all-zeros. The idea is to
# match theirs, which is done by falling back. This test ensures that the
# predictions match with sklearn in case it isn't done during conformance tests.
@pytest.mark.parametrize("fit_intercept", [False, True])
@pytest.mark.parametrize("C", [1, 0.1])
@pytest.mark.allow_sklearn_fallback
def test_binary_multinomial_probabilities(fit_intercept, C):
from sklearnex.linear_model import LogisticRegression
# Adapted from this test:
# https://github.com/scikit-learn/scikit-learn/blob/baf828ca126bcb2c0ad813226963621cafe38adb/sklearn/utils/estimator_checks.py#L963
X = np.array(
[
[1, 3],
[1, 3],
[1, 3],
[1, 3],
[2, 1],
[2, 1],
[2, 1],
[2, 1],
[3, 3],
[3, 3],
[3, 3],
[3, 3],
[4, 1],
[4, 1],
[4, 1],
[4, 1],
],
dtype=np.float64,
)
y_binary = np.array([1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int)
params = {"C": C, "fit_intercept": fit_intercept}
if not sklearn_check_version("1.8"):
params["multi_class"] = "multinomial"
with warnings.catch_warnings():
warnings.simplefilter("ignore")
model_sklearnex = LogisticRegression(**params).fit(X, y_binary)
model_sklearn = _sklearn_LogisticRegression(**params).fit(X, y_binary)
np.testing.assert_allclose(
model_sklearnex.predict_proba(X),
model_sklearn.predict_proba(X),
rtol=1e-2,
atol=1e-3,
)
# Note: some solvers have an internal state, such as previous gradients,
# which is not preserved across warm starts and which influences the
# optimization routines. For these, a warm-started call with the coefficients
# from a previous iterations will not be equal to a cold-start call with
# one more iteration.
# Note2: usually, passing weights will cause the procedure to fall back to
# stock scikit-learn. We want to check that fallbacks also handle warm starts
# correctly when falling back.
@pytest.mark.parametrize("fit_intercept", [False, True])
@pytest.mark.parametrize("n_classes", [2, 3])
@pytest.mark.parametrize(
"multi_class", ["auto", "multinomial"] if not sklearn_check_version("1.8") else [None]
)
@pytest.mark.parametrize("weighted", [False, True])
@pytest.mark.allow_sklearn_fallback
def test_warm_start_stateful(fit_intercept, n_classes, multi_class, weighted):
from sklearnex.linear_model import LogisticRegression
X, y = make_classification(
random_state=123,
n_classes=n_classes,
n_clusters_per_class=1,
n_features=2,
n_redundant=0,
# Note: oneDAL and scikit-learn deal with large numbers differently
# in the calculations, so when comparing against sklearn, we want
# to avoid ending up with large numbers in the computations.
class_sep=0.25,
)
# Note1: these will throw warnings due to reaching the maximum
# number of iterations without converging, which is expected
# given that those are being severely limited for the tests.
# Note2: this will first compare the results after one iteration, and
# if those already differ too much (which can be the case given numerical
# differences), will then skip the rest of test that checks the warm starts.
params = {
"solver": "lbfgs",
"fit_intercept": fit_intercept,
"max_iter": 1,
"warm_start": True,
}
if not sklearn_check_version("1.8"):
params["multi_class"] = multi_class
model1 = _sklearn_LogisticRegression(**params)
model2 = LogisticRegression(**params)
for est in (model1, model2):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
est.fit(
X,
y,
np.ones(X.shape[0]) if weighted else None,
)
try:
np.testing.assert_allclose(model1.coef_, model2.coef_)
except AssertionError:
pytest.skip("Too large numerical differences for further comparisons")
for est in (model1, model2):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
est.fit(
X,
y,
np.ones(X.shape[0]) if weighted else None,
)
np.testing.assert_allclose(model1.coef_, model2.coef_)
if fit_intercept:
if n_classes == 2:
np.testing.assert_allclose(model1.intercept_, model2.intercept_)
else:
# Note: softmax function is invariable to shifting by a constant
intercepts1 = model1.intercept_ - model1.intercept_.mean()
intercepts2 = model2.intercept_ - model2.intercept_.mean()
np.testing.assert_allclose(intercepts1, intercepts2)
# Note: other solvers do not have any internal state and are supposed to yield the
# same result after one iteration given the current coefficients.
@pytest.mark.parametrize("fit_intercept", [False, True])
@pytest.mark.parametrize(
"multi_class", ["ovr", "multinomial"] if not sklearn_check_version("1.8") else [None]
)
@pytest.mark.parametrize("weighted", [False, True])
@pytest.mark.allow_sklearn_fallback
@pytest.mark.skipif(
not check_preview_is_enabled(), reason="Functionality in preview mode"
)
def test_warm_start_binary(fit_intercept, multi_class, weighted):
from sklearnex.linear_model import LogisticRegression
X, y = make_classification(
random_state=123,
n_classes=2,
n_clusters_per_class=1,
n_features=2,
n_redundant=0,
class_sep=0.5,
)
params = {"random_state": 123, "solver": "newton-cg", "fit_intercept": fit_intercept}
if not sklearn_check_version("1.8"):
params["multi_class"] = multi_class
with warnings.catch_warnings():
warnings.simplefilter("ignore")
model1 = LogisticRegression(**params, max_iter=2)
model2 = LogisticRegression(**params, max_iter=1, warm_start=True)
# fit model2 twice using the same data to get 2 iterations (with warm_start)
for est in (model1, model2, model2):
est.fit(
X,
y,
np.ones(X.shape[0]) if weighted else None,
)
np.testing.assert_allclose(model1.coef_, model2.coef_)
if fit_intercept:
np.testing.assert_allclose(model1.intercept_, model2.intercept_)
@pytest.mark.parametrize("fit_intercept", [False, True])
@pytest.mark.parametrize(
"multi_class", ["ovr", "multinomial"] if not sklearn_check_version("1.8") else [None]
)
@pytest.mark.parametrize("weighted", [False, True])
@pytest.mark.allow_sklearn_fallback
@pytest.mark.skipif(not check_preview_is_enabled(), reason="Functionality in preview")
def test_warm_start_multinomial(fit_intercept, multi_class, weighted):
from sklearnex.linear_model import LogisticRegression
X, y = make_classification(
random_state=123,
n_classes=3,
n_clusters_per_class=1,
n_features=2,
n_redundant=0,
class_sep=0.5,
)
params = {"random_state": 123, "solver": "newton-cg", "fit_intercept": fit_intercept}
if not sklearn_check_version("1.8"):
params["multi_class"] = multi_class
with warnings.catch_warnings():
warnings.simplefilter("ignore")
model1 = LogisticRegression(**params, max_iter=2)
model2 = LogisticRegression(**params, max_iter=1, warm_start=True)
# fit model2 twice using the same data to get 2 iterations (with warm_start)
for est in (model1, model2, model2):
est.fit(
X,
y,
np.ones(X.shape[0]) if weighted else None,
)
np.testing.assert_allclose(model1.coef_, model2.coef_)
if fit_intercept:
# Note: softmax function is invariable to shifting by a constant
intercepts1 = model1.intercept_ - model1.intercept_.mean()
intercepts2 = model2.intercept_ - model2.intercept_.mean()
np.testing.assert_allclose(intercepts1, intercepts2)
# This is a bit different from the others - it just aims to test that it
# is processing the regularization correctly under all circumstances, and
# that it is not multiplying or dividing the coefficients by two when it
# shouldn't do it.
# It has some overlap with the tests at the beginning, but it is only tested
# with oneDAL>=2025.8. After that version has been released and CIs updated
# to use it, the earlier tests 'test_logistic_regression_is_correct' and
# 'test_multinomial_logistic_regression_is_correct' can be removed.
@pytest.mark.skipif(
not daal_check_version((2025, "P", 800)), reason="Bugs fixed in later oneDAL releases"
)
@pytest.mark.parametrize(
"multi_class", ["auto", "multinomial"] if not sklearn_check_version("1.8") else [None]
)
@pytest.mark.parametrize("C", [1, 0.2, 20.0])
@pytest.mark.parametrize("solver", ["lbfgs", "newton-cg"])
@pytest.mark.parametrize("n_classes", [2, 3])
@pytest.mark.allow_sklearn_fallback
def test_custom_solvers_are_correct(multi_class, C, solver, n_classes):
if solver == "newton-cg" and not check_preview_is_enabled():
pytest.skip("Functionality in preview mode")
from sklearnex.linear_model import LogisticRegression
X, y = make_classification(
random_state=123,
n_classes=n_classes,
n_clusters_per_class=1,
n_features=2,
n_redundant=0,
n_samples=50,
class_sep=0.25,
)
params = {"C": C}
if not sklearn_check_version:
params["multi_class"] = multi_class
with warnings.catch_warnings():
warnings.simplefilter("ignore")
model_sklearn = _sklearn_LogisticRegression(**params).fit(X, y)
params["solver"] = solver
model_sklearnex = LogisticRegression(**params, max_iter=1_000, tol=1e-8).fit(X, y)
model_sklearnex_refitted = (
LogisticRegression(**params, max_iter=1_000, tol=1e-8, warm_start=True)
.fit(X, y)
.fit(X, y)
)
np.testing.assert_allclose(
model_sklearnex.coef_, model_sklearn.coef_, rtol=1e-3, atol=5e-3
)
if n_classes == 2:
np.testing.assert_allclose(
model_sklearnex.intercept_, model_sklearn.intercept_, atol=1e-3
)
else:
np.testing.assert_allclose(
model_sklearnex.intercept_ - model_sklearnex.intercept_.mean(),
model_sklearn.intercept_ - model_sklearn.intercept_.mean(),
atol=1e-3,
)
np.testing.assert_allclose(
model_sklearnex_refitted.coef_, model_sklearn.coef_, rtol=1e-3, atol=5e-3
)
if n_classes == 2:
np.testing.assert_allclose(
model_sklearnex_refitted.intercept_, model_sklearn.intercept_, atol=1e-3
)
else:
np.testing.assert_allclose(
model_sklearnex_refitted.intercept_
- model_sklearnex_refitted.intercept_.mean(),
model_sklearn.intercept_ - model_sklearn.intercept_.mean(),
atol=1e-3,
)
np.testing.assert_allclose(
model_sklearnex.predict_proba(X),
model_sklearn.predict_proba(X),
rtol=1e-3,
atol=1e-3,
)
np.testing.assert_allclose(
model_sklearnex_refitted.predict_proba(X),
model_sklearn.predict_proba(X),
rtol=1e-3,
atol=1e-3,
)
@pytest.mark.parametrize(
"dataframe,queue", get_dataframes_and_queues(device_filter_="gpu")
)
def test_gpu_logreg_prediction_shapes(dataframe, queue):
if not queue or not queue.sycl_device.is_gpu:
pytest.skip("Test for GPU-only code branch")
from sklearnex.linear_model import LogisticRegression
X, y = make_classification(random_state=123)
X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
y = _convert_to_dataframe(y, sycl_queue=queue, target_df=dataframe)
model = LogisticRegression(solver="newton-cg").fit(X, y)
pred = model.predict(X)
pred_proba = model.predict_proba(X)
pred_log_proba = model.predict_log_proba(X)
pred_decision_function = model.decision_function(X)
np.testing.assert_array_equal(pred.shape, (X.shape[0],))
np.testing.assert_array_equal(pred_proba.shape, (X.shape[0], 2))
np.testing.assert_array_equal(pred_log_proba.shape, (X.shape[0], 2))
np.testing.assert_array_equal(pred_decision_function.shape, (X.shape[0],))
@pytest.mark.skipif(
not daal_check_version((2025, "P", 900)), reason="Bugs fixed in later oneDAL releases"
)
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
def test_log_proba_doesnt_return_inf(dataframe, queue):
if (not queue or queue.sycl_device.is_cpu) and not check_preview_is_enabled():
pytest.skip("Functionality in preview mode")
from sklearnex.linear_model import LogisticRegression
X, y = make_classification(random_state=123)
X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
y = _convert_to_dataframe(y, sycl_queue=queue, target_df=dataframe)
model = LogisticRegression(solver="newton-cg").fit(X, y)
X_problem = 1e10 * _as_numpy(model.coef_).reshape((1, -1))
X_problem = np.vstack([X_problem, -X_problem])
pred_log_proba = model.predict_log_proba(X_problem)
pred_log_proba = _as_numpy(pred_log_proba)
assert not np.any(np.isinf(pred_log_proba))
@pytest.mark.skipif(
not sklearn_check_version("1.5"), reason="Array API requires sklearn>=1.5"
)
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
@pytest.mark.parametrize("y_type", ["numeric", "string"])
@pytest.mark.allow_sklearn_fallback
def test_array_api_logreg(dataframe, queue, y_type):
"""Test LogisticRegression with Array API dispatch enabled.
Tests cover:
- GPU: dpnp/dpctl arrays with newton-cg solver
- CPU: dpnp/dpctl and array_api_strict arrays with lbfgs solver
- Both numeric and string label targets
Validates that:
1. No fallback to sklearn on GPU
2. Model attributes (coef_, intercept_, n_iter_) have correct types
3. All prediction methods return correct types and shapes
4. score returns a scalar value
"""
from sklearnex.linear_model import LogisticRegression
is_gpu = queue is not None and queue.sycl_device.is_gpu
# Skip conditions based on device and array type
if is_gpu:
solver = "newton-cg"
else:
if not sklearn_check_version("1.9"):
pytest.skip("Array API with lbfgs on CPU requires sklearn>=1.9")
solver = "lbfgs"
# Generate test data
X, y = make_classification(n_samples=100, n_features=20, n_classes=2, random_state=42)
X_arr = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
if y_type == "numeric":
y_arr = _convert_to_dataframe(y, sycl_queue=queue, target_df=dataframe)
else:
y_arr = np.take(["class_a", "class_b"], y)
# Fit model with array API dispatch enabled
with config_context(array_api_dispatch=True):
model = LogisticRegression(solver=solver).fit(X_arr, y_arr)
# 1. Check no fallback to sklearn on GPU
if is_gpu:
assert hasattr(
model, "_onedal_estimator"
), "Model should not fall back to sklearn on GPU with array API"
# # 2. Check attributes have correct types (same as X)
def _check_predict_type_and_shape(
pred, expected_type, expected_shape, device_check=False
):
assert (
type(pred).__name__ == expected_type.__name__
), f"predict type {type(pred)} should match expected type {expected_type}"
assert (
pred.shape == expected_shape
), f"predict shape {pred.shape} should be {expected_shape}"
if device_check:
assert getattr(pred, "device", None) == getattr(
X_arr, "device", None
), "predict should be on same device as X_arr"
device_check = dataframe != "pandas"
coef_expected_type = type(X_arr) if dataframe != "pandas" else np.ndarray
_check_predict_type_and_shape(
model.coef_, coef_expected_type, (1, X.shape[1]), device_check
)
_check_predict_type_and_shape(
model.intercept_, coef_expected_type, (1,), device_check
)
# 3. Check predict returns correct type and shape
with config_context(array_api_dispatch=True):
pred = model.predict(X_arr)
if y_type == "numeric" and dataframe != "pandas":
pred_expected_type = type(y_arr)
else:
pred_expected_type = np.ndarray
_check_predict_type_and_shape(
pred, pred_expected_type, (X.shape[0],), device_check and (y_type == "numeric")
)
# 4. Check predict_proba returns correct type (same as X) and shape
with config_context(array_api_dispatch=True):
pred_proba = model.predict_proba(X_arr)
proba_expected_type = type(X_arr) if dataframe != "pandas" else np.ndarray
_check_predict_type_and_shape(
pred_proba, proba_expected_type, (X.shape[0], 2), device_check
)
# 5. Check predict_log_proba returns correct type (same as X) and shape
with config_context(array_api_dispatch=True):
pred_log_proba = model.predict_log_proba(X_arr)
_check_predict_type_and_shape(
pred_log_proba, proba_expected_type, (X.shape[0], 2), device_check
)
# 6. Check decision_function returns correct type (same as X) and shape
if dataframe != "pandas":
# If pandas dataframe provided as input stock sklearn throws an error because of incorrect logic
# of device comparison. When this issue is fixed, this condition should be removed
with config_context(array_api_dispatch=True):
dec_func = model.decision_function(X_arr)
_check_predict_type_and_shape(
dec_func, proba_expected_type, (X.shape[0],), device_check
)
# 7. Check score returns a scalar
with config_context(array_api_dispatch=True):
score = model.score(X_arr, y_arr)
assert isinstance(
score, (float, np.floating, np.number)
), f"score should return a scalar, got type {type(score)}"