scikit-learn-intelex 2024.5.0__py312-none-manylinux1_x86_64.whl → 2024.6.0__py312-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.
Files changed (35) hide show
  1. {scikit_learn_intelex-2024.5.0.dist-info → scikit_learn_intelex-2024.6.0.dist-info}/METADATA +2 -2
  2. {scikit_learn_intelex-2024.5.0.dist-info → scikit_learn_intelex-2024.6.0.dist-info}/RECORD +34 -30
  3. sklearnex/cluster/dbscan.py +3 -0
  4. sklearnex/cluster/tests/test_dbscan.py +8 -6
  5. sklearnex/conftest.py +11 -1
  6. sklearnex/decomposition/tests/test_pca.py +4 -2
  7. sklearnex/dispatcher.py +15 -1
  8. sklearnex/ensemble/_forest.py +114 -23
  9. sklearnex/ensemble/tests/test_forest.py +13 -3
  10. sklearnex/glob/dispatcher.py +16 -2
  11. sklearnex/linear_model/incremental_linear.py +102 -25
  12. sklearnex/linear_model/linear.py +25 -7
  13. sklearnex/linear_model/logistic_regression.py +13 -15
  14. sklearnex/linear_model/tests/test_incremental_linear.py +10 -10
  15. sklearnex/linear_model/tests/test_linear.py +2 -2
  16. sklearnex/neighbors/knn_regression.py +24 -0
  17. sklearnex/preview/__init__.py +1 -1
  18. sklearnex/preview/decomposition/__init__.py +19 -0
  19. sklearnex/preview/decomposition/incremental_pca.py +228 -0
  20. sklearnex/preview/decomposition/tests/test_incremental_pca.py +266 -0
  21. sklearnex/svm/_common.py +165 -20
  22. sklearnex/svm/nusvc.py +40 -4
  23. sklearnex/svm/nusvr.py +31 -2
  24. sklearnex/svm/svc.py +40 -4
  25. sklearnex/svm/svr.py +31 -2
  26. sklearnex/tests/_utils.py +49 -17
  27. sklearnex/tests/test_common.py +54 -0
  28. sklearnex/tests/test_memory_usage.py +185 -126
  29. sklearnex/tests/test_patching.py +5 -12
  30. sklearnex/tests/test_run_to_run_stability.py +283 -0
  31. sklearnex/utils/_namespace.py +1 -1
  32. sklearnex/tests/test_run_to_run_stability_tests.py +0 -428
  33. {scikit_learn_intelex-2024.5.0.dist-info → scikit_learn_intelex-2024.6.0.dist-info}/LICENSE.txt +0 -0
  34. {scikit_learn_intelex-2024.5.0.dist-info → scikit_learn_intelex-2024.6.0.dist-info}/WHEEL +0 -0
  35. {scikit_learn_intelex-2024.5.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
+ )
@@ -94,4 +94,4 @@ def get_namespace(*arrays):
94
94
  elif sklearn_check_version("1.2"):
95
95
  return sklearn_get_namespace(*arrays)
96
96
  else:
97
- return np, True
97
+ return np, False