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.
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/__init__.py +61 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/__main__.py +59 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/_config.py +110 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/_device_offload.py +223 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/_utils.py +95 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/basic_statistics/__init__.py +20 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/basic_statistics/basic_statistics.py +17 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/__init__.py +21 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/dbscan.py +187 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/k_means.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/tests/test_dbscan.py +37 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/tests/test_kmeans.py +31 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/decomposition/__init__.py +20 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/decomposition/pca.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/decomposition/tests/test_pca.py +28 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/dispatcher.py +329 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/doc/third-party-programs.txt +424 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/ensemble/__init__.py +30 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/ensemble/_forest.py +1947 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/ensemble/tests/test_forest.py +118 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/glob/__main__.py +73 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/glob/dispatcher.py +88 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/__init__.py +30 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/coordinate_descent.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/linear.py +373 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/logistic_path.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/ridge.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/tests/test_linear.py +77 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/tests/test_logreg.py +29 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/manifold/__init__.py +20 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/manifold/t_sne.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/manifold/tests/test_tsne.py +27 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/__init__.py +24 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/pairwise.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/ranking.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/tests/test_metrics.py +40 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/model_selection/__init__.py +22 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/model_selection/split.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/model_selection/tests/test_model_selection.py +35 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/__init__.py +28 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/common.py +264 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/knn_classification.py +331 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/knn_regression.py +307 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/knn_unsupervised.py +220 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/lof.py +437 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/tests/test_neighbors.py +85 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/__init__.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/__init__.py +20 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/_common.py +84 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/k_means.py +370 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/__init__.py +20 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/pca.py +376 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/tests/test_preview_pca.py +38 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/__init__.py +24 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/basic_statistics/__init__.py +19 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/basic_statistics/basic_statistics.py +21 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/cluster/__init__.py +30 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/cluster/dbscan.py +50 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/cluster/kmeans.py +21 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/decomposition/__init__.py +19 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/decomposition/pca.py +21 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/ensemble/__init__.py +19 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/ensemble/forest.py +79 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/linear_model/__init__.py +19 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/linear_model/linear_model.py +21 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/neighbors/__init__.py +19 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/neighbors/neighbors.py +25 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/__init__.py +30 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/_common.py +188 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/nusvc.py +272 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/nusvr.py +163 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/svc.py +301 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/svr.py +164 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/tests/test_svm.py +102 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/_models_info.py +170 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_config.py +39 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_memory_usage.py +225 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_monkeypatch.py +210 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_parallel.py +50 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_patching.py +122 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_run_to_run_stability_tests.py +428 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/utils/_launch_algorithms.py +118 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/utils/__init__.py +19 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/utils/parallel.py +59 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/utils/validation.py +18 -0
- scikit_learn_intelex-2024.0.1.dist-info/LICENSE.txt +202 -0
- scikit_learn_intelex-2024.0.1.dist-info/METADATA +230 -0
- scikit_learn_intelex-2024.0.1.dist-info/RECORD +90 -0
- scikit_learn_intelex-2024.0.1.dist-info/WHEEL +5 -0
- scikit_learn_intelex-2024.0.1.dist-info/top_level.txt +1 -0
scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/pca.py
ADDED
|
@@ -0,0 +1,376 @@
|
|
|
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 numbers
|
|
19
|
+
from math import sqrt
|
|
20
|
+
|
|
21
|
+
import numpy as np
|
|
22
|
+
from scipy.sparse import issparse
|
|
23
|
+
from sklearn.base import BaseEstimator
|
|
24
|
+
from sklearn.utils.extmath import stable_cumsum
|
|
25
|
+
from sklearn.utils.validation import check_array, check_is_fitted
|
|
26
|
+
|
|
27
|
+
from daal4py.sklearn._utils import sklearn_check_version
|
|
28
|
+
from onedal.utils import _check_array
|
|
29
|
+
|
|
30
|
+
from ..._device_offload import dispatch
|
|
31
|
+
from ..._utils import PatchingConditionsChain
|
|
32
|
+
|
|
33
|
+
if sklearn_check_version("1.1") and not sklearn_check_version("1.2"):
|
|
34
|
+
from sklearn.utils import check_scalar
|
|
35
|
+
if sklearn_check_version("0.23"):
|
|
36
|
+
from sklearn.decomposition._pca import _infer_dimension
|
|
37
|
+
else:
|
|
38
|
+
from sklearn.decomposition._pca import _infer_dimension_
|
|
39
|
+
|
|
40
|
+
from sklearn.decomposition import PCA as sklearn_PCA
|
|
41
|
+
|
|
42
|
+
from onedal.decomposition import PCA as onedal_PCA
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class PCA(sklearn_PCA):
|
|
46
|
+
if sklearn_check_version("1.2"):
|
|
47
|
+
_parameter_constraints: dict = {**sklearn_PCA._parameter_constraints}
|
|
48
|
+
|
|
49
|
+
def __init__(
|
|
50
|
+
self,
|
|
51
|
+
n_components=None,
|
|
52
|
+
*,
|
|
53
|
+
copy=True,
|
|
54
|
+
whiten=False,
|
|
55
|
+
svd_solver="auto",
|
|
56
|
+
tol=0.0,
|
|
57
|
+
iterated_power="auto",
|
|
58
|
+
n_oversamples=10,
|
|
59
|
+
power_iteration_normalizer="auto",
|
|
60
|
+
random_state=None,
|
|
61
|
+
):
|
|
62
|
+
self.n_components = n_components
|
|
63
|
+
self.copy = copy
|
|
64
|
+
self.whiten = whiten
|
|
65
|
+
self.svd_solver = svd_solver
|
|
66
|
+
self.tol = tol
|
|
67
|
+
self.iterated_power = iterated_power
|
|
68
|
+
self.n_oversamples = n_oversamples
|
|
69
|
+
self.power_iteration_normalizer = power_iteration_normalizer
|
|
70
|
+
self.random_state = random_state
|
|
71
|
+
|
|
72
|
+
def _validate_n_components(self, n_components, n_samples, n_features, n_sf_min):
|
|
73
|
+
if n_components == "mle":
|
|
74
|
+
if n_samples < n_features:
|
|
75
|
+
raise ValueError(
|
|
76
|
+
"n_components='mle' is only supported if" " n_samples >= n_features"
|
|
77
|
+
)
|
|
78
|
+
elif not 0 <= n_components <= n_sf_min:
|
|
79
|
+
raise ValueError(
|
|
80
|
+
"n_components=%r must be between 0 and "
|
|
81
|
+
"min(n_samples, n_features)=%r with "
|
|
82
|
+
"svd_solver='full'" % (n_components, min(n_samples, n_features))
|
|
83
|
+
)
|
|
84
|
+
elif n_components >= 1:
|
|
85
|
+
if not isinstance(n_components, numbers.Integral):
|
|
86
|
+
raise ValueError(
|
|
87
|
+
"n_components=%r must be of type int "
|
|
88
|
+
"when greater than or equal to 1, "
|
|
89
|
+
"was of type=%r" % (n_components, type(n_components))
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
def fit(self, X, y=None):
|
|
93
|
+
if sklearn_check_version("1.2"):
|
|
94
|
+
self._validate_params()
|
|
95
|
+
elif sklearn_check_version("1.1"):
|
|
96
|
+
check_scalar(
|
|
97
|
+
self.n_oversamples,
|
|
98
|
+
"n_oversamples",
|
|
99
|
+
min_val=1,
|
|
100
|
+
target_type=numbers.Integral,
|
|
101
|
+
)
|
|
102
|
+
self._fit(X)
|
|
103
|
+
return self
|
|
104
|
+
|
|
105
|
+
def _fit(self, X):
|
|
106
|
+
if issparse(X):
|
|
107
|
+
raise TypeError(
|
|
108
|
+
"PCA does not support sparse input. See "
|
|
109
|
+
"TruncatedSVD for a possible alternative."
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
if sklearn_check_version("0.23"):
|
|
113
|
+
X = self._validate_data(
|
|
114
|
+
X, dtype=[np.float64, np.float32], ensure_2d=True, copy=False
|
|
115
|
+
)
|
|
116
|
+
else:
|
|
117
|
+
X = _check_array(
|
|
118
|
+
X, dtype=[np.float64, np.float32], ensure_2d=True, copy=False
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
n_samples, n_features = X.shape
|
|
122
|
+
n_sf_min = min(n_samples, n_features)
|
|
123
|
+
|
|
124
|
+
if self.n_components is None:
|
|
125
|
+
if self.svd_solver == "arpack":
|
|
126
|
+
n_components = n_sf_min - 1
|
|
127
|
+
else:
|
|
128
|
+
n_components = n_sf_min
|
|
129
|
+
else:
|
|
130
|
+
n_components = self.n_components
|
|
131
|
+
|
|
132
|
+
self._validate_n_components(n_components, n_samples, n_features, n_sf_min)
|
|
133
|
+
|
|
134
|
+
self._fit_svd_solver = self.svd_solver
|
|
135
|
+
shape_good_for_daal = X.shape[1] / X.shape[0] < 2
|
|
136
|
+
if self._fit_svd_solver == "auto":
|
|
137
|
+
if sklearn_check_version("1.1"):
|
|
138
|
+
if max(X.shape) <= 500 or n_components == "mle":
|
|
139
|
+
self._fit_svd_solver = "full"
|
|
140
|
+
elif 1 <= n_components < 0.8 * n_sf_min:
|
|
141
|
+
self._fit_svd_solver = "randomized"
|
|
142
|
+
else:
|
|
143
|
+
self._fit_svd_solver = "full"
|
|
144
|
+
else:
|
|
145
|
+
if n_components == "mle":
|
|
146
|
+
self._fit_svd_solver = "full"
|
|
147
|
+
else:
|
|
148
|
+
n, p, k = X.shape[0], X.shape[1], n_components
|
|
149
|
+
# check if sklearnex is faster than randomized sklearn
|
|
150
|
+
# Refer to daal4py
|
|
151
|
+
regression_coefs = np.array(
|
|
152
|
+
[
|
|
153
|
+
[9.779873e-11, n * p * k],
|
|
154
|
+
[-1.122062e-11, n * p * p],
|
|
155
|
+
[1.127905e-09, n**2],
|
|
156
|
+
]
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
if (
|
|
160
|
+
n_components >= 1
|
|
161
|
+
and np.dot(regression_coefs[:, 0], regression_coefs[:, 1]) <= 0
|
|
162
|
+
):
|
|
163
|
+
self._fit_svd_solver = "randomized"
|
|
164
|
+
else:
|
|
165
|
+
self._fit_svd_solver = "full"
|
|
166
|
+
|
|
167
|
+
if not shape_good_for_daal or self._fit_svd_solver != "full":
|
|
168
|
+
if sklearn_check_version("0.23"):
|
|
169
|
+
X = self._validate_data(X, copy=self.copy)
|
|
170
|
+
else:
|
|
171
|
+
X = check_array(X, copy=self.copy)
|
|
172
|
+
|
|
173
|
+
# Call different fits for either full or truncated SVD
|
|
174
|
+
if shape_good_for_daal and self._fit_svd_solver == "full":
|
|
175
|
+
return dispatch(
|
|
176
|
+
self,
|
|
177
|
+
"fit",
|
|
178
|
+
{
|
|
179
|
+
"onedal": self.__class__._onedal_fit,
|
|
180
|
+
"sklearn": sklearn_PCA._fit_full,
|
|
181
|
+
},
|
|
182
|
+
X,
|
|
183
|
+
)
|
|
184
|
+
elif not shape_good_for_daal and self._fit_svd_solver == "full":
|
|
185
|
+
return sklearn_PCA._fit_full(self, X, n_components)
|
|
186
|
+
elif self._fit_svd_solver in ["arpack", "randomized"]:
|
|
187
|
+
return sklearn_PCA._fit_truncated(
|
|
188
|
+
self,
|
|
189
|
+
X,
|
|
190
|
+
n_components,
|
|
191
|
+
self._fit_svd_solver,
|
|
192
|
+
)
|
|
193
|
+
else:
|
|
194
|
+
raise ValueError("Unrecognized svd_solver='{0}'".format(self._fit_svd_solver))
|
|
195
|
+
|
|
196
|
+
def _onedal_supported(self, method_name, *data):
|
|
197
|
+
class_name = self.__class__.__name__
|
|
198
|
+
if method_name == "fit":
|
|
199
|
+
patching_status = PatchingConditionsChain(
|
|
200
|
+
f"sklearn.decomposition.{class_name}.{method_name}"
|
|
201
|
+
)
|
|
202
|
+
patching_status.and_conditions(
|
|
203
|
+
[
|
|
204
|
+
(
|
|
205
|
+
self._fit_svd_solver == "full",
|
|
206
|
+
f"'{self._fit_svd_solver}' SVD solver is not supported. "
|
|
207
|
+
"Only 'full' solver is supported.",
|
|
208
|
+
),
|
|
209
|
+
]
|
|
210
|
+
)
|
|
211
|
+
return patching_status
|
|
212
|
+
elif method_name == "transform":
|
|
213
|
+
patching_status = PatchingConditionsChain(
|
|
214
|
+
f"sklearn.decomposition.{class_name}.{method_name}"
|
|
215
|
+
)
|
|
216
|
+
patching_status.and_conditions(
|
|
217
|
+
[
|
|
218
|
+
(hasattr(self, "_onedal_estimator"), "oneDAL model was not trained"),
|
|
219
|
+
]
|
|
220
|
+
)
|
|
221
|
+
return patching_status
|
|
222
|
+
raise RuntimeError(f"Unknown method {method_name} in {self.__class__.__name__}")
|
|
223
|
+
|
|
224
|
+
def _onedal_cpu_supported(self, method_name, *data):
|
|
225
|
+
return self._onedal_supported(method_name, *data)
|
|
226
|
+
|
|
227
|
+
def _onedal_gpu_supported(self, method_name, *data):
|
|
228
|
+
return self._onedal_supported(method_name, *data)
|
|
229
|
+
|
|
230
|
+
def _onedal_fit(self, X, y=None, queue=None):
|
|
231
|
+
if self.n_components == "mle" or self.n_components is None:
|
|
232
|
+
onedal_n_components = min(X.shape)
|
|
233
|
+
elif 0 < self.n_components < 1:
|
|
234
|
+
onedal_n_components = min(X.shape)
|
|
235
|
+
else:
|
|
236
|
+
onedal_n_components = self.n_components
|
|
237
|
+
|
|
238
|
+
onedal_params = {
|
|
239
|
+
"n_components": onedal_n_components,
|
|
240
|
+
"is_deterministic": True,
|
|
241
|
+
"method": "precomputed",
|
|
242
|
+
}
|
|
243
|
+
self._onedal_estimator = onedal_PCA(**onedal_params)
|
|
244
|
+
self._onedal_estimator.fit(X, queue=queue)
|
|
245
|
+
self._save_attributes()
|
|
246
|
+
|
|
247
|
+
U = None
|
|
248
|
+
S = self.singular_values_
|
|
249
|
+
V = self.components_
|
|
250
|
+
|
|
251
|
+
return U, S, V
|
|
252
|
+
|
|
253
|
+
def _onedal_predict(self, X, queue=None):
|
|
254
|
+
return self._onedal_estimator.predict(X, queue)
|
|
255
|
+
|
|
256
|
+
def _onedal_transform(self, X):
|
|
257
|
+
X = _check_array(X, dtype=[np.float64, np.float32], ensure_2d=True, copy=False)
|
|
258
|
+
|
|
259
|
+
if hasattr(self, "n_features_in_"):
|
|
260
|
+
if self.n_features_in_ != X.shape[1]:
|
|
261
|
+
raise ValueError(
|
|
262
|
+
f"X has {X.shape[1]} features, "
|
|
263
|
+
f"but {self.__class__.__name__} is expecting "
|
|
264
|
+
f"{self.n_features_in_} features as input"
|
|
265
|
+
)
|
|
266
|
+
elif hasattr(self, "n_features_"):
|
|
267
|
+
if self.n_features_ != X.shape[1]:
|
|
268
|
+
raise ValueError(
|
|
269
|
+
f"X has {X.shape[1]} features, "
|
|
270
|
+
f"but {self.__class__.__name__} is expecting "
|
|
271
|
+
f"{self.n_features_} features as input"
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
# Mean center
|
|
275
|
+
X_centered = X - self.mean_
|
|
276
|
+
return dispatch(
|
|
277
|
+
self,
|
|
278
|
+
"transform",
|
|
279
|
+
{
|
|
280
|
+
"onedal": self.__class__._onedal_predict,
|
|
281
|
+
"sklearn": sklearn_PCA.transform,
|
|
282
|
+
},
|
|
283
|
+
X_centered,
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
def transform(self, X):
|
|
287
|
+
check_is_fitted(self)
|
|
288
|
+
if hasattr(self, "_onedal_estimator"):
|
|
289
|
+
X_new = self._onedal_transform(X)[:, : self.n_components_]
|
|
290
|
+
if self.whiten:
|
|
291
|
+
X_new /= np.sqrt(self.explained_variance_)
|
|
292
|
+
else:
|
|
293
|
+
return sklearn_PCA.transform(self, X)
|
|
294
|
+
return X_new
|
|
295
|
+
|
|
296
|
+
def fit_transform(self, X, y=None):
|
|
297
|
+
"""Fit the model with X and apply the dimensionality reduction on X.
|
|
298
|
+
Parameters
|
|
299
|
+
----------
|
|
300
|
+
X : array-like of shape (n_samples, n_features)
|
|
301
|
+
Training data, where `n_samples` is the number of samples
|
|
302
|
+
and `n_features` is the number of features.
|
|
303
|
+
y : Ignored.
|
|
304
|
+
|
|
305
|
+
Returns
|
|
306
|
+
-------
|
|
307
|
+
X_new : ndarray of shape (n_samples, n_components)
|
|
308
|
+
Transformed values of X.
|
|
309
|
+
"""
|
|
310
|
+
if self.svd_solver in ["randomized", "arpack"]:
|
|
311
|
+
return sklearn_PCA.fit_transform(self, X)
|
|
312
|
+
else:
|
|
313
|
+
self.fit(X)
|
|
314
|
+
if hasattr(self, "_onedal_estimator"):
|
|
315
|
+
X_new = self._onedal_transform(X)[:, : self.n_components_]
|
|
316
|
+
if self.whiten:
|
|
317
|
+
X_new /= np.sqrt(self.explained_variance_)
|
|
318
|
+
return X_new
|
|
319
|
+
else:
|
|
320
|
+
return sklearn_PCA.transform(self, X)
|
|
321
|
+
|
|
322
|
+
def _save_attributes(self):
|
|
323
|
+
self.n_samples_ = self._onedal_estimator.n_samples_
|
|
324
|
+
|
|
325
|
+
if sklearn_check_version("1.2"):
|
|
326
|
+
self.n_features_in_ = self._onedal_estimator.n_features_in_
|
|
327
|
+
n_features = self.n_features_in_
|
|
328
|
+
elif sklearn_check_version("0.24"):
|
|
329
|
+
self.n_features_ = self._onedal_estimator.n_features_
|
|
330
|
+
self.n_features_in_ = self._onedal_estimator.n_features_in_
|
|
331
|
+
n_features = self.n_features_in_
|
|
332
|
+
else:
|
|
333
|
+
self.n_features_ = self._onedal_estimator.n_features_
|
|
334
|
+
n_features = self.n_features_
|
|
335
|
+
n_sf_min = min(self.n_samples_, n_features)
|
|
336
|
+
|
|
337
|
+
self.mean_ = self._onedal_estimator.mean_
|
|
338
|
+
self.singular_values_ = self._onedal_estimator.singular_values_
|
|
339
|
+
self.explained_variance_ = self._onedal_estimator.explained_variance_
|
|
340
|
+
self.explained_variance_ratio_ = self._onedal_estimator.explained_variance_ratio_
|
|
341
|
+
|
|
342
|
+
if self.n_components is None:
|
|
343
|
+
self.n_components_ = self._onedal_estimator.n_components_
|
|
344
|
+
elif self.n_components == "mle":
|
|
345
|
+
if sklearn_check_version("0.23"):
|
|
346
|
+
self.n_components_ = _infer_dimension(
|
|
347
|
+
self.explained_variance_, self.n_samples_
|
|
348
|
+
)
|
|
349
|
+
else:
|
|
350
|
+
self.n_components_ = _infer_dimension_(
|
|
351
|
+
self.explained_variance_, self.n_samples_, n_features
|
|
352
|
+
)
|
|
353
|
+
elif 0 < self.n_components < 1.0:
|
|
354
|
+
ratio_cumsum = stable_cumsum(self.explained_variance_ratio_)
|
|
355
|
+
self.n_components_ = (
|
|
356
|
+
np.searchsorted(ratio_cumsum, self.n_components, side="right") + 1
|
|
357
|
+
)
|
|
358
|
+
else:
|
|
359
|
+
self.n_components_ = self._onedal_estimator.n_components_
|
|
360
|
+
|
|
361
|
+
if self.n_components_ < n_sf_min:
|
|
362
|
+
if self.explained_variance_.shape[0] == n_sf_min:
|
|
363
|
+
self.noise_variance_ = self.explained_variance_[
|
|
364
|
+
self.n_components_ :
|
|
365
|
+
].mean()
|
|
366
|
+
else:
|
|
367
|
+
self.noise_variance_ = self._onedal_estimator.noise_variance_
|
|
368
|
+
else:
|
|
369
|
+
self.noise_variance_ = 0.0
|
|
370
|
+
|
|
371
|
+
self.explained_variance_ = self.explained_variance_[: self.n_components_]
|
|
372
|
+
self.explained_variance_ratio_ = self.explained_variance_ratio_[
|
|
373
|
+
: self.n_components_
|
|
374
|
+
]
|
|
375
|
+
self.components_ = self._onedal_estimator.components_[: self.n_components_]
|
|
376
|
+
self.singular_values_ = self.singular_values_[: self.n_components_]
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
import pytest
|
|
20
|
+
from numpy.testing import assert_allclose
|
|
21
|
+
|
|
22
|
+
from onedal.tests.utils._dataframes_support import (
|
|
23
|
+
_as_numpy,
|
|
24
|
+
_convert_to_dataframe,
|
|
25
|
+
get_dataframes_and_queues,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
30
|
+
def test_sklearnex_import(dataframe, queue):
|
|
31
|
+
from sklearnex.preview.decomposition import PCA
|
|
32
|
+
|
|
33
|
+
X = [[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]
|
|
34
|
+
X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
|
|
35
|
+
pca = PCA(n_components=2, svd_solver="full").fit(X)
|
|
36
|
+
assert "sklearnex" in pca.__module__
|
|
37
|
+
assert hasattr(pca, "_onedal_estimator")
|
|
38
|
+
assert_allclose(_as_numpy(pca.singular_values_), [6.30061232, 0.54980396])
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
__all__ = [
|
|
18
|
+
"basic_statistics",
|
|
19
|
+
"cluster",
|
|
20
|
+
"decomposition",
|
|
21
|
+
"ensemble",
|
|
22
|
+
"linear_model",
|
|
23
|
+
"neighbors",
|
|
24
|
+
]
|
|
@@ -0,0 +1,19 @@
|
|
|
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 .basic_statistics import BasicStatistics
|
|
18
|
+
|
|
19
|
+
__all__ = ["BasicStatistics"]
|
|
@@ -0,0 +1,21 @@
|
|
|
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 onedal.spmd.basic_statistics import BasicStatistics
|
|
18
|
+
|
|
19
|
+
# TODO:
|
|
20
|
+
# Currently it uses `onedal` module interface.
|
|
21
|
+
# Add sklearnex dispatching.
|
|
@@ -0,0 +1,30 @@
|
|
|
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 daal4py.sklearn._utils import daal_check_version
|
|
18
|
+
|
|
19
|
+
from .dbscan import DBSCAN
|
|
20
|
+
|
|
21
|
+
if daal_check_version((2023, "P", 200)):
|
|
22
|
+
from .kmeans import KMeans
|
|
23
|
+
|
|
24
|
+
__all__ = ["DBSCAN", "KMeans"]
|
|
25
|
+
else:
|
|
26
|
+
# TODO:
|
|
27
|
+
# update versioning for DBSCAN.
|
|
28
|
+
__all__ = [
|
|
29
|
+
"DBSCAN",
|
|
30
|
+
]
|
|
@@ -0,0 +1,50 @@
|
|
|
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 abc import ABC
|
|
18
|
+
|
|
19
|
+
from onedal.spmd.cluster import DBSCAN as onedal_DBSCAN
|
|
20
|
+
|
|
21
|
+
from ...cluster import DBSCAN as DBSCAN_Batch
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class BaseDBSCANspmd(ABC):
|
|
25
|
+
def _onedal_dbscan(self, **onedal_params):
|
|
26
|
+
return onedal_DBSCAN(**onedal_params)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class DBSCAN(BaseDBSCANspmd, DBSCAN_Batch):
|
|
30
|
+
__doc__ = DBSCAN_Batch.__doc__
|
|
31
|
+
|
|
32
|
+
def _onedal_cpu_supported(self, method_name, *data):
|
|
33
|
+
# TODO:
|
|
34
|
+
# check which methods supported SPMD interface on CPU.
|
|
35
|
+
ready = super()._onedal_cpu_supported(method_name, *data)
|
|
36
|
+
if not ready:
|
|
37
|
+
raise RuntimeError(
|
|
38
|
+
f"Method {method_name} in {self.__class__.__name__} "
|
|
39
|
+
"is not supported with given inputs."
|
|
40
|
+
)
|
|
41
|
+
return ready
|
|
42
|
+
|
|
43
|
+
def _onedal_gpu_supported(self, method_name, *data):
|
|
44
|
+
ready = super()._onedal_gpu_supported(method_name, *data)
|
|
45
|
+
if not ready:
|
|
46
|
+
raise RuntimeError(
|
|
47
|
+
f"Method {method_name} in {self.__class__.__name__} "
|
|
48
|
+
"is not supported with given inputs."
|
|
49
|
+
)
|
|
50
|
+
return ready
|
|
@@ -0,0 +1,21 @@
|
|
|
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 onedal.spmd.cluster import KMeans
|
|
18
|
+
|
|
19
|
+
# TODO:
|
|
20
|
+
# Currently it uses `onedal` module interface.
|
|
21
|
+
# Add sklearnex dispatching.
|
scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/decomposition/__init__.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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 .pca import PCA
|
|
18
|
+
|
|
19
|
+
__all__ = ["PCA"]
|
|
@@ -0,0 +1,21 @@
|
|
|
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 onedal.spmd.decomposition import PCA
|
|
18
|
+
|
|
19
|
+
# TODO:
|
|
20
|
+
# Currently it uses `onedal` module interface.
|
|
21
|
+
# Add sklearnex dispatching.
|
|
@@ -0,0 +1,19 @@
|
|
|
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 .forest import RandomForestClassifier, RandomForestRegressor
|
|
18
|
+
|
|
19
|
+
__all__ = ["RandomForestClassifier", "RandomForestRegressor"]
|