scikit-learn-intelex 2024.0.1__py312-none-win_amd64.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 (90) hide show
  1. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/__init__.py +61 -0
  2. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/__main__.py +59 -0
  3. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/_config.py +110 -0
  4. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/_device_offload.py +223 -0
  5. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/_utils.py +95 -0
  6. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/basic_statistics/__init__.py +20 -0
  7. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/basic_statistics/basic_statistics.py +17 -0
  8. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/__init__.py +21 -0
  9. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/dbscan.py +187 -0
  10. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/k_means.py +18 -0
  11. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/tests/test_dbscan.py +37 -0
  12. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/tests/test_kmeans.py +31 -0
  13. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/decomposition/__init__.py +20 -0
  14. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/decomposition/pca.py +18 -0
  15. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/decomposition/tests/test_pca.py +28 -0
  16. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/dispatcher.py +329 -0
  17. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/doc/third-party-programs.txt +424 -0
  18. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/ensemble/__init__.py +30 -0
  19. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/ensemble/_forest.py +1947 -0
  20. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/ensemble/tests/test_forest.py +118 -0
  21. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/glob/__main__.py +73 -0
  22. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/glob/dispatcher.py +88 -0
  23. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/__init__.py +30 -0
  24. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/coordinate_descent.py +18 -0
  25. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/linear.py +373 -0
  26. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/logistic_path.py +18 -0
  27. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/ridge.py +18 -0
  28. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/tests/test_linear.py +77 -0
  29. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/tests/test_logreg.py +29 -0
  30. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/manifold/__init__.py +20 -0
  31. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/manifold/t_sne.py +18 -0
  32. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/manifold/tests/test_tsne.py +27 -0
  33. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/__init__.py +24 -0
  34. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/pairwise.py +18 -0
  35. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/ranking.py +18 -0
  36. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/tests/test_metrics.py +40 -0
  37. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/model_selection/__init__.py +22 -0
  38. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/model_selection/split.py +18 -0
  39. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/model_selection/tests/test_model_selection.py +35 -0
  40. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/__init__.py +28 -0
  41. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/common.py +264 -0
  42. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/knn_classification.py +331 -0
  43. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/knn_regression.py +307 -0
  44. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/knn_unsupervised.py +220 -0
  45. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/lof.py +437 -0
  46. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/tests/test_neighbors.py +85 -0
  47. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/__init__.py +18 -0
  48. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/__init__.py +20 -0
  49. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/_common.py +84 -0
  50. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/k_means.py +370 -0
  51. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/__init__.py +20 -0
  52. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/pca.py +376 -0
  53. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/tests/test_preview_pca.py +38 -0
  54. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/__init__.py +24 -0
  55. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/basic_statistics/__init__.py +19 -0
  56. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/basic_statistics/basic_statistics.py +21 -0
  57. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/cluster/__init__.py +30 -0
  58. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/cluster/dbscan.py +50 -0
  59. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/cluster/kmeans.py +21 -0
  60. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/decomposition/__init__.py +19 -0
  61. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/decomposition/pca.py +21 -0
  62. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/ensemble/__init__.py +19 -0
  63. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/ensemble/forest.py +79 -0
  64. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/linear_model/__init__.py +19 -0
  65. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/linear_model/linear_model.py +21 -0
  66. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/neighbors/__init__.py +19 -0
  67. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/neighbors/neighbors.py +25 -0
  68. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/__init__.py +30 -0
  69. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/_common.py +188 -0
  70. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/nusvc.py +272 -0
  71. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/nusvr.py +163 -0
  72. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/svc.py +301 -0
  73. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/svr.py +164 -0
  74. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/tests/test_svm.py +102 -0
  75. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/_models_info.py +170 -0
  76. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_config.py +39 -0
  77. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_memory_usage.py +225 -0
  78. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_monkeypatch.py +210 -0
  79. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_parallel.py +50 -0
  80. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_patching.py +122 -0
  81. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_run_to_run_stability_tests.py +428 -0
  82. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/utils/_launch_algorithms.py +118 -0
  83. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/utils/__init__.py +19 -0
  84. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/utils/parallel.py +59 -0
  85. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/utils/validation.py +18 -0
  86. scikit_learn_intelex-2024.0.1.dist-info/LICENSE.txt +202 -0
  87. scikit_learn_intelex-2024.0.1.dist-info/METADATA +230 -0
  88. scikit_learn_intelex-2024.0.1.dist-info/RECORD +90 -0
  89. scikit_learn_intelex-2024.0.1.dist-info/WHEEL +5 -0
  90. scikit_learn_intelex-2024.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,187 @@
1
+ #!/usr/bin/env python
2
+ # ===============================================================================
3
+ # Copyright 2021 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
+ import numbers
19
+ from abc import ABC
20
+
21
+ import numpy as np
22
+ from scipy import sparse as sp
23
+ from sklearn.cluster import DBSCAN as sklearn_DBSCAN
24
+ from sklearn.utils.validation import _check_sample_weight
25
+
26
+ from daal4py.sklearn._utils import sklearn_check_version
27
+ from onedal.cluster import DBSCAN as onedal_DBSCAN
28
+
29
+ from .._device_offload import dispatch, wrap_output_data
30
+ from .._utils import PatchingConditionsChain
31
+
32
+ if sklearn_check_version("1.1") and not sklearn_check_version("1.2"):
33
+ from sklearn.utils import check_scalar
34
+
35
+
36
+ class BaseDBSCAN(ABC):
37
+ def _onedal_dbscan(self, **onedal_params):
38
+ return onedal_DBSCAN(**onedal_params)
39
+
40
+ def _save_attributes(self):
41
+ assert hasattr(self, "_onedal_estimator")
42
+
43
+ self.labels_ = self._onedal_estimator.labels_
44
+ self.core_sample_indices_ = self._onedal_estimator.core_sample_indices_
45
+ self.components_ = self._onedal_estimator.components_
46
+ self.n_features_in_ = self._onedal_estimator.n_features_in_
47
+
48
+
49
+ class DBSCAN(sklearn_DBSCAN, BaseDBSCAN):
50
+ __doc__ = sklearn_DBSCAN.__doc__
51
+
52
+ if sklearn_check_version("1.2"):
53
+ _parameter_constraints: dict = {**sklearn_DBSCAN._parameter_constraints}
54
+
55
+ def __init__(
56
+ self,
57
+ eps=0.5,
58
+ *,
59
+ min_samples=5,
60
+ metric="euclidean",
61
+ metric_params=None,
62
+ algorithm="auto",
63
+ leaf_size=30,
64
+ p=None,
65
+ n_jobs=None,
66
+ ):
67
+ super(DBSCAN, self).__init__(
68
+ eps=eps,
69
+ min_samples=min_samples,
70
+ metric=metric,
71
+ metric_params=metric_params,
72
+ algorithm=algorithm,
73
+ leaf_size=leaf_size,
74
+ p=p,
75
+ n_jobs=n_jobs,
76
+ )
77
+ self.eps = eps
78
+ self.min_samples = min_samples
79
+ self.metric = metric
80
+ self.metric_params = metric_params
81
+ self.algorithm = algorithm
82
+ self.leaf_size = leaf_size
83
+ self.p = p
84
+ self.n_jobs = n_jobs
85
+
86
+ def _onedal_fit(self, X, y, sample_weight=None, queue=None):
87
+ onedal_params = {
88
+ "eps": self.eps,
89
+ "min_samples": self.min_samples,
90
+ "metric": self.metric,
91
+ "metric_params": self.metric_params,
92
+ "algorithm": self.algorithm,
93
+ "leaf_size": self.leaf_size,
94
+ "p": self.p,
95
+ "n_jobs": self.n_jobs,
96
+ }
97
+ self._onedal_estimator = self._onedal_dbscan(**onedal_params)
98
+
99
+ self._onedal_estimator.fit(X, y=y, sample_weight=sample_weight, queue=queue)
100
+ self._save_attributes()
101
+
102
+ def _onedal_supported(self, method_name, *data):
103
+ class_name = self.__class__.__name__
104
+ patching_status = PatchingConditionsChain(
105
+ f"sklearn.cluster.{class_name}.{method_name}"
106
+ )
107
+ if method_name == "fit":
108
+ X, y, sample_weight = data
109
+ patching_status.and_conditions(
110
+ [
111
+ (
112
+ self.algorithm in ["auto", "brute"],
113
+ f"'{self.algorithm}' algorithm is not supported. "
114
+ "Only 'auto' and 'brute' algorithms are supported",
115
+ ),
116
+ (
117
+ self.metric == "euclidean"
118
+ or (self.metric == "minkowski" and self.p == 2),
119
+ f"'{self.metric}' (p={self.p}) metric is not supported. "
120
+ "Only 'euclidean' or 'minkowski' with p=2 metrics are supported.",
121
+ ),
122
+ (not sp.issparse(X), "X is sparse. Sparse input is not supported."),
123
+ ]
124
+ )
125
+ return patching_status
126
+ raise RuntimeError(f"Unknown method {method_name} in {self.__class__.__name__}")
127
+
128
+ def _onedal_cpu_supported(self, method_name, *data):
129
+ return self._onedal_supported(method_name, *data)
130
+
131
+ def _onedal_gpu_supported(self, method_name, *data):
132
+ return self._onedal_supported(method_name, *data)
133
+
134
+ def fit(self, X, y=None, sample_weight=None):
135
+ if sklearn_check_version("1.2"):
136
+ self._validate_params()
137
+ elif sklearn_check_version("1.1"):
138
+ check_scalar(
139
+ self.eps,
140
+ "eps",
141
+ target_type=numbers.Real,
142
+ min_val=0.0,
143
+ include_boundaries="neither",
144
+ )
145
+ check_scalar(
146
+ self.min_samples,
147
+ "min_samples",
148
+ target_type=numbers.Integral,
149
+ min_val=1,
150
+ include_boundaries="left",
151
+ )
152
+ check_scalar(
153
+ self.leaf_size,
154
+ "leaf_size",
155
+ target_type=numbers.Integral,
156
+ min_val=1,
157
+ include_boundaries="left",
158
+ )
159
+ if self.p is not None:
160
+ check_scalar(
161
+ self.p,
162
+ "p",
163
+ target_type=numbers.Real,
164
+ min_val=0.0,
165
+ include_boundaries="left",
166
+ )
167
+ if self.n_jobs is not None:
168
+ check_scalar(self.n_jobs, "n_jobs", target_type=numbers.Integral)
169
+ else:
170
+ if self.eps <= 0.0:
171
+ raise ValueError(f"eps == {self.eps}, must be > 0.0.")
172
+
173
+ if sample_weight is not None:
174
+ sample_weight = _check_sample_weight(sample_weight, X)
175
+ dispatch(
176
+ self,
177
+ "fit",
178
+ {
179
+ "onedal": self.__class__._onedal_fit,
180
+ "sklearn": sklearn_DBSCAN.fit,
181
+ },
182
+ X,
183
+ y,
184
+ sample_weight,
185
+ )
186
+
187
+ return self
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env python
2
+ # ===============================================================================
3
+ # Copyright 2021 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 daal4py.sklearn.cluster import KMeans
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env python
2
+ # ===============================================================================
3
+ # Copyright 2021 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
+ import numpy as np
19
+ import pytest
20
+ from numpy.testing import assert_allclose
21
+
22
+
23
+ # TODO:
24
+ # adding this parameterized testing
25
+ # somehow breaks other test with preview module patch:
26
+ # sklearnex/tests/test_monkeypatch.py::test_preview_namespace.
27
+ # @pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
28
+ def test_sklearnex_import_dbscan():
29
+ from sklearnex.cluster import DBSCAN
30
+
31
+ X = np.array([[1, 2], [2, 2], [2, 3], [8, 7], [8, 8], [25, 80]])
32
+ dbscan = DBSCAN(eps=3, min_samples=2).fit(X)
33
+ assert "sklearnex" in dbscan.__module__
34
+
35
+ result = dbscan.labels_
36
+ expected = np.array([0, 0, 0, 1, 1, -1], dtype=np.int32)
37
+ assert_allclose(expected, result)
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env python
2
+ # ===============================================================================
3
+ # Copyright 2021 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
+ import numpy as np
19
+ from numpy.testing import assert_allclose
20
+
21
+
22
+ def test_sklearnex_import():
23
+ from sklearnex.cluster import KMeans
24
+
25
+ X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
26
+ kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
27
+ assert "daal4py" in kmeans.__module__
28
+
29
+ result = kmeans.predict([[0, 0], [12, 3]])
30
+ expected = np.array([1, 0], dtype=np.int32)
31
+ assert_allclose(expected, result)
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env python
2
+ # ===============================================================================
3
+ # Copyright 2021 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"]
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env python
2
+ # ===============================================================================
3
+ # Copyright 2021 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 daal4py.sklearn.decomposition import PCA
@@ -0,0 +1,28 @@
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
+ import numpy as np
19
+ from numpy.testing import assert_allclose
20
+
21
+
22
+ def test_sklearnex_import():
23
+ from sklearnex.decomposition import PCA
24
+
25
+ X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
26
+ pca = PCA(n_components=2, svd_solver="full").fit(X)
27
+ assert "daal4py" in pca.__module__
28
+ assert_allclose(pca.singular_values_, [6.30061232, 0.54980396])
@@ -0,0 +1,329 @@
1
+ #!/usr/bin/env python
2
+ # ==============================================================================
3
+ # Copyright 2021 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
+ import os
19
+ import sys
20
+ from functools import lru_cache
21
+
22
+ from daal4py.sklearn._utils import daal_check_version, sklearn_check_version
23
+
24
+
25
+ def _is_new_patching_available():
26
+ return os.environ.get("OFF_ONEDAL_IFACE") is None and daal_check_version(
27
+ (2021, "P", 300)
28
+ )
29
+
30
+
31
+ def _is_preview_enabled():
32
+ return os.environ.get("SKLEARNEX_PREVIEW") is not None
33
+
34
+
35
+ @lru_cache(maxsize=None)
36
+ def get_patch_map():
37
+ from daal4py.sklearn.monkeypatch.dispatcher import _get_map_of_algorithms
38
+
39
+ mapping = _get_map_of_algorithms().copy()
40
+
41
+ if _is_new_patching_available():
42
+ # Scikit-learn* modules
43
+ import sklearn as base_module
44
+ import sklearn.cluster as cluster_module
45
+ import sklearn.decomposition as decomposition_module
46
+ import sklearn.ensemble as ensemble_module
47
+ import sklearn.linear_model as linear_model_module
48
+ import sklearn.neighbors as neighbors_module
49
+ import sklearn.svm as svm_module
50
+
51
+ if sklearn_check_version("1.2.1"):
52
+ import sklearn.utils.parallel as parallel_module
53
+ else:
54
+ import sklearn.utils.fixes as parallel_module
55
+
56
+ # Classes and functions for patching
57
+ from ._config import config_context as config_context_sklearnex
58
+ from ._config import get_config as get_config_sklearnex
59
+ from ._config import set_config as set_config_sklearnex
60
+
61
+ if sklearn_check_version("1.2.1"):
62
+ from .utils.parallel import _FuncWrapper as _FuncWrapper_sklearnex
63
+ else:
64
+ from .utils.parallel import _FuncWrapperOld as _FuncWrapper_sklearnex
65
+
66
+ from .cluster import DBSCAN as DBSCAN_sklearnex
67
+ from .ensemble import ExtraTreesClassifier as ExtraTreesClassifier_sklearnex
68
+ from .ensemble import ExtraTreesRegressor as ExtraTreesRegressor_sklearnex
69
+ from .ensemble import RandomForestClassifier as RandomForestClassifier_sklearnex
70
+ from .ensemble import RandomForestRegressor as RandomForestRegressor_sklearnex
71
+ from .linear_model import LinearRegression as LinearRegression_sklearnex
72
+ from .neighbors import KNeighborsClassifier as KNeighborsClassifier_sklearnex
73
+ from .neighbors import KNeighborsRegressor as KNeighborsRegressor_sklearnex
74
+ from .neighbors import LocalOutlierFactor as LocalOutlierFactor_sklearnex
75
+ from .neighbors import NearestNeighbors as NearestNeighbors_sklearnex
76
+
77
+ # Preview classes for patching
78
+ from .preview.cluster import KMeans as KMeans_sklearnex
79
+ from .preview.decomposition import PCA as PCA_sklearnex
80
+ from .svm import SVC as SVC_sklearnex
81
+ from .svm import SVR as SVR_sklearnex
82
+ from .svm import NuSVC as NuSVC_sklearnex
83
+ from .svm import NuSVR as NuSVR_sklearnex
84
+
85
+ # Patch for mapping
86
+ if _is_preview_enabled():
87
+ # PCA
88
+ mapping.pop("pca")
89
+ mapping["pca"] = [[(decomposition_module, "PCA", PCA_sklearnex), None]]
90
+
91
+ # KMeans
92
+ mapping.pop("kmeans")
93
+ mapping["kmeans"] = [
94
+ [
95
+ (
96
+ cluster_module,
97
+ "KMeans",
98
+ KMeans_sklearnex,
99
+ ),
100
+ None,
101
+ ]
102
+ ]
103
+
104
+ # DBSCAN
105
+ mapping.pop("dbscan")
106
+ mapping["dbscan"] = [[(cluster_module, "DBSCAN", DBSCAN_sklearnex), None]]
107
+
108
+ # SVM
109
+ mapping.pop("svm")
110
+ mapping.pop("svc")
111
+ mapping["svr"] = [[(svm_module, "SVR", SVR_sklearnex), None]]
112
+ mapping["svc"] = [[(svm_module, "SVC", SVC_sklearnex), None]]
113
+ mapping["nusvr"] = [[(svm_module, "NuSVR", NuSVR_sklearnex), None]]
114
+ mapping["nusvc"] = [[(svm_module, "NuSVC", NuSVC_sklearnex), None]]
115
+
116
+ # Linear Regression
117
+ mapping.pop("linear")
118
+ mapping.pop("linearregression")
119
+ mapping["linear"] = [
120
+ [
121
+ (
122
+ linear_model_module,
123
+ "LinearRegression",
124
+ LinearRegression_sklearnex,
125
+ ),
126
+ None,
127
+ ]
128
+ ]
129
+ mapping["linearregression"] = mapping["linear"]
130
+
131
+ # kNN
132
+ mapping.pop("knn_classifier")
133
+ mapping.pop("kneighborsclassifier")
134
+ mapping.pop("knn_regressor")
135
+ mapping.pop("kneighborsregressor")
136
+ mapping.pop("nearest_neighbors")
137
+ mapping.pop("nearestneighbors")
138
+ mapping["knn_classifier"] = [
139
+ [
140
+ (
141
+ neighbors_module,
142
+ "KNeighborsClassifier",
143
+ KNeighborsClassifier_sklearnex,
144
+ ),
145
+ None,
146
+ ]
147
+ ]
148
+ mapping["knn_regressor"] = [
149
+ [
150
+ (
151
+ neighbors_module,
152
+ "KNeighborsRegressor",
153
+ KNeighborsRegressor_sklearnex,
154
+ ),
155
+ None,
156
+ ]
157
+ ]
158
+ mapping["nearest_neighbors"] = [
159
+ [(neighbors_module, "NearestNeighbors", NearestNeighbors_sklearnex), None]
160
+ ]
161
+ mapping["kneighborsclassifier"] = mapping["knn_classifier"]
162
+ mapping["kneighborsregressor"] = mapping["knn_regressor"]
163
+ mapping["nearestneighbors"] = mapping["nearest_neighbors"]
164
+
165
+ # Ensemble
166
+ mapping["extra_trees_classifier"] = [
167
+ [
168
+ (
169
+ ensemble_module,
170
+ "ExtraTreesClassifier",
171
+ ExtraTreesClassifier_sklearnex,
172
+ ),
173
+ None,
174
+ ]
175
+ ]
176
+ mapping["extra_trees_regressor"] = [
177
+ [
178
+ (
179
+ ensemble_module,
180
+ "ExtraTreesRegressor",
181
+ ExtraTreesRegressor_sklearnex,
182
+ ),
183
+ None,
184
+ ]
185
+ ]
186
+ mapping["extratreesclassifier"] = mapping["extra_trees_classifier"]
187
+ mapping["extratreesregressor"] = mapping["extra_trees_regressor"]
188
+ mapping.pop("random_forest_classifier")
189
+ mapping.pop("random_forest_regressor")
190
+ mapping.pop("randomforestclassifier")
191
+ mapping.pop("randomforestregressor")
192
+ mapping["random_forest_classifier"] = [
193
+ [
194
+ (
195
+ ensemble_module,
196
+ "RandomForestClassifier",
197
+ RandomForestClassifier_sklearnex,
198
+ ),
199
+ None,
200
+ ]
201
+ ]
202
+ mapping["random_forest_regressor"] = [
203
+ [
204
+ (
205
+ ensemble_module,
206
+ "RandomForestRegressor",
207
+ RandomForestRegressor_sklearnex,
208
+ ),
209
+ None,
210
+ ]
211
+ ]
212
+ mapping["randomforestclassifier"] = mapping["random_forest_classifier"]
213
+ mapping["randomforestregressor"] = mapping["random_forest_regressor"]
214
+
215
+ # LocalOutlierFactor
216
+ mapping["lof"] = [
217
+ [
218
+ (neighbors_module, "LocalOutlierFactor", LocalOutlierFactor_sklearnex),
219
+ None,
220
+ ]
221
+ ]
222
+ mapping["localoutlierfactor"] = mapping["lof"]
223
+
224
+ # Configs
225
+ mapping["set_config"] = [
226
+ [(base_module, "set_config", set_config_sklearnex), None]
227
+ ]
228
+ mapping["get_config"] = [
229
+ [(base_module, "get_config", get_config_sklearnex), None]
230
+ ]
231
+ mapping["config_context"] = [
232
+ [(base_module, "config_context", config_context_sklearnex), None]
233
+ ]
234
+
235
+ # Necessary for proper work with multiple threads
236
+ mapping["parallel.get_config"] = [
237
+ [(parallel_module, "get_config", get_config_sklearnex), None]
238
+ ]
239
+ mapping["_funcwrapper"] = [
240
+ [(parallel_module, "_FuncWrapper", _FuncWrapper_sklearnex), None]
241
+ ]
242
+ return mapping
243
+
244
+
245
+ def get_patch_names():
246
+ return list(get_patch_map().keys())
247
+
248
+
249
+ def patch_sklearn(name=None, verbose=True, global_patch=False, preview=False):
250
+ if preview:
251
+ os.environ["SKLEARNEX_PREVIEW"] = "enabled_via_patch_sklearn"
252
+ if not sklearn_check_version("0.22"):
253
+ raise NotImplementedError(
254
+ "Intel(R) Extension for Scikit-learn* patches apply "
255
+ "for scikit-learn >= 0.22 only ..."
256
+ )
257
+
258
+ if global_patch:
259
+ from sklearnex.glob.dispatcher import patch_sklearn_global
260
+
261
+ patch_sklearn_global(name, verbose)
262
+
263
+ from daal4py.sklearn import patch_sklearn as patch_sklearn_orig
264
+
265
+ if _is_new_patching_available():
266
+ for config in ["set_config", "get_config", "config_context"]:
267
+ patch_sklearn_orig(
268
+ config, verbose=False, deprecation=False, get_map=get_patch_map
269
+ )
270
+ if isinstance(name, list):
271
+ for algorithm in name:
272
+ patch_sklearn_orig(
273
+ algorithm, verbose=False, deprecation=False, get_map=get_patch_map
274
+ )
275
+ else:
276
+ patch_sklearn_orig(name, verbose=False, deprecation=False, get_map=get_patch_map)
277
+
278
+ if verbose and sys.stderr is not None:
279
+ sys.stderr.write(
280
+ "Intel(R) Extension for Scikit-learn* enabled "
281
+ "(https://github.com/intel/scikit-learn-intelex)\n"
282
+ )
283
+
284
+
285
+ def unpatch_sklearn(name=None, global_unpatch=False):
286
+ if global_unpatch:
287
+ from sklearnex.glob.dispatcher import unpatch_sklearn_global
288
+
289
+ unpatch_sklearn_global()
290
+ from daal4py.sklearn import unpatch_sklearn as unpatch_sklearn_orig
291
+
292
+ if isinstance(name, list):
293
+ for algorithm in name:
294
+ unpatch_sklearn_orig(algorithm, get_map=get_patch_map)
295
+ else:
296
+ if _is_new_patching_available():
297
+ for config in ["set_config", "get_config", "config_context"]:
298
+ unpatch_sklearn_orig(config, get_map=get_patch_map)
299
+ unpatch_sklearn_orig(name, get_map=get_patch_map)
300
+ if os.environ.get("SKLEARNEX_PREVIEW") == "enabled_via_patch_sklearn":
301
+ os.environ.pop("SKLEARNEX_PREVIEW")
302
+
303
+
304
+ def sklearn_is_patched(name=None, return_map=False):
305
+ from daal4py.sklearn import sklearn_is_patched as sklearn_is_patched_orig
306
+
307
+ if isinstance(name, list):
308
+ if return_map:
309
+ result = {}
310
+ for algorithm in name:
311
+ result[algorithm] = sklearn_is_patched_orig(
312
+ algorithm, get_map=get_patch_map
313
+ )
314
+ return result
315
+ else:
316
+ is_patched = True
317
+ for algorithm in name:
318
+ is_patched = is_patched and sklearn_is_patched_orig(
319
+ algorithm, get_map=get_patch_map
320
+ )
321
+ return is_patched
322
+ else:
323
+ return sklearn_is_patched_orig(name, get_map=get_patch_map, return_map=return_map)
324
+
325
+
326
+ def is_patched_instance(instance: object) -> bool:
327
+ """Returns True if the `instance` is patched with scikit-learn-intelex"""
328
+ module = getattr(instance, "__module__", "")
329
+ return ("daal4py" in module) or ("sklearnex" in module)