scikit-learn-intelex 2024.0.1__py311-none-manylinux1_x86_64.whl → 2024.4.0__py311-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.

Files changed (89) hide show
  1. {scikit_learn_intelex-2024.0.1.dist-info → scikit_learn_intelex-2024.4.0.dist-info}/METADATA +2 -2
  2. scikit_learn_intelex-2024.4.0.dist-info/RECORD +101 -0
  3. sklearnex/__init__.py +11 -7
  4. sklearnex/__main__.py +0 -1
  5. sklearnex/_device_offload.py +31 -4
  6. sklearnex/_utils.py +15 -1
  7. sklearnex/basic_statistics/__init__.py +2 -2
  8. sklearnex/basic_statistics/incremental_basic_statistics.py +288 -0
  9. sklearnex/basic_statistics/tests/test_incremental_basic_statistics.py +386 -0
  10. sklearnex/cluster/__init__.py +0 -1
  11. sklearnex/cluster/dbscan.py +5 -2
  12. sklearnex/cluster/k_means.py +0 -1
  13. sklearnex/cluster/tests/test_dbscan.py +0 -1
  14. sklearnex/cluster/tests/test_kmeans.py +0 -1
  15. sklearnex/conftest.py +63 -0
  16. sklearnex/covariance/__init__.py +19 -0
  17. sklearnex/covariance/incremental_covariance.py +130 -0
  18. sklearnex/covariance/tests/test_incremental_covariance.py +143 -0
  19. sklearnex/decomposition/__init__.py +0 -1
  20. sklearnex/decomposition/pca.py +319 -2
  21. sklearnex/decomposition/tests/test_pca.py +34 -6
  22. sklearnex/dispatcher.py +93 -28
  23. sklearnex/ensemble/__init__.py +0 -1
  24. sklearnex/ensemble/_forest.py +93 -89
  25. sklearnex/ensemble/tests/test_forest.py +15 -20
  26. sklearnex/glob/__main__.py +0 -1
  27. sklearnex/glob/dispatcher.py +0 -1
  28. sklearnex/linear_model/__init__.py +1 -3
  29. sklearnex/linear_model/coordinate_descent.py +0 -1
  30. sklearnex/linear_model/linear.py +275 -332
  31. sklearnex/linear_model/logistic_path.py +0 -1
  32. sklearnex/linear_model/logistic_regression.py +385 -0
  33. sklearnex/linear_model/ridge.py +0 -1
  34. sklearnex/linear_model/tests/test_linear.py +47 -7
  35. sklearnex/linear_model/tests/test_logreg.py +70 -8
  36. sklearnex/manifold/__init__.py +0 -1
  37. sklearnex/manifold/t_sne.py +0 -1
  38. sklearnex/manifold/tests/test_tsne.py +0 -1
  39. sklearnex/metrics/__init__.py +0 -1
  40. sklearnex/metrics/pairwise.py +0 -1
  41. sklearnex/metrics/ranking.py +0 -1
  42. sklearnex/metrics/tests/test_metrics.py +0 -1
  43. sklearnex/model_selection/__init__.py +0 -1
  44. sklearnex/model_selection/split.py +0 -1
  45. sklearnex/model_selection/tests/test_model_selection.py +0 -1
  46. sklearnex/neighbors/__init__.py +1 -2
  47. sklearnex/neighbors/_lof.py +221 -0
  48. sklearnex/neighbors/common.py +5 -3
  49. sklearnex/neighbors/knn_classification.py +47 -133
  50. sklearnex/neighbors/knn_regression.py +20 -129
  51. sklearnex/neighbors/knn_unsupervised.py +15 -89
  52. sklearnex/neighbors/tests/test_neighbors.py +12 -17
  53. sklearnex/preview/__init__.py +1 -2
  54. sklearnex/preview/cluster/__init__.py +0 -1
  55. sklearnex/preview/cluster/k_means.py +7 -74
  56. sklearnex/preview/{decomposition → covariance}/__init__.py +19 -20
  57. sklearnex/preview/covariance/covariance.py +133 -0
  58. sklearnex/preview/covariance/tests/test_covariance.py +66 -0
  59. sklearnex/spmd/__init__.py +1 -0
  60. sklearnex/spmd/covariance/__init__.py +19 -0
  61. sklearnex/spmd/covariance/covariance.py +21 -0
  62. sklearnex/spmd/ensemble/forest.py +4 -12
  63. sklearnex/spmd/linear_model/__init__.py +2 -1
  64. sklearnex/spmd/linear_model/logistic_regression.py +21 -0
  65. sklearnex/svm/__init__.py +0 -1
  66. sklearnex/svm/_common.py +4 -7
  67. sklearnex/svm/nusvc.py +73 -49
  68. sklearnex/svm/nusvr.py +8 -52
  69. sklearnex/svm/svc.py +74 -51
  70. sklearnex/svm/svr.py +5 -49
  71. sklearnex/svm/tests/test_svm.py +0 -1
  72. sklearnex/tests/_utils.py +164 -0
  73. sklearnex/tests/test_memory_usage.py +9 -7
  74. sklearnex/tests/test_monkeypatch.py +192 -134
  75. sklearnex/tests/test_n_jobs_support.py +99 -0
  76. sklearnex/tests/test_parallel.py +6 -8
  77. sklearnex/tests/test_patching.py +338 -89
  78. sklearnex/utils/__init__.py +2 -1
  79. sklearnex/utils/_namespace.py +97 -0
  80. sklearnex/utils/validation.py +0 -1
  81. scikit_learn_intelex-2024.0.1.dist-info/RECORD +0 -90
  82. sklearnex/neighbors/lof.py +0 -437
  83. sklearnex/preview/decomposition/pca.py +0 -376
  84. sklearnex/preview/decomposition/tests/test_preview_pca.py +0 -38
  85. sklearnex/tests/_models_info.py +0 -170
  86. sklearnex/tests/utils/_launch_algorithms.py +0 -118
  87. {scikit_learn_intelex-2024.0.1.dist-info → scikit_learn_intelex-2024.4.0.dist-info}/LICENSE.txt +0 -0
  88. {scikit_learn_intelex-2024.0.1.dist-info → scikit_learn_intelex-2024.4.0.dist-info}/WHEEL +0 -0
  89. {scikit_learn_intelex-2024.0.1.dist-info → scikit_learn_intelex-2024.4.0.dist-info}/top_level.txt +0 -0
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env python
2
1
  # ===============================================================================
3
2
  # Copyright 2021 Intel Corporation
4
3
  #
@@ -15,90 +14,22 @@
15
14
  # limitations under the License.
16
15
  # ===============================================================================
17
16
 
18
- try:
19
- from packaging.version import Version
20
- except ImportError:
21
- from distutils.version import LooseVersion as Version
22
-
23
- import warnings
24
-
25
- import numpy as np
26
- from sklearn import __version__ as sklearn_version
27
- from sklearn.neighbors._ball_tree import BallTree
28
- from sklearn.neighbors._base import VALID_METRICS
29
- from sklearn.neighbors._base import NeighborsBase as sklearn_NeighborsBase
30
- from sklearn.neighbors._kd_tree import KDTree
31
17
  from sklearn.neighbors._unsupervised import NearestNeighbors as sklearn_NearestNeighbors
32
18
  from sklearn.utils.validation import _deprecate_positional_args, check_is_fitted
33
19
 
20
+ from daal4py.sklearn._n_jobs_support import control_n_jobs
34
21
  from daal4py.sklearn._utils import sklearn_check_version
35
22
  from onedal.neighbors import NearestNeighbors as onedal_NearestNeighbors
36
- from onedal.utils import _check_array, _num_features, _num_samples
37
23
 
38
24
  from .._device_offload import dispatch, wrap_output_data
39
25
  from .common import KNeighborsDispatchingBase
40
26
 
41
- if sklearn_check_version("0.22") and Version(sklearn_version) < Version("0.23"):
42
27
 
43
- class NearestNeighbors_(sklearn_NearestNeighbors):
44
- def __init__(
45
- self,
46
- n_neighbors=5,
47
- radius=1.0,
48
- algorithm="auto",
49
- leaf_size=30,
50
- metric="minkowski",
51
- p=2,
52
- metric_params=None,
53
- n_jobs=None,
54
- ):
55
- super().__init__(
56
- n_neighbors=n_neighbors,
57
- radius=radius,
58
- algorithm=algorithm,
59
- leaf_size=leaf_size,
60
- metric=metric,
61
- p=p,
62
- metric_params=metric_params,
63
- n_jobs=n_jobs,
64
- )
65
-
66
- else:
67
-
68
- class NearestNeighbors_(sklearn_NearestNeighbors):
69
- if sklearn_check_version("1.2"):
70
- _parameter_constraints: dict = {
71
- **sklearn_NearestNeighbors._parameter_constraints
72
- }
73
-
74
- @_deprecate_positional_args
75
- def __init__(
76
- self,
77
- *,
78
- n_neighbors=5,
79
- radius=1.0,
80
- algorithm="auto",
81
- leaf_size=30,
82
- metric="minkowski",
83
- p=2,
84
- metric_params=None,
85
- n_jobs=None,
86
- ):
87
- super().__init__(
88
- n_neighbors=n_neighbors,
89
- radius=radius,
90
- algorithm=algorithm,
91
- leaf_size=leaf_size,
92
- metric=metric,
93
- p=p,
94
- metric_params=metric_params,
95
- n_jobs=n_jobs,
96
- )
97
-
98
-
99
- class NearestNeighbors(NearestNeighbors_, KNeighborsDispatchingBase):
28
+ @control_n_jobs(decorated_methods=["fit", "kneighbors"])
29
+ class NearestNeighbors(sklearn_NearestNeighbors, KNeighborsDispatchingBase):
30
+ __doc__ = sklearn_NearestNeighbors.__doc__
100
31
  if sklearn_check_version("1.2"):
101
- _parameter_constraints: dict = {**NearestNeighbors_._parameter_constraints}
32
+ _parameter_constraints: dict = {**sklearn_NearestNeighbors._parameter_constraints}
102
33
 
103
34
  @_deprecate_positional_args
104
35
  def __init__(
@@ -124,7 +55,6 @@ class NearestNeighbors(NearestNeighbors_, KNeighborsDispatchingBase):
124
55
  )
125
56
 
126
57
  def fit(self, X, y=None):
127
- self._fit_validation(X, y)
128
58
  dispatch(
129
59
  self,
130
60
  "fit",
@@ -150,8 +80,8 @@ class NearestNeighbors(NearestNeighbors_, KNeighborsDispatchingBase):
150
80
  "sklearn": sklearn_NearestNeighbors.kneighbors,
151
81
  },
152
82
  X,
153
- n_neighbors,
154
- return_distance,
83
+ n_neighbors=n_neighbors,
84
+ return_distance=return_distance,
155
85
  )
156
86
 
157
87
  @wrap_output_data
@@ -165,18 +95,10 @@ class NearestNeighbors(NearestNeighbors_, KNeighborsDispatchingBase):
165
95
  or getattr(self, "_tree", 0) is None
166
96
  and self._fit_method == "kd_tree"
167
97
  ):
168
- if sklearn_check_version("0.24"):
169
- sklearn_NearestNeighbors.fit(self, self._fit_X, getattr(self, "_y", None))
170
- else:
171
- sklearn_NearestNeighbors.fit(self, self._fit_X)
172
- if sklearn_check_version("0.22"):
173
- result = sklearn_NearestNeighbors.radius_neighbors(
174
- self, X, radius, return_distance, sort_results
175
- )
176
- else:
177
- result = sklearn_NearestNeighbors.radius_neighbors(
178
- self, X, radius, return_distance
179
- )
98
+ sklearn_NearestNeighbors.fit(self, self._fit_X, getattr(self, "_y", None))
99
+ result = sklearn_NearestNeighbors.radius_neighbors(
100
+ self, X, radius, return_distance, sort_results
101
+ )
180
102
 
181
103
  return result
182
104
 
@@ -218,3 +140,7 @@ class NearestNeighbors(NearestNeighbors_, KNeighborsDispatchingBase):
218
140
  self._fit_X = self._onedal_estimator._fit_X
219
141
  self._fit_method = self._onedal_estimator._fit_method
220
142
  self._tree = self._onedal_estimator._tree
143
+
144
+ fit.__doc__ = sklearn_NearestNeighbors.__doc__
145
+ kneighbors.__doc__ = sklearn_NearestNeighbors.kneighbors.__doc__
146
+ radius_neighbors.__doc__ = sklearn_NearestNeighbors.radius_neighbors.__doc__
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env python
2
1
  # ===============================================================================
3
2
  # Copyright 2021 Intel Corporation
4
3
  #
@@ -15,7 +14,6 @@
15
14
  # limitations under the License.
16
15
  # ===============================================================================
17
16
 
18
- import numpy as np
19
17
  import pytest
20
18
  from numpy.testing import assert_allclose
21
19
 
@@ -24,12 +22,16 @@ from onedal.tests.utils._dataframes_support import (
24
22
  _convert_to_dataframe,
25
23
  get_dataframes_and_queues,
26
24
  )
25
+ from sklearnex.neighbors import (
26
+ KNeighborsClassifier,
27
+ KNeighborsRegressor,
28
+ LocalOutlierFactor,
29
+ NearestNeighbors,
30
+ )
27
31
 
28
32
 
29
33
  @pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
30
34
  def test_sklearnex_import_knn_classifier(dataframe, queue):
31
- from sklearnex.neighbors import KNeighborsClassifier
32
-
33
35
  X = _convert_to_dataframe([[0], [1], [2], [3]], sycl_queue=queue, target_df=dataframe)
34
36
  y = _convert_to_dataframe([0, 0, 1, 1], sycl_queue=queue, target_df=dataframe)
35
37
  neigh = KNeighborsClassifier(n_neighbors=3).fit(X, y)
@@ -41,8 +43,6 @@ def test_sklearnex_import_knn_classifier(dataframe, queue):
41
43
 
42
44
  @pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
43
45
  def test_sklearnex_import_knn_regression(dataframe, queue):
44
- from sklearnex.neighbors import KNeighborsRegressor
45
-
46
46
  X = _convert_to_dataframe([[0], [1], [2], [3]], sycl_queue=queue, target_df=dataframe)
47
47
  y = _convert_to_dataframe([0, 0, 1, 1], sycl_queue=queue, target_df=dataframe)
48
48
  neigh = KNeighborsRegressor(n_neighbors=2).fit(X, y)
@@ -52,18 +52,16 @@ def test_sklearnex_import_knn_regression(dataframe, queue):
52
52
  assert_allclose(pred, [0.5])
53
53
 
54
54
 
55
- # TODO:
56
- # investigate failure for `dpnp.ndarrays` and `dpctl.tensors`.
55
+ @pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
57
56
  @pytest.mark.parametrize(
58
- "dataframe,queue", get_dataframes_and_queues(dataframe_filter_="numpy")
57
+ "estimator",
58
+ [LocalOutlierFactor, NearestNeighbors],
59
59
  )
60
- def test_sklearnex_import_nn(dataframe, queue):
61
- from sklearnex.neighbors import NearestNeighbors
62
-
60
+ def test_sklearnex_kneighbors(estimator, dataframe, queue):
63
61
  X = [[0, 0, 2], [1, 0, 0], [0, 0, 1]]
64
62
  X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
65
63
  test = _convert_to_dataframe([[0, 0, 1.3]], sycl_queue=queue, target_df=dataframe)
66
- neigh = NearestNeighbors(n_neighbors=2).fit(X)
64
+ neigh = estimator(n_neighbors=2).fit(X)
67
65
  result = neigh.kneighbors(test, 2, return_distance=False)
68
66
  result = _as_numpy(result)
69
67
  assert "sklearnex" in neigh.__module__
@@ -72,14 +70,11 @@ def test_sklearnex_import_nn(dataframe, queue):
72
70
 
73
71
  @pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
74
72
  def test_sklearnex_import_lof(dataframe, queue):
75
- from sklearnex.neighbors import LocalOutlierFactor
76
-
77
73
  X = [[7, 7, 7], [1, 0, 0], [0, 0, 1], [0, 0, 1]]
78
74
  X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
79
75
  lof = LocalOutlierFactor(n_neighbors=2)
80
76
  result = lof.fit_predict(X)
81
77
  result = _as_numpy(result)
82
- assert hasattr(lof, "_knn")
78
+ assert hasattr(lof, "_onedal_estimator")
83
79
  assert "sklearnex" in lof.__module__
84
- assert "sklearnex" in lof._knn.__module__
85
80
  assert_allclose(result, [-1, 1, 1, 1])
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env python
2
1
  # ==============================================================================
3
2
  # Copyright 2023 Intel Corporation
4
3
  #
@@ -15,4 +14,4 @@
15
14
  # limitations under the License.
16
15
  # ==============================================================================
17
16
 
18
- __all__ = ["cluster", "decomposition"]
17
+ __all__ = ["cluster", "covariance"]
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env python
2
1
  # ==============================================================================
3
2
  # Copyright 2023 Intel Corporation
4
3
  #
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env python
2
1
  # ==============================================================================
3
2
  # Copyright 2023 Intel Corporation
4
3
  #
@@ -30,6 +29,7 @@ if daal_check_version((2023, "P", 200)):
30
29
  check_is_fitted,
31
30
  )
32
31
 
32
+ from daal4py.sklearn._n_jobs_support import control_n_jobs
33
33
  from daal4py.sklearn._utils import sklearn_check_version
34
34
  from onedal.cluster import KMeans as onedal_KMeans
35
35
 
@@ -37,6 +37,7 @@ if daal_check_version((2023, "P", 200)):
37
37
  from ..._utils import PatchingConditionsChain
38
38
  from ._common import BaseKMeans
39
39
 
40
+ @control_n_jobs(decorated_methods=["fit", "predict"])
40
41
  class KMeans(sklearn_KMeans, BaseKMeans):
41
42
  __doc__ = sklearn_KMeans.__doc__
42
43
  n_iter_, inertia_ = None, None
@@ -171,24 +172,6 @@ if daal_check_version((2023, "P", 200)):
171
172
  return patching_status
172
173
 
173
174
  def fit(self, X, y=None, sample_weight=None):
174
- """Compute k-means clustering.
175
-
176
- Parameters
177
- ----------
178
- X : array-like or sparse matrix, shape=(n_samples, n_features)
179
- Training instances to cluster. It must be noted that the data
180
- will be converted to C ordering, which will cause a memory
181
- copy if the given data is not C-contiguous.
182
-
183
- y : Ignored
184
- not used, present here for API consistency by convention.
185
-
186
- sample_weight : array-like, shape (n_samples,), optional
187
- The weights for each observation in X. If None, all observations
188
- are assigned equal weight (default: None)
189
-
190
- """
191
-
192
175
  if sklearn_check_version("1.0"):
193
176
  self._check_feature_names(X, reset=True)
194
177
  if sklearn_check_version("1.2"):
@@ -256,24 +239,6 @@ if daal_check_version((2023, "P", 200)):
256
239
 
257
240
  @wrap_output_data
258
241
  def predict(self, X):
259
- """Compute k-means clustering.
260
-
261
- Parameters
262
- ----------
263
- X : array-like or sparse matrix, shape=(n_samples, n_features)
264
- Training instances to cluster. It must be noted that the data
265
- will be converted to C ordering, which will cause a memory
266
- copy if the given data is not C-contiguous.
267
-
268
- y : Ignored
269
- not used, present here for API consistency by convention.
270
-
271
- sample_weight : array-like, shape (n_samples,), optional
272
- The weights for each observation in X. If None, all observations
273
- are assigned equal weight (default: None)
274
-
275
- """
276
-
277
242
  if sklearn_check_version("1.0"):
278
243
  self._check_feature_names(X, reset=True)
279
244
  if sklearn_check_version("1.2"):
@@ -316,52 +281,20 @@ if daal_check_version((2023, "P", 200)):
316
281
 
317
282
  @wrap_output_data
318
283
  def fit_transform(self, X, y=None, sample_weight=None):
319
- """Compute clustering and transform X to cluster-distance space.
320
-
321
- Equivalent to fit(X).transform(X), but more efficiently implemented.
322
-
323
- Parameters
324
- ----------
325
- X : {array-like, sparse matrix} of shape (n_samples, n_features)
326
- New data to transform.
327
-
328
- y : Ignored
329
- Not used, present here for API consistency by convention.
330
-
331
- sample_weight : array-like of shape (n_samples,), default=None
332
- The weights for each observation in X. If None, all observations
333
- are assigned equal weight.
334
-
335
- Returns
336
- -------
337
- X_new : ndarray of shape (n_samples, n_clusters)
338
- X transformed in the new space.
339
- """
340
284
  return self.fit(X, sample_weight=sample_weight)._transform(X)
341
285
 
342
286
  @wrap_output_data
343
287
  def transform(self, X):
344
- """Transform X to a cluster-distance space.
345
-
346
- In the new space, each dimension is the distance to the cluster
347
- centers. Note that even if X is sparse, the array returned by
348
- `transform` will typically be dense.
349
-
350
- Parameters
351
- ----------
352
- X : {array-like, sparse matrix} of shape (n_samples, n_features)
353
- New data to transform.
354
-
355
- Returns
356
- -------
357
- X_new : ndarray of shape (n_samples, n_clusters)
358
- X transformed in the new space.
359
- """
360
288
  check_is_fitted(self)
361
289
 
362
290
  X = self._check_test_data(X)
363
291
  return self._transform(X)
364
292
 
293
+ fit.__doc__ = sklearn_KMeans.fit.__doc__
294
+ predict.__doc__ = sklearn_KMeans.predict.__doc__
295
+ transform.__doc__ = sklearn_KMeans.transform.__doc__
296
+ fit_transform.__doc__ = sklearn_KMeans.fit_transform.__doc__
297
+
365
298
  else:
366
299
  from daal4py.sklearn.cluster import KMeans
367
300
 
@@ -1,20 +1,19 @@
1
- #!/usr/bin/env python
2
- # ===============================================================================
3
- # Copyright 2023 Intel Corporation
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- # ===============================================================================
17
-
18
- from .pca import PCA
19
-
20
- __all__ = ["PCA"]
1
+ # ===============================================================================
2
+ # Copyright 2023 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
+ from .covariance import EmpiricalCovariance
18
+
19
+ __all__ = ["EmpiricalCovariance"]
@@ -0,0 +1,133 @@
1
+ # ===============================================================================
2
+ # Copyright 2023 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 warnings
18
+
19
+ import numpy as np
20
+ from scipy import sparse as sp
21
+ from sklearn.covariance import EmpiricalCovariance as sklearn_EmpiricalCovariance
22
+ from sklearn.utils import check_array
23
+
24
+ from daal4py.sklearn._n_jobs_support import control_n_jobs
25
+ from daal4py.sklearn._utils import daal_check_version, sklearn_check_version
26
+ from onedal.common.hyperparameters import get_hyperparameters
27
+ from onedal.covariance import EmpiricalCovariance as onedal_EmpiricalCovariance
28
+ from sklearnex import config_context
29
+ from sklearnex.metrics import pairwise_distances
30
+
31
+ from ..._device_offload import dispatch, wrap_output_data
32
+ from ..._utils import PatchingConditionsChain, register_hyperparameters
33
+
34
+
35
+ @register_hyperparameters({"fit": get_hyperparameters("covariance", "compute")})
36
+ @control_n_jobs(decorated_methods=["fit", "mahalanobis"])
37
+ class EmpiricalCovariance(sklearn_EmpiricalCovariance):
38
+ __doc__ = sklearn_EmpiricalCovariance.__doc__
39
+
40
+ if sklearn_check_version("1.2"):
41
+ _parameter_constraints: dict = {
42
+ **sklearn_EmpiricalCovariance._parameter_constraints,
43
+ }
44
+
45
+ def _save_attributes(self):
46
+ assert hasattr(self, "_onedal_estimator")
47
+ if not daal_check_version((2024, "P", 400)) and self.assume_centered:
48
+ location = self._onedal_estimator.location_[None, :]
49
+ self._onedal_estimator.covariance_ += np.dot(location.T, location)
50
+ self._onedal_estimator.location_ = np.zeros_like(np.squeeze(location))
51
+ self._set_covariance(self._onedal_estimator.covariance_)
52
+ self.location_ = self._onedal_estimator.location_
53
+
54
+ _onedal_covariance = staticmethod(onedal_EmpiricalCovariance)
55
+
56
+ def _onedal_fit(self, X, queue=None):
57
+ if X.shape[0] == 1:
58
+ warnings.warn(
59
+ "Only one sample available. You may want to reshape your data array"
60
+ )
61
+
62
+ onedal_params = {
63
+ "method": "dense",
64
+ "bias": True,
65
+ "assume_centered": self.assume_centered,
66
+ }
67
+
68
+ self._onedal_estimator = self._onedal_covariance(**onedal_params)
69
+ self._onedal_estimator.fit(X, queue=queue)
70
+ self._save_attributes()
71
+
72
+ def _onedal_supported(self, method_name, *data):
73
+ class_name = self.__class__.__name__
74
+ patching_status = PatchingConditionsChain(
75
+ f"sklearn.covariance.{class_name}.{method_name}"
76
+ )
77
+ if method_name in ["fit", "mahalanobis"]:
78
+ (X,) = data
79
+ patching_status.and_conditions(
80
+ [
81
+ (not sp.issparse(X), "X is sparse. Sparse input is not supported."),
82
+ ]
83
+ )
84
+ return patching_status
85
+ raise RuntimeError(f"Unknown method {method_name} in {self.__class__.__name__}")
86
+
87
+ _onedal_cpu_supported = _onedal_supported
88
+ _onedal_gpu_supported = _onedal_supported
89
+
90
+ def fit(self, X, y=None):
91
+ if sklearn_check_version("1.2"):
92
+ self._validate_params()
93
+ if sklearn_check_version("0.23"):
94
+ X = self._validate_data(X, force_all_finite=False)
95
+ else:
96
+ X = check_array(X, force_all_finite=False)
97
+
98
+ dispatch(
99
+ self,
100
+ "fit",
101
+ {
102
+ "onedal": self.__class__._onedal_fit,
103
+ "sklearn": sklearn_EmpiricalCovariance.fit,
104
+ },
105
+ X,
106
+ )
107
+
108
+ return self
109
+
110
+ # expose sklearnex pairwise_distances if mahalanobis distance eventually supported
111
+ @wrap_output_data
112
+ def mahalanobis(self, X):
113
+ if sklearn_check_version("1.0"):
114
+ X = self._validate_data(X, reset=False)
115
+ else:
116
+ X = check_array(X)
117
+
118
+ precision = self.get_precision()
119
+ with config_context(assume_finite=True):
120
+ # compute mahalanobis distances
121
+ dist = pairwise_distances(
122
+ X, self.location_[np.newaxis, :], metric="mahalanobis", VI=precision
123
+ )
124
+
125
+ return np.reshape(dist, (len(X),)) ** 2
126
+
127
+ error_norm = wrap_output_data(sklearn_EmpiricalCovariance.error_norm)
128
+ score = wrap_output_data(sklearn_EmpiricalCovariance.score)
129
+
130
+ fit.__doc__ = sklearn_EmpiricalCovariance.fit.__doc__
131
+ mahalanobis.__doc__ = sklearn_EmpiricalCovariance.mahalanobis
132
+ error_norm.__doc__ = sklearn_EmpiricalCovariance.error_norm.__doc__
133
+ score.__doc__ = sklearn_EmpiricalCovariance.score.__doc__
@@ -0,0 +1,66 @@
1
+ # ===============================================================================
2
+ # Copyright 2023 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 numpy as np
18
+ import pytest
19
+ from numpy.testing import assert_allclose
20
+
21
+ from daal4py.sklearn._utils import daal_check_version
22
+ from onedal.tests.utils._dataframes_support import (
23
+ _convert_to_dataframe,
24
+ get_dataframes_and_queues,
25
+ )
26
+
27
+
28
+ @pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
29
+ @pytest.mark.parametrize("macro_block", [None, 1024])
30
+ @pytest.mark.parametrize("assume_centered", [True, False])
31
+ def test_sklearnex_import_covariance(dataframe, queue, macro_block, assume_centered):
32
+ from sklearnex.preview.covariance import EmpiricalCovariance
33
+
34
+ X = np.array([[0, 1], [0, 1]])
35
+
36
+ X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
37
+ empcov = EmpiricalCovariance(assume_centered=assume_centered)
38
+ if daal_check_version((2024, "P", 0)) and macro_block is not None:
39
+ hparams = empcov.get_hyperparameters("fit")
40
+ hparams.cpu_macro_block = macro_block
41
+ result = empcov.fit(X)
42
+
43
+ expected_covariance = np.array([[0, 0], [0, 0]])
44
+ expected_means = np.array([0, 0])
45
+
46
+ if assume_centered:
47
+ expected_covariance = np.array([[0, 0], [0, 1]])
48
+ else:
49
+ expected_means = np.array([0, 1])
50
+
51
+ assert_allclose(expected_covariance, result.covariance_)
52
+ assert_allclose(expected_means, result.location_)
53
+
54
+ X = np.array([[1, 2], [3, 6]])
55
+
56
+ X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
57
+ result = empcov.fit(X)
58
+
59
+ if assume_centered:
60
+ expected_covariance = np.array([[5, 10], [10, 20]])
61
+ else:
62
+ expected_covariance = np.array([[1, 2], [2, 4]])
63
+ expected_means = np.array([2, 4])
64
+
65
+ assert_allclose(expected_covariance, result.covariance_)
66
+ assert_allclose(expected_means, result.location_)
@@ -17,6 +17,7 @@
17
17
  __all__ = [
18
18
  "basic_statistics",
19
19
  "cluster",
20
+ "covariance",
20
21
  "decomposition",
21
22
  "ensemble",
22
23
  "linear_model",
@@ -0,0 +1,19 @@
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
+ from .covariance import EmpiricalCovariance
18
+
19
+ __all__ = ["EmpiricalCovariance"]
@@ -0,0 +1,21 @@
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
+ from onedal.spmd.covariance import EmpiricalCovariance
18
+
19
+ # TODO:
20
+ # Currently it uses `onedal` module interface.
21
+ # Add sklearnex dispatching.