scikit-learn-intelex 2024.4.0__py310-none-manylinux1_x86_64.whl → 2024.6.0__py310-none-manylinux1_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of scikit-learn-intelex might be problematic. Click here for more details.
- {scikit_learn_intelex-2024.4.0.dist-info → scikit_learn_intelex-2024.6.0.dist-info}/METADATA +2 -2
- {scikit_learn_intelex-2024.4.0.dist-info → scikit_learn_intelex-2024.6.0.dist-info}/RECORD +43 -36
- sklearnex/_device_offload.py +8 -1
- sklearnex/basic_statistics/tests/test_incremental_basic_statistics.py +2 -4
- sklearnex/cluster/dbscan.py +3 -0
- sklearnex/cluster/tests/test_dbscan.py +8 -6
- sklearnex/conftest.py +11 -1
- sklearnex/covariance/incremental_covariance.py +217 -30
- sklearnex/covariance/tests/test_incremental_covariance.py +54 -17
- sklearnex/decomposition/pca.py +68 -13
- sklearnex/decomposition/tests/test_pca.py +6 -4
- sklearnex/dispatcher.py +46 -1
- sklearnex/ensemble/_forest.py +114 -22
- sklearnex/ensemble/tests/test_forest.py +13 -3
- sklearnex/glob/dispatcher.py +16 -2
- sklearnex/linear_model/__init__.py +5 -3
- sklearnex/linear_model/incremental_linear.py +464 -0
- sklearnex/linear_model/linear.py +27 -9
- sklearnex/linear_model/logistic_regression.py +13 -15
- sklearnex/linear_model/tests/test_incremental_linear.py +200 -0
- sklearnex/linear_model/tests/test_linear.py +2 -2
- sklearnex/neighbors/knn_regression.py +24 -0
- sklearnex/neighbors/tests/test_neighbors.py +2 -2
- sklearnex/preview/__init__.py +1 -1
- sklearnex/preview/decomposition/__init__.py +19 -0
- sklearnex/preview/decomposition/incremental_pca.py +228 -0
- sklearnex/preview/decomposition/tests/test_incremental_pca.py +266 -0
- sklearnex/svm/_common.py +165 -20
- sklearnex/svm/nusvc.py +40 -4
- sklearnex/svm/nusvr.py +31 -2
- sklearnex/svm/svc.py +40 -4
- sklearnex/svm/svr.py +31 -2
- sklearnex/tests/_utils.py +70 -29
- sklearnex/tests/test_common.py +54 -0
- sklearnex/tests/test_memory_usage.py +195 -132
- sklearnex/tests/test_n_jobs_support.py +4 -0
- sklearnex/tests/test_patching.py +22 -10
- sklearnex/tests/test_run_to_run_stability.py +283 -0
- sklearnex/utils/_namespace.py +1 -1
- sklearnex/utils/tests/test_finite.py +89 -0
- sklearnex/tests/test_run_to_run_stability_tests.py +0 -428
- {scikit_learn_intelex-2024.4.0.dist-info → scikit_learn_intelex-2024.6.0.dist-info}/LICENSE.txt +0 -0
- {scikit_learn_intelex-2024.4.0.dist-info → scikit_learn_intelex-2024.6.0.dist-info}/WHEEL +0 -0
- {scikit_learn_intelex-2024.4.0.dist-info → scikit_learn_intelex-2024.6.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
# ===============================================================================
|
|
2
|
+
# Copyright 2020 Intel Corporation
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
# ===============================================================================
|
|
16
|
+
|
|
17
|
+
import random
|
|
18
|
+
from collections.abc import Iterable
|
|
19
|
+
from functools import partial
|
|
20
|
+
from numbers import Number
|
|
21
|
+
|
|
22
|
+
import numpy as np
|
|
23
|
+
import pytest
|
|
24
|
+
from _utils import (
|
|
25
|
+
PATCHED_MODELS,
|
|
26
|
+
SPECIAL_INSTANCES,
|
|
27
|
+
_sklearn_clone_dict,
|
|
28
|
+
gen_dataset,
|
|
29
|
+
gen_models_info,
|
|
30
|
+
)
|
|
31
|
+
from numpy.testing import assert_allclose
|
|
32
|
+
from scipy import sparse
|
|
33
|
+
from sklearn.datasets import (
|
|
34
|
+
load_breast_cancer,
|
|
35
|
+
load_diabetes,
|
|
36
|
+
load_iris,
|
|
37
|
+
make_classification,
|
|
38
|
+
make_regression,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
import daal4py as d4p
|
|
42
|
+
from onedal.tests.utils._dataframes_support import _as_numpy, get_dataframes_and_queues
|
|
43
|
+
from sklearnex.cluster import DBSCAN, KMeans
|
|
44
|
+
from sklearnex.decomposition import PCA
|
|
45
|
+
from sklearnex.metrics import pairwise_distances, roc_auc_score
|
|
46
|
+
from sklearnex.model_selection import train_test_split
|
|
47
|
+
from sklearnex.neighbors import (
|
|
48
|
+
KNeighborsClassifier,
|
|
49
|
+
KNeighborsRegressor,
|
|
50
|
+
NearestNeighbors,
|
|
51
|
+
)
|
|
52
|
+
from sklearnex.svm import SVC
|
|
53
|
+
|
|
54
|
+
# to reproduce errors even in CI
|
|
55
|
+
d4p.daalinit(nthreads=100)
|
|
56
|
+
|
|
57
|
+
_dataset_dict = {
|
|
58
|
+
"classification": [
|
|
59
|
+
partial(load_iris, return_X_y=True),
|
|
60
|
+
partial(load_breast_cancer, return_X_y=True),
|
|
61
|
+
],
|
|
62
|
+
"regression": [
|
|
63
|
+
partial(load_diabetes, return_X_y=True),
|
|
64
|
+
partial(
|
|
65
|
+
make_regression, n_samples=500, n_features=10, noise=64.0, random_state=42
|
|
66
|
+
),
|
|
67
|
+
],
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def eval_method(X, y, est, method):
|
|
72
|
+
res = []
|
|
73
|
+
est.fit(X, y)
|
|
74
|
+
|
|
75
|
+
if method:
|
|
76
|
+
if method != "score":
|
|
77
|
+
res = getattr(est, method)(X)
|
|
78
|
+
else:
|
|
79
|
+
res = est.score(X, y)
|
|
80
|
+
|
|
81
|
+
if not isinstance(res, Iterable):
|
|
82
|
+
res = [res]
|
|
83
|
+
|
|
84
|
+
# if estimator follows sklearn design rules, then set attributes should have a
|
|
85
|
+
# trailing underscore
|
|
86
|
+
attributes = [
|
|
87
|
+
i
|
|
88
|
+
for i in dir(est)
|
|
89
|
+
if hasattr(est, i) and not i.startswith("_") and i.endswith("_")
|
|
90
|
+
]
|
|
91
|
+
results = [getattr(est, i) for i in attributes] + [_as_numpy(i) for i in res]
|
|
92
|
+
attributes += [method for i in res]
|
|
93
|
+
return results, attributes
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _run_test(estimator, method, datasets):
|
|
97
|
+
|
|
98
|
+
for X, y in datasets:
|
|
99
|
+
baseline, attributes = eval_method(X, y, estimator, method)
|
|
100
|
+
|
|
101
|
+
for i in range(10):
|
|
102
|
+
res, _ = eval_method(X, y, estimator, method)
|
|
103
|
+
|
|
104
|
+
for r, b, n in zip(res, baseline, attributes):
|
|
105
|
+
if (
|
|
106
|
+
isinstance(b, Number)
|
|
107
|
+
or hasattr(b, "__array__")
|
|
108
|
+
or hasattr(b, "__array_namespace__")
|
|
109
|
+
or hasattr(b, "__sycl_usm_ndarray__")
|
|
110
|
+
):
|
|
111
|
+
assert_allclose(
|
|
112
|
+
r, b, rtol=0.0, atol=0.0, err_msg=str(n + " is incorrect")
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
SPARSE_INSTANCES = _sklearn_clone_dict(
|
|
117
|
+
{
|
|
118
|
+
str(i): i
|
|
119
|
+
for i in [
|
|
120
|
+
SVC(),
|
|
121
|
+
KMeans(),
|
|
122
|
+
KMeans(init="random"),
|
|
123
|
+
]
|
|
124
|
+
}
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
STABILITY_INSTANCES = _sklearn_clone_dict(
|
|
128
|
+
{
|
|
129
|
+
str(i): i
|
|
130
|
+
for i in [
|
|
131
|
+
KNeighborsClassifier(algorithm="brute", weights="distance"),
|
|
132
|
+
KNeighborsClassifier(algorithm="kd_tree", weights="distance"),
|
|
133
|
+
KNeighborsClassifier(algorithm="kd_tree"),
|
|
134
|
+
KNeighborsRegressor(algorithm="brute", weights="distance"),
|
|
135
|
+
KNeighborsRegressor(algorithm="kd_tree", weights="distance"),
|
|
136
|
+
KNeighborsRegressor(algorithm="kd_tree"),
|
|
137
|
+
NearestNeighbors(algorithm="kd_tree"),
|
|
138
|
+
DBSCAN(algorithm="brute"),
|
|
139
|
+
PCA(n_components=0.5, svd_solver="covariance_eigh"),
|
|
140
|
+
KMeans(init="random"),
|
|
141
|
+
]
|
|
142
|
+
}
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@pytest.mark.parametrize("dataframe, queue", get_dataframes_and_queues("numpy"))
|
|
147
|
+
@pytest.mark.parametrize("estimator, method", gen_models_info(PATCHED_MODELS))
|
|
148
|
+
def test_standard_estimator_stability(estimator, method, dataframe, queue):
|
|
149
|
+
if estimator in ["LogisticRegression", "TSNE"]:
|
|
150
|
+
pytest.skip(f"stability not guaranteed for {estimator}")
|
|
151
|
+
if estimator in ["KMeans", "PCA"] and method == "score" and queue == None:
|
|
152
|
+
pytest.skip(f"variation observed in {estimator}.score")
|
|
153
|
+
|
|
154
|
+
est = PATCHED_MODELS[estimator]()
|
|
155
|
+
|
|
156
|
+
if method and not hasattr(est, method):
|
|
157
|
+
pytest.skip(f"sklearn available_if prevents testing {estimator}.{method}")
|
|
158
|
+
|
|
159
|
+
params = est.get_params().copy()
|
|
160
|
+
if "random_state" in params:
|
|
161
|
+
params["random_state"] = 0
|
|
162
|
+
est.set_params(**params)
|
|
163
|
+
|
|
164
|
+
datasets = gen_dataset(est, datasets=_dataset_dict, queue=queue, target_df=dataframe)
|
|
165
|
+
_run_test(est, method, datasets)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
@pytest.mark.allow_sklearn_fallback
|
|
169
|
+
@pytest.mark.parametrize("dataframe, queue", get_dataframes_and_queues("numpy"))
|
|
170
|
+
@pytest.mark.parametrize("estimator, method", gen_models_info(SPECIAL_INSTANCES))
|
|
171
|
+
def test_special_estimator_stability(estimator, method, dataframe, queue):
|
|
172
|
+
if queue is None and estimator in ["LogisticRegression(solver='newton-cg')"]:
|
|
173
|
+
pytest.skip(f"stability not guaranteed for {estimator}")
|
|
174
|
+
if "KMeans" in estimator and method == "score" and queue == None:
|
|
175
|
+
pytest.skip(f"variation observed in KMeans.score")
|
|
176
|
+
|
|
177
|
+
est = SPECIAL_INSTANCES[estimator]
|
|
178
|
+
|
|
179
|
+
if method and not hasattr(est, method):
|
|
180
|
+
pytest.skip(f"sklearn available_if prevents testing {estimator}.{method}")
|
|
181
|
+
|
|
182
|
+
params = est.get_params().copy()
|
|
183
|
+
if "random_state" in params:
|
|
184
|
+
params["random_state"] = 0
|
|
185
|
+
est.set_params(**params)
|
|
186
|
+
|
|
187
|
+
datasets = gen_dataset(est, datasets=_dataset_dict, queue=queue, target_df=dataframe)
|
|
188
|
+
_run_test(est, method, datasets)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
@pytest.mark.parametrize("dataframe, queue", get_dataframes_and_queues("numpy"))
|
|
192
|
+
@pytest.mark.parametrize("estimator, method", gen_models_info(SPARSE_INSTANCES))
|
|
193
|
+
def test_sparse_estimator_stability(estimator, method, dataframe, queue):
|
|
194
|
+
if "KMeans" in estimator and method == "score" and queue == None:
|
|
195
|
+
pytest.skip(f"variation observed in KMeans.score")
|
|
196
|
+
|
|
197
|
+
est = SPARSE_INSTANCES[estimator]
|
|
198
|
+
|
|
199
|
+
if method and not hasattr(est, method):
|
|
200
|
+
pytest.skip(f"sklearn available_if prevents testing {estimator}.{method}")
|
|
201
|
+
|
|
202
|
+
params = est.get_params().copy()
|
|
203
|
+
if "random_state" in params:
|
|
204
|
+
params["random_state"] = 0
|
|
205
|
+
est.set_params(**params)
|
|
206
|
+
|
|
207
|
+
datasets = gen_dataset(
|
|
208
|
+
est, sparse=True, datasets=_dataset_dict, queue=queue, target_df=dataframe
|
|
209
|
+
)
|
|
210
|
+
_run_test(est, method, datasets)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
@pytest.mark.parametrize("dataframe, queue", get_dataframes_and_queues("numpy"))
|
|
214
|
+
@pytest.mark.parametrize("estimator, method", gen_models_info(STABILITY_INSTANCES))
|
|
215
|
+
def test_other_estimator_stability(estimator, method, dataframe, queue):
|
|
216
|
+
if "KMeans" in estimator and method == "score" and queue == None:
|
|
217
|
+
pytest.skip(f"variation observed in KMeans.score")
|
|
218
|
+
|
|
219
|
+
est = STABILITY_INSTANCES[estimator]
|
|
220
|
+
|
|
221
|
+
if method and not hasattr(est, method):
|
|
222
|
+
pytest.skip(f"sklearn available_if prevents testing {estimator}.{method}")
|
|
223
|
+
|
|
224
|
+
params = est.get_params().copy()
|
|
225
|
+
if "random_state" in params:
|
|
226
|
+
params["random_state"] = 0
|
|
227
|
+
est.set_params(**params)
|
|
228
|
+
|
|
229
|
+
datasets = gen_dataset(est, datasets=_dataset_dict, queue=queue, target_df=dataframe)
|
|
230
|
+
_run_test(est, method, datasets)
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
@pytest.mark.parametrize("features", range(5, 10))
|
|
234
|
+
def test_train_test_split(features):
|
|
235
|
+
X, y = make_classification(
|
|
236
|
+
n_samples=4000,
|
|
237
|
+
n_features=features,
|
|
238
|
+
n_informative=features,
|
|
239
|
+
n_redundant=0,
|
|
240
|
+
n_clusters_per_class=8,
|
|
241
|
+
random_state=0,
|
|
242
|
+
)
|
|
243
|
+
(
|
|
244
|
+
baseline_X_train,
|
|
245
|
+
baseline_X_test,
|
|
246
|
+
baseline_y_train,
|
|
247
|
+
baseline_y_test,
|
|
248
|
+
) = train_test_split(X, y, test_size=0.33, random_state=0)
|
|
249
|
+
baseline = [baseline_X_train, baseline_X_test, baseline_y_train, baseline_y_test]
|
|
250
|
+
for _ in range(10):
|
|
251
|
+
X_train, X_test, y_train, y_test = train_test_split(
|
|
252
|
+
X, y, test_size=0.33, random_state=0
|
|
253
|
+
)
|
|
254
|
+
res = [X_train, X_test, y_train, y_test]
|
|
255
|
+
for a, b in zip(res, baseline):
|
|
256
|
+
np.testing.assert_allclose(
|
|
257
|
+
a, b, rtol=0.0, atol=0.0, err_msg=str("train_test_split is incorrect")
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
@pytest.mark.parametrize("metric", ["cosine", "correlation"])
|
|
262
|
+
def test_pairwise_distances(metric):
|
|
263
|
+
X = np.random.rand(1000)
|
|
264
|
+
X = np.array(X, dtype=np.float64)
|
|
265
|
+
baseline = pairwise_distances(X.reshape(1, -1), metric=metric)
|
|
266
|
+
for _ in range(5):
|
|
267
|
+
res = pairwise_distances(X.reshape(1, -1), metric=metric)
|
|
268
|
+
for a, b in zip(res, baseline):
|
|
269
|
+
np.testing.assert_allclose(
|
|
270
|
+
a, b, rtol=0.0, atol=0.0, err_msg=str("pairwise_distances is incorrect")
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
@pytest.mark.parametrize("array_size", [100, 1000, 10000])
|
|
275
|
+
def test_roc_auc(array_size):
|
|
276
|
+
a = [random.randint(0, 1) for i in range(array_size)]
|
|
277
|
+
b = [random.randint(0, 1) for i in range(array_size)]
|
|
278
|
+
baseline = roc_auc_score(a, b)
|
|
279
|
+
for _ in range(5):
|
|
280
|
+
res = roc_auc_score(a, b)
|
|
281
|
+
np.testing.assert_allclose(
|
|
282
|
+
baseline, res, rtol=0.0, atol=0.0, err_msg=str("roc_auc is incorrect")
|
|
283
|
+
)
|
sklearnex/utils/_namespace.py
CHANGED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# ==============================================================================
|
|
2
|
+
# Copyright 2024 Intel Corporation
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
# ==============================================================================
|
|
16
|
+
|
|
17
|
+
import time
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
import numpy.random as rand
|
|
21
|
+
import pytest
|
|
22
|
+
from numpy.testing import assert_raises
|
|
23
|
+
|
|
24
|
+
from sklearnex.utils import _assert_all_finite
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
|
|
28
|
+
@pytest.mark.parametrize(
|
|
29
|
+
"shape",
|
|
30
|
+
[
|
|
31
|
+
[16, 2048],
|
|
32
|
+
[
|
|
33
|
+
2**16 + 3,
|
|
34
|
+
],
|
|
35
|
+
[1000, 1000],
|
|
36
|
+
],
|
|
37
|
+
)
|
|
38
|
+
@pytest.mark.parametrize("allow_nan", [False, True])
|
|
39
|
+
def test_sum_infinite_actually_finite(dtype, shape, allow_nan):
|
|
40
|
+
X = np.array(shape, dtype=dtype)
|
|
41
|
+
X.fill(np.finfo(dtype).max)
|
|
42
|
+
_assert_all_finite(X, allow_nan=allow_nan)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
|
|
46
|
+
@pytest.mark.parametrize(
|
|
47
|
+
"shape",
|
|
48
|
+
[
|
|
49
|
+
[16, 2048],
|
|
50
|
+
[
|
|
51
|
+
2**16 + 3,
|
|
52
|
+
],
|
|
53
|
+
[1000, 1000],
|
|
54
|
+
],
|
|
55
|
+
)
|
|
56
|
+
@pytest.mark.parametrize("allow_nan", [False, True])
|
|
57
|
+
@pytest.mark.parametrize("check", ["inf", "NaN", None])
|
|
58
|
+
@pytest.mark.parametrize("seed", [0, int(time.time())])
|
|
59
|
+
def test_assert_finite_random_location(dtype, shape, allow_nan, check, seed):
|
|
60
|
+
rand.seed(seed)
|
|
61
|
+
X = rand.uniform(high=np.finfo(dtype).max, size=shape).astype(dtype)
|
|
62
|
+
|
|
63
|
+
if check:
|
|
64
|
+
loc = rand.randint(0, X.size - 1)
|
|
65
|
+
X.reshape((-1,))[loc] = float(check)
|
|
66
|
+
|
|
67
|
+
if check is None or (allow_nan and check == "NaN"):
|
|
68
|
+
_assert_all_finite(X, allow_nan=allow_nan)
|
|
69
|
+
else:
|
|
70
|
+
assert_raises(ValueError, _assert_all_finite, X, allow_nan=allow_nan)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
|
|
74
|
+
@pytest.mark.parametrize("allow_nan", [False, True])
|
|
75
|
+
@pytest.mark.parametrize("check", ["inf", "NaN", None])
|
|
76
|
+
@pytest.mark.parametrize("seed", [0, int(time.time())])
|
|
77
|
+
def test_assert_finite_random_shape_and_location(dtype, allow_nan, check, seed):
|
|
78
|
+
lb, ub = 32768, 1048576 # lb is a patching condition, ub 2^20
|
|
79
|
+
rand.seed(seed)
|
|
80
|
+
X = rand.uniform(high=np.finfo(dtype).max, size=rand.randint(lb, ub)).astype(dtype)
|
|
81
|
+
|
|
82
|
+
if check:
|
|
83
|
+
loc = rand.randint(0, X.size - 1)
|
|
84
|
+
X[loc] = float(check)
|
|
85
|
+
|
|
86
|
+
if check is None or (allow_nan and check == "NaN"):
|
|
87
|
+
_assert_all_finite(X, allow_nan=allow_nan)
|
|
88
|
+
else:
|
|
89
|
+
assert_raises(ValueError, _assert_all_finite, X, allow_nan=allow_nan)
|