scikit-learn-intelex 2024.3.0__py312-none-manylinux1_x86_64.whl → 2024.5.0__py312-none-manylinux1_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of scikit-learn-intelex might be problematic. Click here for more details.
- {scikit_learn_intelex-2024.3.0.dist-info → scikit_learn_intelex-2024.5.0.dist-info}/METADATA +2 -2
- {scikit_learn_intelex-2024.3.0.dist-info → scikit_learn_intelex-2024.5.0.dist-info}/RECORD +43 -37
- sklearnex/_device_offload.py +39 -5
- 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 +384 -0
- sklearnex/covariance/incremental_covariance.py +217 -30
- sklearnex/covariance/tests/test_incremental_covariance.py +54 -17
- sklearnex/decomposition/pca.py +71 -19
- sklearnex/decomposition/tests/test_pca.py +2 -2
- sklearnex/dispatcher.py +33 -2
- sklearnex/ensemble/_forest.py +73 -79
- sklearnex/linear_model/__init__.py +5 -3
- sklearnex/linear_model/incremental_linear.py +387 -0
- sklearnex/linear_model/linear.py +275 -340
- sklearnex/linear_model/logistic_regression.py +50 -9
- sklearnex/linear_model/tests/test_incremental_linear.py +200 -0
- sklearnex/linear_model/tests/test_linear.py +40 -5
- sklearnex/neighbors/_lof.py +53 -36
- sklearnex/neighbors/common.py +4 -1
- sklearnex/neighbors/knn_classification.py +37 -122
- sklearnex/neighbors/knn_regression.py +10 -117
- sklearnex/neighbors/knn_unsupervised.py +6 -78
- sklearnex/neighbors/tests/test_neighbors.py +2 -2
- sklearnex/preview/cluster/k_means.py +5 -73
- sklearnex/preview/covariance/covariance.py +6 -5
- sklearnex/preview/covariance/tests/test_covariance.py +18 -5
- sklearnex/svm/_common.py +4 -7
- sklearnex/svm/nusvc.py +66 -50
- sklearnex/svm/nusvr.py +3 -49
- sklearnex/svm/svc.py +66 -51
- sklearnex/svm/svr.py +3 -49
- sklearnex/tests/_utils.py +34 -16
- sklearnex/tests/test_memory_usage.py +5 -1
- sklearnex/tests/test_n_jobs_support.py +12 -2
- sklearnex/tests/test_patching.py +87 -58
- sklearnex/tests/test_run_to_run_stability_tests.py +1 -1
- sklearnex/utils/__init__.py +2 -1
- sklearnex/utils/_namespace.py +97 -0
- sklearnex/utils/tests/test_finite.py +89 -0
- {scikit_learn_intelex-2024.3.0.dist-info → scikit_learn_intelex-2024.5.0.dist-info}/LICENSE.txt +0 -0
- {scikit_learn_intelex-2024.3.0.dist-info → scikit_learn_intelex-2024.5.0.dist-info}/WHEEL +0 -0
- {scikit_learn_intelex-2024.3.0.dist-info → scikit_learn_intelex-2024.5.0.dist-info}/top_level.txt +0 -0
|
@@ -14,129 +14,30 @@
|
|
|
14
14
|
# limitations under the License.
|
|
15
15
|
# ===============================================================================
|
|
16
16
|
|
|
17
|
-
from
|
|
18
|
-
from daal4py.sklearn._utils import sklearn_check_version
|
|
19
|
-
|
|
20
|
-
if not sklearn_check_version("1.2"):
|
|
21
|
-
from sklearn.neighbors._base import _check_weights
|
|
22
|
-
|
|
17
|
+
from sklearn.metrics import accuracy_score
|
|
23
18
|
from sklearn.neighbors._classification import (
|
|
24
19
|
KNeighborsClassifier as sklearn_KNeighborsClassifier,
|
|
25
20
|
)
|
|
26
21
|
from sklearn.neighbors._unsupervised import NearestNeighbors as sklearn_NearestNeighbors
|
|
27
22
|
from sklearn.utils.validation import _deprecate_positional_args, check_is_fitted
|
|
28
23
|
|
|
24
|
+
from daal4py.sklearn._n_jobs_support import control_n_jobs
|
|
25
|
+
from daal4py.sklearn._utils import sklearn_check_version
|
|
29
26
|
from onedal.neighbors import KNeighborsClassifier as onedal_KNeighborsClassifier
|
|
30
27
|
|
|
31
28
|
from .._device_offload import dispatch, wrap_output_data
|
|
32
29
|
from .common import KNeighborsDispatchingBase
|
|
33
30
|
|
|
34
|
-
if sklearn_check_version("0.24"):
|
|
35
|
-
|
|
36
|
-
class KNeighborsClassifier_(sklearn_KNeighborsClassifier):
|
|
37
|
-
if sklearn_check_version("1.2"):
|
|
38
|
-
_parameter_constraints: dict = {
|
|
39
|
-
**sklearn_KNeighborsClassifier._parameter_constraints
|
|
40
|
-
}
|
|
41
31
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
*,
|
|
47
|
-
weights="uniform",
|
|
48
|
-
algorithm="auto",
|
|
49
|
-
leaf_size=30,
|
|
50
|
-
p=2,
|
|
51
|
-
metric="minkowski",
|
|
52
|
-
metric_params=None,
|
|
53
|
-
n_jobs=None,
|
|
54
|
-
**kwargs,
|
|
55
|
-
):
|
|
56
|
-
super().__init__(
|
|
57
|
-
n_neighbors=n_neighbors,
|
|
58
|
-
algorithm=algorithm,
|
|
59
|
-
leaf_size=leaf_size,
|
|
60
|
-
metric=metric,
|
|
61
|
-
p=p,
|
|
62
|
-
metric_params=metric_params,
|
|
63
|
-
n_jobs=n_jobs,
|
|
64
|
-
**kwargs,
|
|
65
|
-
)
|
|
66
|
-
self.weights = (
|
|
67
|
-
weights if sklearn_check_version("1.0") else _check_weights(weights)
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
elif sklearn_check_version("0.22"):
|
|
71
|
-
from sklearn.neighbors._base import (
|
|
72
|
-
SupervisedIntegerMixin as BaseSupervisedIntegerMixin,
|
|
73
|
-
)
|
|
74
|
-
|
|
75
|
-
class KNeighborsClassifier_(sklearn_KNeighborsClassifier, BaseSupervisedIntegerMixin):
|
|
76
|
-
@_deprecate_positional_args
|
|
77
|
-
def __init__(
|
|
78
|
-
self,
|
|
79
|
-
n_neighbors=5,
|
|
80
|
-
*,
|
|
81
|
-
weights="uniform",
|
|
82
|
-
algorithm="auto",
|
|
83
|
-
leaf_size=30,
|
|
84
|
-
p=2,
|
|
85
|
-
metric="minkowski",
|
|
86
|
-
metric_params=None,
|
|
87
|
-
n_jobs=None,
|
|
88
|
-
**kwargs,
|
|
89
|
-
):
|
|
90
|
-
super().__init__(
|
|
91
|
-
n_neighbors=n_neighbors,
|
|
92
|
-
algorithm=algorithm,
|
|
93
|
-
leaf_size=leaf_size,
|
|
94
|
-
metric=metric,
|
|
95
|
-
p=p,
|
|
96
|
-
metric_params=metric_params,
|
|
97
|
-
n_jobs=n_jobs,
|
|
98
|
-
**kwargs,
|
|
99
|
-
)
|
|
100
|
-
self.weights = _check_weights(weights)
|
|
101
|
-
|
|
102
|
-
else:
|
|
103
|
-
from sklearn.neighbors.base import (
|
|
104
|
-
SupervisedIntegerMixin as BaseSupervisedIntegerMixin,
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
class KNeighborsClassifier_(sklearn_KNeighborsClassifier, BaseSupervisedIntegerMixin):
|
|
108
|
-
@_deprecate_positional_args
|
|
109
|
-
def __init__(
|
|
110
|
-
self,
|
|
111
|
-
n_neighbors=5,
|
|
112
|
-
*,
|
|
113
|
-
weights="uniform",
|
|
114
|
-
algorithm="auto",
|
|
115
|
-
leaf_size=30,
|
|
116
|
-
p=2,
|
|
117
|
-
metric="minkowski",
|
|
118
|
-
metric_params=None,
|
|
119
|
-
n_jobs=None,
|
|
120
|
-
**kwargs,
|
|
121
|
-
):
|
|
122
|
-
super().__init__(
|
|
123
|
-
n_neighbors=n_neighbors,
|
|
124
|
-
algorithm=algorithm,
|
|
125
|
-
leaf_size=leaf_size,
|
|
126
|
-
metric=metric,
|
|
127
|
-
p=p,
|
|
128
|
-
metric_params=metric_params,
|
|
129
|
-
n_jobs=n_jobs,
|
|
130
|
-
**kwargs,
|
|
131
|
-
)
|
|
132
|
-
self.weights = _check_weights(weights)
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
@control_n_jobs(decorated_methods=["fit", "predict", "predict_proba", "kneighbors"])
|
|
136
|
-
class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
|
|
32
|
+
@control_n_jobs(
|
|
33
|
+
decorated_methods=["fit", "predict", "predict_proba", "kneighbors", "score"]
|
|
34
|
+
)
|
|
35
|
+
class KNeighborsClassifier(sklearn_KNeighborsClassifier, KNeighborsDispatchingBase):
|
|
137
36
|
__doc__ = sklearn_KNeighborsClassifier.__doc__
|
|
138
37
|
if sklearn_check_version("1.2"):
|
|
139
|
-
_parameter_constraints: dict = {
|
|
38
|
+
_parameter_constraints: dict = {
|
|
39
|
+
**sklearn_KNeighborsClassifier._parameter_constraints
|
|
40
|
+
}
|
|
140
41
|
|
|
141
42
|
if sklearn_check_version("1.0"):
|
|
142
43
|
|
|
@@ -192,7 +93,6 @@ class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
|
|
|
192
93
|
)
|
|
193
94
|
|
|
194
95
|
def fit(self, X, y):
|
|
195
|
-
self._fit_validation(X, y)
|
|
196
96
|
dispatch(
|
|
197
97
|
self,
|
|
198
98
|
"fit",
|
|
@@ -235,6 +135,23 @@ class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
|
|
|
235
135
|
X,
|
|
236
136
|
)
|
|
237
137
|
|
|
138
|
+
@wrap_output_data
|
|
139
|
+
def score(self, X, y, sample_weight=None):
|
|
140
|
+
check_is_fitted(self)
|
|
141
|
+
if sklearn_check_version("1.0"):
|
|
142
|
+
self._check_feature_names(X, reset=False)
|
|
143
|
+
return dispatch(
|
|
144
|
+
self,
|
|
145
|
+
"score",
|
|
146
|
+
{
|
|
147
|
+
"onedal": self.__class__._onedal_score,
|
|
148
|
+
"sklearn": sklearn_KNeighborsClassifier.score,
|
|
149
|
+
},
|
|
150
|
+
X,
|
|
151
|
+
y,
|
|
152
|
+
sample_weight=sample_weight,
|
|
153
|
+
)
|
|
154
|
+
|
|
238
155
|
@wrap_output_data
|
|
239
156
|
def kneighbors(self, X=None, n_neighbors=None, return_distance=True):
|
|
240
157
|
check_is_fitted(self)
|
|
@@ -263,18 +180,10 @@ class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
|
|
|
263
180
|
or getattr(self, "_tree", 0) is None
|
|
264
181
|
and self._fit_method == "kd_tree"
|
|
265
182
|
):
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
if sklearn_check_version("0.22"):
|
|
271
|
-
result = sklearn_NearestNeighbors.radius_neighbors(
|
|
272
|
-
self, X, radius, return_distance, sort_results
|
|
273
|
-
)
|
|
274
|
-
else:
|
|
275
|
-
result = sklearn_NearestNeighbors.radius_neighbors(
|
|
276
|
-
self, X, radius, return_distance
|
|
277
|
-
)
|
|
183
|
+
sklearn_NearestNeighbors.fit(self, self._fit_X, getattr(self, "_y", None))
|
|
184
|
+
result = sklearn_NearestNeighbors.radius_neighbors(
|
|
185
|
+
self, X, radius, return_distance, sort_results
|
|
186
|
+
)
|
|
278
187
|
|
|
279
188
|
return result
|
|
280
189
|
|
|
@@ -313,6 +222,11 @@ class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
|
|
|
313
222
|
X, n_neighbors, return_distance, queue=queue
|
|
314
223
|
)
|
|
315
224
|
|
|
225
|
+
def _onedal_score(self, X, y, sample_weight=None, queue=None):
|
|
226
|
+
return accuracy_score(
|
|
227
|
+
y, self._onedal_predict(X, queue=queue), sample_weight=sample_weight
|
|
228
|
+
)
|
|
229
|
+
|
|
316
230
|
def _save_attributes(self):
|
|
317
231
|
self.classes_ = self._onedal_estimator.classes_
|
|
318
232
|
self.n_features_in_ = self._onedal_estimator.n_features_in_
|
|
@@ -326,5 +240,6 @@ class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
|
|
|
326
240
|
fit.__doc__ = sklearn_KNeighborsClassifier.fit.__doc__
|
|
327
241
|
predict.__doc__ = sklearn_KNeighborsClassifier.predict.__doc__
|
|
328
242
|
predict_proba.__doc__ = sklearn_KNeighborsClassifier.predict_proba.__doc__
|
|
243
|
+
score.__doc__ = sklearn_KNeighborsClassifier.score.__doc__
|
|
329
244
|
kneighbors.__doc__ = sklearn_KNeighborsClassifier.kneighbors.__doc__
|
|
330
245
|
radius_neighbors.__doc__ = sklearn_NearestNeighbors.radius_neighbors.__doc__
|
|
@@ -14,125 +14,27 @@
|
|
|
14
14
|
# limitations under the License.
|
|
15
15
|
# ==============================================================================
|
|
16
16
|
|
|
17
|
-
from daal4py.sklearn._n_jobs_support import control_n_jobs
|
|
18
|
-
from daal4py.sklearn._utils import sklearn_check_version
|
|
19
|
-
|
|
20
|
-
if not sklearn_check_version("1.2"):
|
|
21
|
-
from sklearn.neighbors._base import _check_weights
|
|
22
|
-
|
|
23
17
|
from sklearn.neighbors._regression import (
|
|
24
18
|
KNeighborsRegressor as sklearn_KNeighborsRegressor,
|
|
25
19
|
)
|
|
26
20
|
from sklearn.neighbors._unsupervised import NearestNeighbors as sklearn_NearestNeighbors
|
|
27
21
|
from sklearn.utils.validation import _deprecate_positional_args, check_is_fitted
|
|
28
22
|
|
|
23
|
+
from daal4py.sklearn._n_jobs_support import control_n_jobs
|
|
24
|
+
from daal4py.sklearn._utils import sklearn_check_version
|
|
29
25
|
from onedal.neighbors import KNeighborsRegressor as onedal_KNeighborsRegressor
|
|
30
26
|
|
|
31
27
|
from .._device_offload import dispatch, wrap_output_data
|
|
32
28
|
from .common import KNeighborsDispatchingBase
|
|
33
29
|
|
|
34
|
-
if sklearn_check_version("0.24"):
|
|
35
|
-
|
|
36
|
-
class KNeighborsRegressor_(sklearn_KNeighborsRegressor):
|
|
37
|
-
if sklearn_check_version("1.2"):
|
|
38
|
-
_parameter_constraints: dict = {
|
|
39
|
-
**sklearn_KNeighborsRegressor._parameter_constraints
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
@_deprecate_positional_args
|
|
43
|
-
def __init__(
|
|
44
|
-
self,
|
|
45
|
-
n_neighbors=5,
|
|
46
|
-
*,
|
|
47
|
-
weights="uniform",
|
|
48
|
-
algorithm="auto",
|
|
49
|
-
leaf_size=30,
|
|
50
|
-
p=2,
|
|
51
|
-
metric="minkowski",
|
|
52
|
-
metric_params=None,
|
|
53
|
-
n_jobs=None,
|
|
54
|
-
**kwargs,
|
|
55
|
-
):
|
|
56
|
-
super().__init__(
|
|
57
|
-
n_neighbors=n_neighbors,
|
|
58
|
-
algorithm=algorithm,
|
|
59
|
-
leaf_size=leaf_size,
|
|
60
|
-
metric=metric,
|
|
61
|
-
p=p,
|
|
62
|
-
metric_params=metric_params,
|
|
63
|
-
n_jobs=n_jobs,
|
|
64
|
-
**kwargs,
|
|
65
|
-
)
|
|
66
|
-
self.weights = (
|
|
67
|
-
weights if sklearn_check_version("1.0") else _check_weights(weights)
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
elif sklearn_check_version("0.22"):
|
|
71
|
-
from sklearn.neighbors._base import SupervisedFloatMixin as BaseSupervisedFloatMixin
|
|
72
|
-
|
|
73
|
-
class KNeighborsRegressor_(sklearn_KNeighborsRegressor, BaseSupervisedFloatMixin):
|
|
74
|
-
@_deprecate_positional_args
|
|
75
|
-
def __init__(
|
|
76
|
-
self,
|
|
77
|
-
n_neighbors=5,
|
|
78
|
-
*,
|
|
79
|
-
weights="uniform",
|
|
80
|
-
algorithm="auto",
|
|
81
|
-
leaf_size=30,
|
|
82
|
-
p=2,
|
|
83
|
-
metric="minkowski",
|
|
84
|
-
metric_params=None,
|
|
85
|
-
n_jobs=None,
|
|
86
|
-
**kwargs,
|
|
87
|
-
):
|
|
88
|
-
super().__init__(
|
|
89
|
-
n_neighbors=n_neighbors,
|
|
90
|
-
algorithm=algorithm,
|
|
91
|
-
leaf_size=leaf_size,
|
|
92
|
-
metric=metric,
|
|
93
|
-
p=p,
|
|
94
|
-
metric_params=metric_params,
|
|
95
|
-
n_jobs=n_jobs,
|
|
96
|
-
**kwargs,
|
|
97
|
-
)
|
|
98
|
-
self.weights = _check_weights(weights)
|
|
99
|
-
|
|
100
|
-
else:
|
|
101
|
-
from sklearn.neighbors.base import SupervisedFloatMixin as BaseSupervisedFloatMixin
|
|
102
|
-
|
|
103
|
-
class KNeighborsRegressor_(sklearn_KNeighborsRegressor, BaseSupervisedFloatMixin):
|
|
104
|
-
@_deprecate_positional_args
|
|
105
|
-
def __init__(
|
|
106
|
-
self,
|
|
107
|
-
n_neighbors=5,
|
|
108
|
-
*,
|
|
109
|
-
weights="uniform",
|
|
110
|
-
algorithm="auto",
|
|
111
|
-
leaf_size=30,
|
|
112
|
-
p=2,
|
|
113
|
-
metric="minkowski",
|
|
114
|
-
metric_params=None,
|
|
115
|
-
n_jobs=None,
|
|
116
|
-
**kwargs,
|
|
117
|
-
):
|
|
118
|
-
super().__init__(
|
|
119
|
-
n_neighbors=n_neighbors,
|
|
120
|
-
algorithm=algorithm,
|
|
121
|
-
leaf_size=leaf_size,
|
|
122
|
-
metric=metric,
|
|
123
|
-
p=p,
|
|
124
|
-
metric_params=metric_params,
|
|
125
|
-
n_jobs=n_jobs,
|
|
126
|
-
**kwargs,
|
|
127
|
-
)
|
|
128
|
-
self.weights = _check_weights(weights)
|
|
129
|
-
|
|
130
30
|
|
|
131
31
|
@control_n_jobs(decorated_methods=["fit", "predict", "kneighbors"])
|
|
132
|
-
class KNeighborsRegressor(
|
|
32
|
+
class KNeighborsRegressor(sklearn_KNeighborsRegressor, KNeighborsDispatchingBase):
|
|
133
33
|
__doc__ = sklearn_KNeighborsRegressor.__doc__
|
|
134
34
|
if sklearn_check_version("1.2"):
|
|
135
|
-
_parameter_constraints: dict = {
|
|
35
|
+
_parameter_constraints: dict = {
|
|
36
|
+
**sklearn_KNeighborsRegressor._parameter_constraints
|
|
37
|
+
}
|
|
136
38
|
|
|
137
39
|
if sklearn_check_version("1.0"):
|
|
138
40
|
|
|
@@ -188,7 +90,6 @@ class KNeighborsRegressor(KNeighborsRegressor_, KNeighborsDispatchingBase):
|
|
|
188
90
|
)
|
|
189
91
|
|
|
190
92
|
def fit(self, X, y):
|
|
191
|
-
self._fit_validation(X, y)
|
|
192
93
|
dispatch(
|
|
193
94
|
self,
|
|
194
95
|
"fit",
|
|
@@ -244,18 +145,10 @@ class KNeighborsRegressor(KNeighborsRegressor_, KNeighborsDispatchingBase):
|
|
|
244
145
|
or getattr(self, "_tree", 0) is None
|
|
245
146
|
and self._fit_method == "kd_tree"
|
|
246
147
|
):
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
if sklearn_check_version("0.22"):
|
|
252
|
-
result = sklearn_NearestNeighbors.radius_neighbors(
|
|
253
|
-
self, X, radius, return_distance, sort_results
|
|
254
|
-
)
|
|
255
|
-
else:
|
|
256
|
-
result = sklearn_NearestNeighbors.radius_neighbors(
|
|
257
|
-
self, X, radius, return_distance
|
|
258
|
-
)
|
|
148
|
+
sklearn_NearestNeighbors.fit(self, self._fit_X, getattr(self, "_y", None))
|
|
149
|
+
result = sklearn_NearestNeighbors.radius_neighbors(
|
|
150
|
+
self, X, radius, return_distance, sort_results
|
|
151
|
+
)
|
|
259
152
|
|
|
260
153
|
return result
|
|
261
154
|
|
|
@@ -14,12 +14,6 @@
|
|
|
14
14
|
# limitations under the License.
|
|
15
15
|
# ===============================================================================
|
|
16
16
|
|
|
17
|
-
try:
|
|
18
|
-
from packaging.version import Version
|
|
19
|
-
except ImportError:
|
|
20
|
-
from distutils.version import LooseVersion as Version
|
|
21
|
-
|
|
22
|
-
from sklearn import __version__ as sklearn_version
|
|
23
17
|
from sklearn.neighbors._unsupervised import NearestNeighbors as sklearn_NearestNeighbors
|
|
24
18
|
from sklearn.utils.validation import _deprecate_positional_args, check_is_fitted
|
|
25
19
|
|
|
@@ -30,69 +24,12 @@ from onedal.neighbors import NearestNeighbors as onedal_NearestNeighbors
|
|
|
30
24
|
from .._device_offload import dispatch, wrap_output_data
|
|
31
25
|
from .common import KNeighborsDispatchingBase
|
|
32
26
|
|
|
33
|
-
if sklearn_check_version("0.22") and Version(sklearn_version) < Version("0.23"):
|
|
34
|
-
|
|
35
|
-
class NearestNeighbors_(sklearn_NearestNeighbors):
|
|
36
|
-
def __init__(
|
|
37
|
-
self,
|
|
38
|
-
n_neighbors=5,
|
|
39
|
-
radius=1.0,
|
|
40
|
-
algorithm="auto",
|
|
41
|
-
leaf_size=30,
|
|
42
|
-
metric="minkowski",
|
|
43
|
-
p=2,
|
|
44
|
-
metric_params=None,
|
|
45
|
-
n_jobs=None,
|
|
46
|
-
):
|
|
47
|
-
super().__init__(
|
|
48
|
-
n_neighbors=n_neighbors,
|
|
49
|
-
radius=radius,
|
|
50
|
-
algorithm=algorithm,
|
|
51
|
-
leaf_size=leaf_size,
|
|
52
|
-
metric=metric,
|
|
53
|
-
p=p,
|
|
54
|
-
metric_params=metric_params,
|
|
55
|
-
n_jobs=n_jobs,
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
else:
|
|
59
|
-
|
|
60
|
-
class NearestNeighbors_(sklearn_NearestNeighbors):
|
|
61
|
-
if sklearn_check_version("1.2"):
|
|
62
|
-
_parameter_constraints: dict = {
|
|
63
|
-
**sklearn_NearestNeighbors._parameter_constraints
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
@_deprecate_positional_args
|
|
67
|
-
def __init__(
|
|
68
|
-
self,
|
|
69
|
-
*,
|
|
70
|
-
n_neighbors=5,
|
|
71
|
-
radius=1.0,
|
|
72
|
-
algorithm="auto",
|
|
73
|
-
leaf_size=30,
|
|
74
|
-
metric="minkowski",
|
|
75
|
-
p=2,
|
|
76
|
-
metric_params=None,
|
|
77
|
-
n_jobs=None,
|
|
78
|
-
):
|
|
79
|
-
super().__init__(
|
|
80
|
-
n_neighbors=n_neighbors,
|
|
81
|
-
radius=radius,
|
|
82
|
-
algorithm=algorithm,
|
|
83
|
-
leaf_size=leaf_size,
|
|
84
|
-
metric=metric,
|
|
85
|
-
p=p,
|
|
86
|
-
metric_params=metric_params,
|
|
87
|
-
n_jobs=n_jobs,
|
|
88
|
-
)
|
|
89
|
-
|
|
90
27
|
|
|
91
28
|
@control_n_jobs(decorated_methods=["fit", "kneighbors"])
|
|
92
|
-
class NearestNeighbors(
|
|
29
|
+
class NearestNeighbors(sklearn_NearestNeighbors, KNeighborsDispatchingBase):
|
|
93
30
|
__doc__ = sklearn_NearestNeighbors.__doc__
|
|
94
31
|
if sklearn_check_version("1.2"):
|
|
95
|
-
_parameter_constraints: dict = {**
|
|
32
|
+
_parameter_constraints: dict = {**sklearn_NearestNeighbors._parameter_constraints}
|
|
96
33
|
|
|
97
34
|
@_deprecate_positional_args
|
|
98
35
|
def __init__(
|
|
@@ -118,7 +55,6 @@ class NearestNeighbors(NearestNeighbors_, KNeighborsDispatchingBase):
|
|
|
118
55
|
)
|
|
119
56
|
|
|
120
57
|
def fit(self, X, y=None):
|
|
121
|
-
self._fit_validation(X, y)
|
|
122
58
|
dispatch(
|
|
123
59
|
self,
|
|
124
60
|
"fit",
|
|
@@ -159,18 +95,10 @@ class NearestNeighbors(NearestNeighbors_, KNeighborsDispatchingBase):
|
|
|
159
95
|
or getattr(self, "_tree", 0) is None
|
|
160
96
|
and self._fit_method == "kd_tree"
|
|
161
97
|
):
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if sklearn_check_version("0.22"):
|
|
167
|
-
result = sklearn_NearestNeighbors.radius_neighbors(
|
|
168
|
-
self, X, radius, return_distance, sort_results
|
|
169
|
-
)
|
|
170
|
-
else:
|
|
171
|
-
result = sklearn_NearestNeighbors.radius_neighbors(
|
|
172
|
-
self, X, radius, return_distance
|
|
173
|
-
)
|
|
98
|
+
sklearn_NearestNeighbors.fit(self, self._fit_X, getattr(self, "_y", None))
|
|
99
|
+
result = sklearn_NearestNeighbors.radius_neighbors(
|
|
100
|
+
self, X, radius, return_distance, sort_results
|
|
101
|
+
)
|
|
174
102
|
|
|
175
103
|
return result
|
|
176
104
|
|
|
@@ -47,9 +47,9 @@ def test_sklearnex_import_knn_regression(dataframe, queue):
|
|
|
47
47
|
y = _convert_to_dataframe([0, 0, 1, 1], sycl_queue=queue, target_df=dataframe)
|
|
48
48
|
neigh = KNeighborsRegressor(n_neighbors=2).fit(X, y)
|
|
49
49
|
y_test = _convert_to_dataframe([[1.5]], sycl_queue=queue, target_df=dataframe)
|
|
50
|
-
pred = _as_numpy(neigh.predict(y_test))
|
|
50
|
+
pred = _as_numpy(neigh.predict(y_test)).squeeze()
|
|
51
51
|
assert "sklearnex" in neigh.__module__
|
|
52
|
-
assert_allclose(pred,
|
|
52
|
+
assert_allclose(pred, 0.5)
|
|
53
53
|
|
|
54
54
|
|
|
55
55
|
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
@@ -172,24 +172,6 @@ if daal_check_version((2023, "P", 200)):
|
|
|
172
172
|
return patching_status
|
|
173
173
|
|
|
174
174
|
def fit(self, X, y=None, sample_weight=None):
|
|
175
|
-
"""Compute k-means clustering.
|
|
176
|
-
|
|
177
|
-
Parameters
|
|
178
|
-
----------
|
|
179
|
-
X : array-like or sparse matrix, shape=(n_samples, n_features)
|
|
180
|
-
Training instances to cluster. It must be noted that the data
|
|
181
|
-
will be converted to C ordering, which will cause a memory
|
|
182
|
-
copy if the given data is not C-contiguous.
|
|
183
|
-
|
|
184
|
-
y : Ignored
|
|
185
|
-
not used, present here for API consistency by convention.
|
|
186
|
-
|
|
187
|
-
sample_weight : array-like, shape (n_samples,), optional
|
|
188
|
-
The weights for each observation in X. If None, all observations
|
|
189
|
-
are assigned equal weight (default: None)
|
|
190
|
-
|
|
191
|
-
"""
|
|
192
|
-
|
|
193
175
|
if sklearn_check_version("1.0"):
|
|
194
176
|
self._check_feature_names(X, reset=True)
|
|
195
177
|
if sklearn_check_version("1.2"):
|
|
@@ -257,24 +239,6 @@ if daal_check_version((2023, "P", 200)):
|
|
|
257
239
|
|
|
258
240
|
@wrap_output_data
|
|
259
241
|
def predict(self, X):
|
|
260
|
-
"""Compute k-means clustering.
|
|
261
|
-
|
|
262
|
-
Parameters
|
|
263
|
-
----------
|
|
264
|
-
X : array-like or sparse matrix, shape=(n_samples, n_features)
|
|
265
|
-
Training instances to cluster. It must be noted that the data
|
|
266
|
-
will be converted to C ordering, which will cause a memory
|
|
267
|
-
copy if the given data is not C-contiguous.
|
|
268
|
-
|
|
269
|
-
y : Ignored
|
|
270
|
-
not used, present here for API consistency by convention.
|
|
271
|
-
|
|
272
|
-
sample_weight : array-like, shape (n_samples,), optional
|
|
273
|
-
The weights for each observation in X. If None, all observations
|
|
274
|
-
are assigned equal weight (default: None)
|
|
275
|
-
|
|
276
|
-
"""
|
|
277
|
-
|
|
278
242
|
if sklearn_check_version("1.0"):
|
|
279
243
|
self._check_feature_names(X, reset=True)
|
|
280
244
|
if sklearn_check_version("1.2"):
|
|
@@ -317,52 +281,20 @@ if daal_check_version((2023, "P", 200)):
|
|
|
317
281
|
|
|
318
282
|
@wrap_output_data
|
|
319
283
|
def fit_transform(self, X, y=None, sample_weight=None):
|
|
320
|
-
"""Compute clustering and transform X to cluster-distance space.
|
|
321
|
-
|
|
322
|
-
Equivalent to fit(X).transform(X), but more efficiently implemented.
|
|
323
|
-
|
|
324
|
-
Parameters
|
|
325
|
-
----------
|
|
326
|
-
X : {array-like, sparse matrix} of shape (n_samples, n_features)
|
|
327
|
-
New data to transform.
|
|
328
|
-
|
|
329
|
-
y : Ignored
|
|
330
|
-
Not used, present here for API consistency by convention.
|
|
331
|
-
|
|
332
|
-
sample_weight : array-like of shape (n_samples,), default=None
|
|
333
|
-
The weights for each observation in X. If None, all observations
|
|
334
|
-
are assigned equal weight.
|
|
335
|
-
|
|
336
|
-
Returns
|
|
337
|
-
-------
|
|
338
|
-
X_new : ndarray of shape (n_samples, n_clusters)
|
|
339
|
-
X transformed in the new space.
|
|
340
|
-
"""
|
|
341
284
|
return self.fit(X, sample_weight=sample_weight)._transform(X)
|
|
342
285
|
|
|
343
286
|
@wrap_output_data
|
|
344
287
|
def transform(self, X):
|
|
345
|
-
"""Transform X to a cluster-distance space.
|
|
346
|
-
|
|
347
|
-
In the new space, each dimension is the distance to the cluster
|
|
348
|
-
centers. Note that even if X is sparse, the array returned by
|
|
349
|
-
`transform` will typically be dense.
|
|
350
|
-
|
|
351
|
-
Parameters
|
|
352
|
-
----------
|
|
353
|
-
X : {array-like, sparse matrix} of shape (n_samples, n_features)
|
|
354
|
-
New data to transform.
|
|
355
|
-
|
|
356
|
-
Returns
|
|
357
|
-
-------
|
|
358
|
-
X_new : ndarray of shape (n_samples, n_clusters)
|
|
359
|
-
X transformed in the new space.
|
|
360
|
-
"""
|
|
361
288
|
check_is_fitted(self)
|
|
362
289
|
|
|
363
290
|
X = self._check_test_data(X)
|
|
364
291
|
return self._transform(X)
|
|
365
292
|
|
|
293
|
+
fit.__doc__ = sklearn_KMeans.fit.__doc__
|
|
294
|
+
predict.__doc__ = sklearn_KMeans.predict.__doc__
|
|
295
|
+
transform.__doc__ = sklearn_KMeans.transform.__doc__
|
|
296
|
+
fit_transform.__doc__ = sklearn_KMeans.fit_transform.__doc__
|
|
297
|
+
|
|
366
298
|
else:
|
|
367
299
|
from daal4py.sklearn.cluster import KMeans
|
|
368
300
|
|
|
@@ -22,7 +22,7 @@ from sklearn.covariance import EmpiricalCovariance as sklearn_EmpiricalCovarianc
|
|
|
22
22
|
from sklearn.utils import check_array
|
|
23
23
|
|
|
24
24
|
from daal4py.sklearn._n_jobs_support import control_n_jobs
|
|
25
|
-
from daal4py.sklearn._utils import sklearn_check_version
|
|
25
|
+
from daal4py.sklearn._utils import daal_check_version, sklearn_check_version
|
|
26
26
|
from onedal.common.hyperparameters import get_hyperparameters
|
|
27
27
|
from onedal.covariance import EmpiricalCovariance as onedal_EmpiricalCovariance
|
|
28
28
|
from sklearnex import config_context
|
|
@@ -44,6 +44,10 @@ class EmpiricalCovariance(sklearn_EmpiricalCovariance):
|
|
|
44
44
|
|
|
45
45
|
def _save_attributes(self):
|
|
46
46
|
assert hasattr(self, "_onedal_estimator")
|
|
47
|
+
if not daal_check_version((2024, "P", 400)) and self.assume_centered:
|
|
48
|
+
location = self._onedal_estimator.location_[None, :]
|
|
49
|
+
self._onedal_estimator.covariance_ += np.dot(location.T, location)
|
|
50
|
+
self._onedal_estimator.location_ = np.zeros_like(np.squeeze(location))
|
|
47
51
|
self._set_covariance(self._onedal_estimator.covariance_)
|
|
48
52
|
self.location_ = self._onedal_estimator.location_
|
|
49
53
|
|
|
@@ -58,6 +62,7 @@ class EmpiricalCovariance(sklearn_EmpiricalCovariance):
|
|
|
58
62
|
onedal_params = {
|
|
59
63
|
"method": "dense",
|
|
60
64
|
"bias": True,
|
|
65
|
+
"assume_centered": self.assume_centered,
|
|
61
66
|
}
|
|
62
67
|
|
|
63
68
|
self._onedal_estimator = self._onedal_covariance(**onedal_params)
|
|
@@ -73,10 +78,6 @@ class EmpiricalCovariance(sklearn_EmpiricalCovariance):
|
|
|
73
78
|
(X,) = data
|
|
74
79
|
patching_status.and_conditions(
|
|
75
80
|
[
|
|
76
|
-
(
|
|
77
|
-
self.assume_centered == False,
|
|
78
|
-
"assume_centered parameter is not supported on oneDAL side",
|
|
79
|
-
),
|
|
80
81
|
(not sp.issparse(X), "X is sparse. Sparse input is not supported."),
|
|
81
82
|
]
|
|
82
83
|
)
|
|
@@ -27,27 +27,40 @@ from onedal.tests.utils._dataframes_support import (
|
|
|
27
27
|
|
|
28
28
|
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
29
29
|
@pytest.mark.parametrize("macro_block", [None, 1024])
|
|
30
|
-
|
|
30
|
+
@pytest.mark.parametrize("assume_centered", [True, False])
|
|
31
|
+
def test_sklearnex_import_covariance(dataframe, queue, macro_block, assume_centered):
|
|
31
32
|
from sklearnex.preview.covariance import EmpiricalCovariance
|
|
32
33
|
|
|
33
34
|
X = np.array([[0, 1], [0, 1]])
|
|
35
|
+
|
|
34
36
|
X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
|
|
35
|
-
empcov = EmpiricalCovariance()
|
|
37
|
+
empcov = EmpiricalCovariance(assume_centered=assume_centered)
|
|
36
38
|
if daal_check_version((2024, "P", 0)) and macro_block is not None:
|
|
37
39
|
hparams = empcov.get_hyperparameters("fit")
|
|
38
40
|
hparams.cpu_macro_block = macro_block
|
|
39
41
|
result = empcov.fit(X)
|
|
42
|
+
|
|
40
43
|
expected_covariance = np.array([[0, 0], [0, 0]])
|
|
41
|
-
expected_means = np.array([0,
|
|
44
|
+
expected_means = np.array([0, 0])
|
|
45
|
+
|
|
46
|
+
if assume_centered:
|
|
47
|
+
expected_covariance = np.array([[0, 0], [0, 1]])
|
|
48
|
+
else:
|
|
49
|
+
expected_means = np.array([0, 1])
|
|
42
50
|
|
|
43
51
|
assert_allclose(expected_covariance, result.covariance_)
|
|
44
52
|
assert_allclose(expected_means, result.location_)
|
|
45
53
|
|
|
46
54
|
X = np.array([[1, 2], [3, 6]])
|
|
55
|
+
|
|
47
56
|
X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
|
|
48
57
|
result = empcov.fit(X)
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
|
|
59
|
+
if assume_centered:
|
|
60
|
+
expected_covariance = np.array([[5, 10], [10, 20]])
|
|
61
|
+
else:
|
|
62
|
+
expected_covariance = np.array([[1, 2], [2, 4]])
|
|
63
|
+
expected_means = np.array([2, 4])
|
|
51
64
|
|
|
52
65
|
assert_allclose(expected_covariance, result.covariance_)
|
|
53
66
|
assert_allclose(expected_means, result.location_)
|