scikit-learn-intelex 2024.1.0__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.
- {scikit_learn_intelex-2024.1.0.dist-info → scikit_learn_intelex-2024.4.0.dist-info}/METADATA +2 -2
- scikit_learn_intelex-2024.4.0.dist-info/RECORD +101 -0
- sklearnex/__init__.py +9 -7
- sklearnex/_device_offload.py +31 -4
- sklearnex/basic_statistics/__init__.py +2 -1
- sklearnex/basic_statistics/incremental_basic_statistics.py +288 -0
- sklearnex/basic_statistics/tests/test_incremental_basic_statistics.py +386 -0
- sklearnex/cluster/dbscan.py +6 -4
- sklearnex/conftest.py +63 -0
- sklearnex/{preview/decomposition → covariance}/__init__.py +19 -19
- sklearnex/covariance/incremental_covariance.py +130 -0
- sklearnex/covariance/tests/test_incremental_covariance.py +143 -0
- sklearnex/decomposition/pca.py +319 -1
- sklearnex/decomposition/tests/test_pca.py +34 -5
- sklearnex/dispatcher.py +93 -61
- sklearnex/ensemble/_forest.py +81 -97
- sklearnex/ensemble/tests/test_forest.py +15 -19
- sklearnex/linear_model/__init__.py +1 -2
- sklearnex/linear_model/linear.py +275 -347
- sklearnex/{preview/linear_model → linear_model}/logistic_regression.py +83 -50
- sklearnex/linear_model/tests/test_linear.py +40 -5
- sklearnex/linear_model/tests/test_logreg.py +70 -7
- sklearnex/neighbors/__init__.py +1 -1
- sklearnex/neighbors/_lof.py +221 -0
- sklearnex/neighbors/common.py +4 -1
- sklearnex/neighbors/knn_classification.py +47 -137
- sklearnex/neighbors/knn_regression.py +20 -132
- sklearnex/neighbors/knn_unsupervised.py +16 -93
- sklearnex/neighbors/tests/test_neighbors.py +12 -16
- sklearnex/preview/__init__.py +1 -1
- sklearnex/preview/cluster/k_means.py +8 -81
- sklearnex/preview/covariance/covariance.py +51 -16
- sklearnex/preview/covariance/tests/test_covariance.py +18 -5
- sklearnex/spmd/__init__.py +1 -0
- sklearnex/{preview/linear_model → spmd/covariance}/__init__.py +5 -5
- sklearnex/spmd/covariance/covariance.py +21 -0
- sklearnex/spmd/ensemble/forest.py +4 -12
- sklearnex/spmd/linear_model/__init__.py +2 -1
- sklearnex/spmd/linear_model/logistic_regression.py +21 -0
- sklearnex/svm/_common.py +4 -7
- sklearnex/svm/nusvc.py +74 -55
- sklearnex/svm/nusvr.py +9 -56
- sklearnex/svm/svc.py +74 -56
- sklearnex/svm/svr.py +6 -53
- sklearnex/tests/_utils.py +164 -0
- sklearnex/tests/test_memory_usage.py +9 -7
- sklearnex/tests/test_monkeypatch.py +179 -138
- sklearnex/tests/test_n_jobs_support.py +77 -9
- sklearnex/tests/test_parallel.py +6 -8
- sklearnex/tests/test_patching.py +338 -89
- sklearnex/utils/__init__.py +2 -1
- sklearnex/utils/_namespace.py +97 -0
- scikit_learn_intelex-2024.1.0.dist-info/RECORD +0 -97
- sklearnex/neighbors/lof.py +0 -436
- sklearnex/preview/decomposition/pca.py +0 -376
- sklearnex/preview/decomposition/tests/test_preview_pca.py +0 -42
- sklearnex/preview/linear_model/tests/test_preview_logistic_regression.py +0 -59
- sklearnex/tests/_models_info.py +0 -170
- sklearnex/tests/utils/_launch_algorithms.py +0 -118
- {scikit_learn_intelex-2024.1.0.dist-info → scikit_learn_intelex-2024.4.0.dist-info}/LICENSE.txt +0 -0
- {scikit_learn_intelex-2024.1.0.dist-info → scikit_learn_intelex-2024.4.0.dist-info}/WHEEL +0 -0
- {scikit_learn_intelex-2024.1.0.dist-info → scikit_learn_intelex-2024.4.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# ===============================================================================
|
|
2
|
+
# Copyright 2024 Intel Corporation
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
# ===============================================================================
|
|
16
|
+
|
|
17
|
+
import numpy as np
|
|
18
|
+
import pytest
|
|
19
|
+
from numpy.testing import assert_allclose
|
|
20
|
+
|
|
21
|
+
from onedal.tests.utils._dataframes_support import (
|
|
22
|
+
_convert_to_dataframe,
|
|
23
|
+
get_dataframes_and_queues,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
28
|
+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
|
|
29
|
+
def test_sklearnex_partial_fit_on_gold_data(dataframe, queue, dtype):
|
|
30
|
+
from sklearnex.covariance import IncrementalEmpiricalCovariance
|
|
31
|
+
|
|
32
|
+
X = np.array([[0, 1], [0, 1]])
|
|
33
|
+
X = X.astype(dtype)
|
|
34
|
+
X_split = np.array_split(X, 2)
|
|
35
|
+
inccov = IncrementalEmpiricalCovariance()
|
|
36
|
+
|
|
37
|
+
for i in range(2):
|
|
38
|
+
X_split_df = _convert_to_dataframe(
|
|
39
|
+
X_split[i], sycl_queue=queue, target_df=dataframe
|
|
40
|
+
)
|
|
41
|
+
result = inccov.partial_fit(X_split_df)
|
|
42
|
+
|
|
43
|
+
expected_covariance = np.array([[0, 0], [0, 0]])
|
|
44
|
+
expected_means = np.array([0, 1])
|
|
45
|
+
|
|
46
|
+
assert_allclose(expected_covariance, result.covariance_)
|
|
47
|
+
assert_allclose(expected_means, result.location_)
|
|
48
|
+
|
|
49
|
+
X = np.array([[1, 2], [3, 6]])
|
|
50
|
+
X = X.astype(dtype)
|
|
51
|
+
X_split = np.array_split(X, 2)
|
|
52
|
+
inccov = IncrementalEmpiricalCovariance()
|
|
53
|
+
|
|
54
|
+
for i in range(2):
|
|
55
|
+
X_split_df = _convert_to_dataframe(
|
|
56
|
+
X_split[i], sycl_queue=queue, target_df=dataframe
|
|
57
|
+
)
|
|
58
|
+
result = inccov.partial_fit(X_split_df)
|
|
59
|
+
|
|
60
|
+
expected_covariance = np.array([[1, 2], [2, 4]])
|
|
61
|
+
expected_means = np.array([2, 4])
|
|
62
|
+
|
|
63
|
+
assert_allclose(expected_covariance, result.covariance_)
|
|
64
|
+
assert_allclose(expected_means, result.location_)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
68
|
+
@pytest.mark.parametrize("batch_size", [2, 4])
|
|
69
|
+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
|
|
70
|
+
def test_sklearnex_fit_on_gold_data(dataframe, queue, batch_size, dtype):
|
|
71
|
+
from sklearnex.covariance import IncrementalEmpiricalCovariance
|
|
72
|
+
|
|
73
|
+
X = np.array([[0, 1, 2, 3], [0, -1, -2, -3], [0, 1, 2, 3], [0, 1, 2, 3]])
|
|
74
|
+
X = X.astype(dtype)
|
|
75
|
+
X_df = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
|
|
76
|
+
inccov = IncrementalEmpiricalCovariance(batch_size=batch_size)
|
|
77
|
+
|
|
78
|
+
result = inccov.fit(X_df)
|
|
79
|
+
|
|
80
|
+
expected_covariance = np.array(
|
|
81
|
+
[[0, 0, 0, 0], [0, 0.75, 1.5, 2.25], [0, 1.5, 3, 4.5], [0, 2.25, 4.5, 6.75]]
|
|
82
|
+
)
|
|
83
|
+
expected_means = np.array([0, 0.5, 1, 1.5])
|
|
84
|
+
|
|
85
|
+
assert_allclose(expected_covariance, result.covariance_)
|
|
86
|
+
assert_allclose(expected_means, result.location_)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
90
|
+
@pytest.mark.parametrize("num_batches", [2, 4, 6, 8, 10])
|
|
91
|
+
@pytest.mark.parametrize("row_count", [100, 1000, 2000])
|
|
92
|
+
@pytest.mark.parametrize("column_count", [10, 100, 200])
|
|
93
|
+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
|
|
94
|
+
def test_sklearnex_partial_fit_on_random_data(
|
|
95
|
+
dataframe, queue, num_batches, row_count, column_count, dtype
|
|
96
|
+
):
|
|
97
|
+
from sklearnex.covariance import IncrementalEmpiricalCovariance
|
|
98
|
+
|
|
99
|
+
seed = 77
|
|
100
|
+
gen = np.random.default_rng(seed)
|
|
101
|
+
X = gen.uniform(low=-0.3, high=+0.7, size=(row_count, column_count))
|
|
102
|
+
X = X.astype(dtype)
|
|
103
|
+
X_split = np.array_split(X, num_batches)
|
|
104
|
+
inccov = IncrementalEmpiricalCovariance()
|
|
105
|
+
|
|
106
|
+
for i in range(num_batches):
|
|
107
|
+
X_split_df = _convert_to_dataframe(
|
|
108
|
+
X_split[i], sycl_queue=queue, target_df=dataframe
|
|
109
|
+
)
|
|
110
|
+
result = inccov.partial_fit(X_split_df)
|
|
111
|
+
|
|
112
|
+
expected_covariance = np.cov(X.T, bias=1)
|
|
113
|
+
expected_means = np.mean(X, axis=0)
|
|
114
|
+
|
|
115
|
+
assert_allclose(expected_covariance, result.covariance_, atol=1e-6)
|
|
116
|
+
assert_allclose(expected_means, result.location_, atol=1e-6)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
120
|
+
@pytest.mark.parametrize("num_batches", [2, 4, 6, 8, 10])
|
|
121
|
+
@pytest.mark.parametrize("row_count", [100, 1000, 2000])
|
|
122
|
+
@pytest.mark.parametrize("column_count", [10, 100, 200])
|
|
123
|
+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
|
|
124
|
+
def test_sklearnex_fit_on_random_data(
|
|
125
|
+
dataframe, queue, num_batches, row_count, column_count, dtype
|
|
126
|
+
):
|
|
127
|
+
from sklearnex.covariance import IncrementalEmpiricalCovariance
|
|
128
|
+
|
|
129
|
+
seed = 77
|
|
130
|
+
gen = np.random.default_rng(seed)
|
|
131
|
+
X = gen.uniform(low=-0.3, high=+0.7, size=(row_count, column_count))
|
|
132
|
+
X = X.astype(dtype)
|
|
133
|
+
X_df = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
|
|
134
|
+
batch_size = row_count // num_batches
|
|
135
|
+
inccov = IncrementalEmpiricalCovariance(batch_size=batch_size)
|
|
136
|
+
|
|
137
|
+
result = inccov.fit(X_df)
|
|
138
|
+
|
|
139
|
+
expected_covariance = np.cov(X.T, bias=1)
|
|
140
|
+
expected_means = np.mean(X, axis=0)
|
|
141
|
+
|
|
142
|
+
assert_allclose(expected_covariance, result.covariance_, atol=1e-6)
|
|
143
|
+
assert_allclose(expected_means, result.location_, atol=1e-6)
|
sklearnex/decomposition/pca.py
CHANGED
|
@@ -14,4 +14,322 @@
|
|
|
14
14
|
# limitations under the License.
|
|
15
15
|
# ===============================================================================
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
import logging
|
|
18
|
+
|
|
19
|
+
from daal4py.sklearn._utils import daal_check_version
|
|
20
|
+
|
|
21
|
+
if daal_check_version((2024, "P", 100)):
|
|
22
|
+
import numbers
|
|
23
|
+
from math import sqrt
|
|
24
|
+
|
|
25
|
+
import numpy as np
|
|
26
|
+
from scipy.sparse import issparse
|
|
27
|
+
from sklearn.utils.validation import check_is_fitted
|
|
28
|
+
|
|
29
|
+
from daal4py.sklearn._n_jobs_support import control_n_jobs
|
|
30
|
+
from daal4py.sklearn._utils import sklearn_check_version
|
|
31
|
+
|
|
32
|
+
from .._device_offload import dispatch, wrap_output_data
|
|
33
|
+
from .._utils import PatchingConditionsChain
|
|
34
|
+
|
|
35
|
+
if sklearn_check_version("1.1") and not sklearn_check_version("1.2"):
|
|
36
|
+
from sklearn.utils import check_scalar
|
|
37
|
+
|
|
38
|
+
from sklearn.decomposition import PCA as sklearn_PCA
|
|
39
|
+
|
|
40
|
+
from onedal.decomposition import PCA as onedal_PCA
|
|
41
|
+
|
|
42
|
+
@control_n_jobs(decorated_methods=["fit", "transform", "fit_transform"])
|
|
43
|
+
class PCA(sklearn_PCA):
|
|
44
|
+
__doc__ = sklearn_PCA.__doc__
|
|
45
|
+
|
|
46
|
+
if sklearn_check_version("1.2"):
|
|
47
|
+
_parameter_constraints: dict = {**sklearn_PCA._parameter_constraints}
|
|
48
|
+
|
|
49
|
+
if sklearn_check_version("1.1"):
|
|
50
|
+
|
|
51
|
+
def __init__(
|
|
52
|
+
self,
|
|
53
|
+
n_components=None,
|
|
54
|
+
*,
|
|
55
|
+
copy=True,
|
|
56
|
+
whiten=False,
|
|
57
|
+
svd_solver="auto",
|
|
58
|
+
tol=0.0,
|
|
59
|
+
iterated_power="auto",
|
|
60
|
+
n_oversamples=10,
|
|
61
|
+
power_iteration_normalizer="auto",
|
|
62
|
+
random_state=None,
|
|
63
|
+
):
|
|
64
|
+
self.n_components = n_components
|
|
65
|
+
self.copy = copy
|
|
66
|
+
self.whiten = whiten
|
|
67
|
+
self.svd_solver = svd_solver
|
|
68
|
+
self.tol = tol
|
|
69
|
+
self.iterated_power = iterated_power
|
|
70
|
+
self.n_oversamples = n_oversamples
|
|
71
|
+
self.power_iteration_normalizer = power_iteration_normalizer
|
|
72
|
+
self.random_state = random_state
|
|
73
|
+
|
|
74
|
+
else:
|
|
75
|
+
|
|
76
|
+
def __init__(
|
|
77
|
+
self,
|
|
78
|
+
n_components=None,
|
|
79
|
+
copy=True,
|
|
80
|
+
whiten=False,
|
|
81
|
+
svd_solver="auto",
|
|
82
|
+
tol=0.0,
|
|
83
|
+
iterated_power="auto",
|
|
84
|
+
random_state=None,
|
|
85
|
+
):
|
|
86
|
+
self.n_components = n_components
|
|
87
|
+
self.copy = copy
|
|
88
|
+
self.whiten = whiten
|
|
89
|
+
self.svd_solver = svd_solver
|
|
90
|
+
self.tol = tol
|
|
91
|
+
self.iterated_power = iterated_power
|
|
92
|
+
self.random_state = random_state
|
|
93
|
+
|
|
94
|
+
def fit(self, X, y=None):
|
|
95
|
+
self._fit(X)
|
|
96
|
+
return self
|
|
97
|
+
|
|
98
|
+
@wrap_output_data
|
|
99
|
+
def _fit(self, X):
|
|
100
|
+
if sklearn_check_version("1.2"):
|
|
101
|
+
self._validate_params()
|
|
102
|
+
elif sklearn_check_version("1.1"):
|
|
103
|
+
check_scalar(
|
|
104
|
+
self.n_oversamples,
|
|
105
|
+
"n_oversamples",
|
|
106
|
+
min_val=1,
|
|
107
|
+
target_type=numbers.Integral,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
U, S, Vt = dispatch(
|
|
111
|
+
self,
|
|
112
|
+
"fit",
|
|
113
|
+
{
|
|
114
|
+
"onedal": self.__class__._onedal_fit,
|
|
115
|
+
"sklearn": sklearn_PCA._fit,
|
|
116
|
+
},
|
|
117
|
+
X,
|
|
118
|
+
)
|
|
119
|
+
return U, S, Vt
|
|
120
|
+
|
|
121
|
+
def _onedal_fit(self, X, queue=None):
|
|
122
|
+
X = self._validate_data(
|
|
123
|
+
X,
|
|
124
|
+
dtype=[np.float64, np.float32],
|
|
125
|
+
ensure_2d=True,
|
|
126
|
+
copy=self.copy,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
onedal_params = {
|
|
130
|
+
"n_components": self.n_components,
|
|
131
|
+
"is_deterministic": True,
|
|
132
|
+
"method": "cov",
|
|
133
|
+
"whiten": self.whiten,
|
|
134
|
+
}
|
|
135
|
+
self._onedal_estimator = onedal_PCA(**onedal_params)
|
|
136
|
+
self._onedal_estimator.fit(X, queue=queue)
|
|
137
|
+
self._save_attributes()
|
|
138
|
+
|
|
139
|
+
U = None
|
|
140
|
+
S = self.singular_values_
|
|
141
|
+
Vt = self.components_
|
|
142
|
+
|
|
143
|
+
return U, S, Vt
|
|
144
|
+
|
|
145
|
+
@wrap_output_data
|
|
146
|
+
def transform(self, X):
|
|
147
|
+
return dispatch(
|
|
148
|
+
self,
|
|
149
|
+
"transform",
|
|
150
|
+
{
|
|
151
|
+
"onedal": self.__class__._onedal_transform,
|
|
152
|
+
"sklearn": sklearn_PCA.transform,
|
|
153
|
+
},
|
|
154
|
+
X,
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
def _onedal_transform(self, X, queue=None):
|
|
158
|
+
check_is_fitted(self)
|
|
159
|
+
X = self._validate_data(
|
|
160
|
+
X,
|
|
161
|
+
dtype=[np.float64, np.float32],
|
|
162
|
+
reset=False,
|
|
163
|
+
)
|
|
164
|
+
self._validate_n_features_in_after_fitting(X)
|
|
165
|
+
if sklearn_check_version("1.0"):
|
|
166
|
+
self._check_feature_names(X, reset=False)
|
|
167
|
+
|
|
168
|
+
return self._onedal_estimator.predict(X, queue=queue)
|
|
169
|
+
|
|
170
|
+
def fit_transform(self, X, y=None):
|
|
171
|
+
U, S, Vt = self._fit(X)
|
|
172
|
+
if U is None:
|
|
173
|
+
# oneDAL PCA was fit
|
|
174
|
+
return self.transform(X)
|
|
175
|
+
else:
|
|
176
|
+
# Scikit-learn PCA was fit
|
|
177
|
+
U = U[:, : self.n_components_]
|
|
178
|
+
|
|
179
|
+
if self.whiten:
|
|
180
|
+
U *= sqrt(X.shape[0] - 1)
|
|
181
|
+
else:
|
|
182
|
+
U *= S[: self.n_components_]
|
|
183
|
+
|
|
184
|
+
return U
|
|
185
|
+
|
|
186
|
+
def _onedal_supported(self, method_name, X):
|
|
187
|
+
class_name = self.__class__.__name__
|
|
188
|
+
patching_status = PatchingConditionsChain(
|
|
189
|
+
f"sklearn.decomposition.{class_name}.{method_name}"
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
if method_name == "fit":
|
|
193
|
+
shape_tuple, _is_shape_compatible = self._get_shape_compatibility(X)
|
|
194
|
+
patching_status.and_conditions(
|
|
195
|
+
[
|
|
196
|
+
(
|
|
197
|
+
_is_shape_compatible,
|
|
198
|
+
"Data shape is not compatible.",
|
|
199
|
+
),
|
|
200
|
+
(
|
|
201
|
+
self._is_solver_compatible_with_onedal(shape_tuple),
|
|
202
|
+
f"Only 'full' svd solver is supported.",
|
|
203
|
+
),
|
|
204
|
+
(not issparse(X), "oneDAL PCA does not support sparse data"),
|
|
205
|
+
]
|
|
206
|
+
)
|
|
207
|
+
return patching_status
|
|
208
|
+
|
|
209
|
+
if method_name == "transform":
|
|
210
|
+
patching_status.and_conditions(
|
|
211
|
+
[
|
|
212
|
+
(
|
|
213
|
+
hasattr(self, "_onedal_estimator"),
|
|
214
|
+
"oneDAL model was not trained",
|
|
215
|
+
),
|
|
216
|
+
]
|
|
217
|
+
)
|
|
218
|
+
return patching_status
|
|
219
|
+
|
|
220
|
+
raise RuntimeError(
|
|
221
|
+
f"Unknown method {method_name} in {self.__class__.__name__}"
|
|
222
|
+
)
|
|
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 _get_shape_compatibility(self, X):
|
|
231
|
+
_is_shape_compatible = False
|
|
232
|
+
_empty_shape = (0, 0)
|
|
233
|
+
if hasattr(X, "shape"):
|
|
234
|
+
shape_tuple = X.shape
|
|
235
|
+
if len(shape_tuple) == 1:
|
|
236
|
+
shape_tuple = (1, shape_tuple[0])
|
|
237
|
+
elif isinstance(X, list):
|
|
238
|
+
if np.ndim(X) == 1:
|
|
239
|
+
shape_tuple = (1, len(X))
|
|
240
|
+
elif np.ndim(X) == 2:
|
|
241
|
+
shape_tuple = (len(X), len(X[0]))
|
|
242
|
+
else:
|
|
243
|
+
return _empty_shape, _is_shape_compatible
|
|
244
|
+
|
|
245
|
+
if shape_tuple[0] > 0 and shape_tuple[1] > 0 and len(shape_tuple) == 2:
|
|
246
|
+
_is_shape_compatible = shape_tuple[1] / shape_tuple[0] < 2
|
|
247
|
+
|
|
248
|
+
return shape_tuple, _is_shape_compatible
|
|
249
|
+
|
|
250
|
+
def _is_solver_compatible_with_onedal(self, shape_tuple):
|
|
251
|
+
self._fit_svd_solver = self.svd_solver
|
|
252
|
+
n_sf_min = min(shape_tuple)
|
|
253
|
+
n_components = n_sf_min if self.n_components is None else self.n_components
|
|
254
|
+
|
|
255
|
+
if self._fit_svd_solver == "auto":
|
|
256
|
+
if sklearn_check_version("1.1"):
|
|
257
|
+
if max(shape_tuple) <= 500 or n_components == "mle":
|
|
258
|
+
self._fit_svd_solver = "full"
|
|
259
|
+
elif 1 <= n_components < 0.8 * n_sf_min:
|
|
260
|
+
self._fit_svd_solver = "randomized"
|
|
261
|
+
else:
|
|
262
|
+
self._fit_svd_solver = "full"
|
|
263
|
+
else:
|
|
264
|
+
if n_components == "mle":
|
|
265
|
+
self._fit_svd_solver = "full"
|
|
266
|
+
else:
|
|
267
|
+
# check if sklearnex is faster than randomized sklearn
|
|
268
|
+
# Refer to daal4py
|
|
269
|
+
regression_coefs = np.array(
|
|
270
|
+
[
|
|
271
|
+
[
|
|
272
|
+
9.779873e-11,
|
|
273
|
+
shape_tuple[0] * shape_tuple[1] * n_components,
|
|
274
|
+
],
|
|
275
|
+
[
|
|
276
|
+
-1.122062e-11,
|
|
277
|
+
shape_tuple[0] * shape_tuple[1] * shape_tuple[1],
|
|
278
|
+
],
|
|
279
|
+
[1.127905e-09, shape_tuple[0] ** 2],
|
|
280
|
+
]
|
|
281
|
+
)
|
|
282
|
+
if (
|
|
283
|
+
n_components >= 1
|
|
284
|
+
and np.dot(regression_coefs[:, 0], regression_coefs[:, 1])
|
|
285
|
+
<= 0
|
|
286
|
+
):
|
|
287
|
+
self._fit_svd_solver = "randomized"
|
|
288
|
+
else:
|
|
289
|
+
self._fit_svd_solver = "full"
|
|
290
|
+
|
|
291
|
+
if self._fit_svd_solver == "full":
|
|
292
|
+
return True
|
|
293
|
+
else:
|
|
294
|
+
return False
|
|
295
|
+
|
|
296
|
+
def _save_attributes(self):
|
|
297
|
+
self.n_samples_ = self._onedal_estimator.n_samples_
|
|
298
|
+
if sklearn_check_version("1.2"):
|
|
299
|
+
self.n_features_in_ = self._onedal_estimator.n_features_
|
|
300
|
+
else:
|
|
301
|
+
self.n_features_ = self._onedal_estimator.n_features_
|
|
302
|
+
self.n_features_in_ = self._onedal_estimator.n_features_
|
|
303
|
+
self.n_components_ = self._onedal_estimator.n_components_
|
|
304
|
+
self.components_ = self._onedal_estimator.components_
|
|
305
|
+
self.mean_ = self._onedal_estimator.mean_
|
|
306
|
+
self.singular_values_ = self._onedal_estimator.singular_values_
|
|
307
|
+
self.explained_variance_ = self._onedal_estimator.explained_variance_.ravel()
|
|
308
|
+
self.explained_variance_ratio_ = (
|
|
309
|
+
self._onedal_estimator.explained_variance_ratio_
|
|
310
|
+
)
|
|
311
|
+
self.noise_variance_ = self._onedal_estimator.noise_variance_
|
|
312
|
+
|
|
313
|
+
def _validate_n_features_in_after_fitting(self, X):
|
|
314
|
+
if sklearn_check_version("1.2"):
|
|
315
|
+
expected_n_features = self.n_features_in_
|
|
316
|
+
else:
|
|
317
|
+
expected_n_features = self.n_features_
|
|
318
|
+
if X.shape[1] != expected_n_features:
|
|
319
|
+
raise ValueError(
|
|
320
|
+
(
|
|
321
|
+
f"X has {X.shape[1]} features, "
|
|
322
|
+
f"but PCA is expecting {expected_n_features} features as input"
|
|
323
|
+
)
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
fit.__doc__ = sklearn_PCA.fit.__doc__
|
|
327
|
+
transform.__doc__ = sklearn_PCA.transform.__doc__
|
|
328
|
+
fit_transform.__doc__ = sklearn_PCA.fit_transform.__doc__
|
|
329
|
+
|
|
330
|
+
else:
|
|
331
|
+
from daal4py.sklearn.decomposition import PCA
|
|
332
|
+
|
|
333
|
+
logging.warning(
|
|
334
|
+
"Sklearnex PCA requires oneDAL version >= 2024.1.0 but it was not found"
|
|
335
|
+
)
|
|
@@ -15,13 +15,42 @@
|
|
|
15
15
|
# ===============================================================================
|
|
16
16
|
|
|
17
17
|
import numpy as np
|
|
18
|
+
import pytest
|
|
18
19
|
from numpy.testing import assert_allclose
|
|
19
20
|
|
|
21
|
+
from daal4py.sklearn._utils import daal_check_version
|
|
22
|
+
from onedal.tests.utils._dataframes_support import (
|
|
23
|
+
_as_numpy,
|
|
24
|
+
_convert_to_dataframe,
|
|
25
|
+
get_dataframes_and_queues,
|
|
26
|
+
)
|
|
20
27
|
|
|
21
|
-
|
|
28
|
+
|
|
29
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
30
|
+
def test_sklearnex_import(dataframe, queue):
|
|
22
31
|
from sklearnex.decomposition import PCA
|
|
23
32
|
|
|
24
|
-
X =
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
+
X_transformed_expected = [
|
|
36
|
+
[-1.38340578, -0.2935787],
|
|
37
|
+
[-2.22189802, 0.25133484],
|
|
38
|
+
[-3.6053038, -0.04224385],
|
|
39
|
+
[1.38340578, 0.2935787],
|
|
40
|
+
[2.22189802, -0.25133484],
|
|
41
|
+
[3.6053038, 0.04224385],
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
pca = PCA(n_components=2, svd_solver="full")
|
|
45
|
+
pca.fit(X)
|
|
46
|
+
X_transformed = pca.transform(X)
|
|
47
|
+
X_fit_transformed = PCA(n_components=2, svd_solver="full").fit_transform(X)
|
|
48
|
+
|
|
49
|
+
if daal_check_version((2024, "P", 100)):
|
|
50
|
+
assert "sklearnex" in pca.__module__
|
|
51
|
+
assert hasattr(pca, "_onedal_estimator")
|
|
52
|
+
else:
|
|
53
|
+
assert "daal4py" in pca.__module__
|
|
54
|
+
assert_allclose([6.30061232, 0.54980396], _as_numpy(pca.singular_values_))
|
|
55
|
+
assert_allclose(X_transformed_expected, _as_numpy(X_transformed))
|
|
56
|
+
assert_allclose(X_transformed_expected, _as_numpy(X_fit_transformed))
|