scikit-learn-intelex 2024.0.1__py311-none-manylinux1_x86_64.whl → 2024.4.0__py311-none-manylinux1_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of scikit-learn-intelex might be problematic. Click here for more details.

Files changed (89) hide show
  1. {scikit_learn_intelex-2024.0.1.dist-info → scikit_learn_intelex-2024.4.0.dist-info}/METADATA +2 -2
  2. scikit_learn_intelex-2024.4.0.dist-info/RECORD +101 -0
  3. sklearnex/__init__.py +11 -7
  4. sklearnex/__main__.py +0 -1
  5. sklearnex/_device_offload.py +31 -4
  6. sklearnex/_utils.py +15 -1
  7. sklearnex/basic_statistics/__init__.py +2 -2
  8. sklearnex/basic_statistics/incremental_basic_statistics.py +288 -0
  9. sklearnex/basic_statistics/tests/test_incremental_basic_statistics.py +386 -0
  10. sklearnex/cluster/__init__.py +0 -1
  11. sklearnex/cluster/dbscan.py +5 -2
  12. sklearnex/cluster/k_means.py +0 -1
  13. sklearnex/cluster/tests/test_dbscan.py +0 -1
  14. sklearnex/cluster/tests/test_kmeans.py +0 -1
  15. sklearnex/conftest.py +63 -0
  16. sklearnex/covariance/__init__.py +19 -0
  17. sklearnex/covariance/incremental_covariance.py +130 -0
  18. sklearnex/covariance/tests/test_incremental_covariance.py +143 -0
  19. sklearnex/decomposition/__init__.py +0 -1
  20. sklearnex/decomposition/pca.py +319 -2
  21. sklearnex/decomposition/tests/test_pca.py +34 -6
  22. sklearnex/dispatcher.py +93 -28
  23. sklearnex/ensemble/__init__.py +0 -1
  24. sklearnex/ensemble/_forest.py +93 -89
  25. sklearnex/ensemble/tests/test_forest.py +15 -20
  26. sklearnex/glob/__main__.py +0 -1
  27. sklearnex/glob/dispatcher.py +0 -1
  28. sklearnex/linear_model/__init__.py +1 -3
  29. sklearnex/linear_model/coordinate_descent.py +0 -1
  30. sklearnex/linear_model/linear.py +275 -332
  31. sklearnex/linear_model/logistic_path.py +0 -1
  32. sklearnex/linear_model/logistic_regression.py +385 -0
  33. sklearnex/linear_model/ridge.py +0 -1
  34. sklearnex/linear_model/tests/test_linear.py +47 -7
  35. sklearnex/linear_model/tests/test_logreg.py +70 -8
  36. sklearnex/manifold/__init__.py +0 -1
  37. sklearnex/manifold/t_sne.py +0 -1
  38. sklearnex/manifold/tests/test_tsne.py +0 -1
  39. sklearnex/metrics/__init__.py +0 -1
  40. sklearnex/metrics/pairwise.py +0 -1
  41. sklearnex/metrics/ranking.py +0 -1
  42. sklearnex/metrics/tests/test_metrics.py +0 -1
  43. sklearnex/model_selection/__init__.py +0 -1
  44. sklearnex/model_selection/split.py +0 -1
  45. sklearnex/model_selection/tests/test_model_selection.py +0 -1
  46. sklearnex/neighbors/__init__.py +1 -2
  47. sklearnex/neighbors/_lof.py +221 -0
  48. sklearnex/neighbors/common.py +5 -3
  49. sklearnex/neighbors/knn_classification.py +47 -133
  50. sklearnex/neighbors/knn_regression.py +20 -129
  51. sklearnex/neighbors/knn_unsupervised.py +15 -89
  52. sklearnex/neighbors/tests/test_neighbors.py +12 -17
  53. sklearnex/preview/__init__.py +1 -2
  54. sklearnex/preview/cluster/__init__.py +0 -1
  55. sklearnex/preview/cluster/k_means.py +7 -74
  56. sklearnex/preview/{decomposition → covariance}/__init__.py +19 -20
  57. sklearnex/preview/covariance/covariance.py +133 -0
  58. sklearnex/preview/covariance/tests/test_covariance.py +66 -0
  59. sklearnex/spmd/__init__.py +1 -0
  60. sklearnex/spmd/covariance/__init__.py +19 -0
  61. sklearnex/spmd/covariance/covariance.py +21 -0
  62. sklearnex/spmd/ensemble/forest.py +4 -12
  63. sklearnex/spmd/linear_model/__init__.py +2 -1
  64. sklearnex/spmd/linear_model/logistic_regression.py +21 -0
  65. sklearnex/svm/__init__.py +0 -1
  66. sklearnex/svm/_common.py +4 -7
  67. sklearnex/svm/nusvc.py +73 -49
  68. sklearnex/svm/nusvr.py +8 -52
  69. sklearnex/svm/svc.py +74 -51
  70. sklearnex/svm/svr.py +5 -49
  71. sklearnex/svm/tests/test_svm.py +0 -1
  72. sklearnex/tests/_utils.py +164 -0
  73. sklearnex/tests/test_memory_usage.py +9 -7
  74. sklearnex/tests/test_monkeypatch.py +192 -134
  75. sklearnex/tests/test_n_jobs_support.py +99 -0
  76. sklearnex/tests/test_parallel.py +6 -8
  77. sklearnex/tests/test_patching.py +338 -89
  78. sklearnex/utils/__init__.py +2 -1
  79. sklearnex/utils/_namespace.py +97 -0
  80. sklearnex/utils/validation.py +0 -1
  81. scikit_learn_intelex-2024.0.1.dist-info/RECORD +0 -90
  82. sklearnex/neighbors/lof.py +0 -437
  83. sklearnex/preview/decomposition/pca.py +0 -376
  84. sklearnex/preview/decomposition/tests/test_preview_pca.py +0 -38
  85. sklearnex/tests/_models_info.py +0 -170
  86. sklearnex/tests/utils/_launch_algorithms.py +0 -118
  87. {scikit_learn_intelex-2024.0.1.dist-info → scikit_learn_intelex-2024.4.0.dist-info}/LICENSE.txt +0 -0
  88. {scikit_learn_intelex-2024.0.1.dist-info → scikit_learn_intelex-2024.4.0.dist-info}/WHEEL +0 -0
  89. {scikit_learn_intelex-2024.0.1.dist-info → scikit_learn_intelex-2024.4.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,221 @@
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 warnings
18
+
19
+ import numpy as np
20
+ from sklearn.neighbors import LocalOutlierFactor as sklearn_LocalOutlierFactor
21
+ from sklearn.utils.metaestimators import available_if
22
+ from sklearn.utils.validation import check_is_fitted
23
+
24
+ from daal4py.sklearn._n_jobs_support import control_n_jobs
25
+ from daal4py.sklearn._utils import sklearn_check_version
26
+ from sklearnex._device_offload import dispatch, wrap_output_data
27
+ from sklearnex.neighbors.common import KNeighborsDispatchingBase
28
+ from sklearnex.neighbors.knn_unsupervised import NearestNeighbors
29
+ from sklearnex.utils import get_namespace
30
+
31
+
32
+ @control_n_jobs(decorated_methods=["fit", "_kneighbors"])
33
+ class LocalOutlierFactor(KNeighborsDispatchingBase, sklearn_LocalOutlierFactor):
34
+ __doc__ = (
35
+ sklearn_LocalOutlierFactor.__doc__
36
+ + "\n NOTE: When X=None, methods kneighbors, kneighbors_graph, and predict will"
37
+ + "\n only output numpy arrays. In that case, the only way to offload to gpu"
38
+ + "\n is to use a global queue (e.g. using config_context)"
39
+ )
40
+ if sklearn_check_version("1.2"):
41
+ _parameter_constraints: dict = {
42
+ **sklearn_LocalOutlierFactor._parameter_constraints
43
+ }
44
+
45
+ # Only certain methods should be taken from knn to prevent code
46
+ # duplication. Inheriting would yield a complicated inheritance
47
+ # structure and violate the sklearn inheritance path.
48
+ _save_attributes = NearestNeighbors._save_attributes
49
+ _onedal_knn_fit = NearestNeighbors._onedal_fit
50
+ _onedal_kneighbors = NearestNeighbors._onedal_kneighbors
51
+
52
+ def _onedal_fit(self, X, y, queue=None):
53
+ if sklearn_check_version("1.2"):
54
+ self._validate_params()
55
+
56
+ self._onedal_knn_fit(X, y, queue)
57
+
58
+ if self.contamination != "auto":
59
+ if not (0.0 < self.contamination <= 0.5):
60
+ raise ValueError(
61
+ "contamination must be in (0, 0.5], " "got: %f" % self.contamination
62
+ )
63
+
64
+ n_samples = self.n_samples_fit_
65
+
66
+ if self.n_neighbors > n_samples:
67
+ warnings.warn(
68
+ "n_neighbors (%s) is greater than the "
69
+ "total number of samples (%s). n_neighbors "
70
+ "will be set to (n_samples - 1) for estimation."
71
+ % (self.n_neighbors, n_samples)
72
+ )
73
+ self.n_neighbors_ = max(1, min(self.n_neighbors, n_samples - 1))
74
+
75
+ (
76
+ self._distances_fit_X_,
77
+ _neighbors_indices_fit_X_,
78
+ ) = self._onedal_kneighbors(n_neighbors=self.n_neighbors_, queue=queue)
79
+
80
+ # Sklearn includes a check for float32 at this point which may not be
81
+ # necessary for onedal
82
+
83
+ self._lrd = self._local_reachability_density(
84
+ self._distances_fit_X_, _neighbors_indices_fit_X_
85
+ )
86
+
87
+ # Compute lof score over training samples to define offset_:
88
+ lrd_ratios_array = self._lrd[_neighbors_indices_fit_X_] / self._lrd[:, np.newaxis]
89
+
90
+ self.negative_outlier_factor_ = -np.mean(lrd_ratios_array, axis=1)
91
+
92
+ if self.contamination == "auto":
93
+ # inliers score around -1 (the higher, the less abnormal).
94
+ self.offset_ = -1.5
95
+ else:
96
+ self.offset_ = np.percentile(
97
+ self.negative_outlier_factor_, 100.0 * self.contamination
98
+ )
99
+
100
+ return self
101
+
102
+ def fit(self, X, y=None):
103
+ result = dispatch(
104
+ self,
105
+ "fit",
106
+ {
107
+ "onedal": self.__class__._onedal_fit,
108
+ "sklearn": sklearn_LocalOutlierFactor.fit,
109
+ },
110
+ X,
111
+ None,
112
+ )
113
+ return result
114
+
115
+ def _predict(self, X=None):
116
+ check_is_fitted(self)
117
+
118
+ if X is not None:
119
+ xp, _ = get_namespace(X)
120
+ output = self.decision_function(X) < 0
121
+ is_inlier = xp.ones_like(output, dtype=int)
122
+ is_inlier[output] = -1
123
+ else:
124
+ is_inlier = np.ones(self.n_samples_fit_, dtype=int)
125
+ is_inlier[self.negative_outlier_factor_ < self.offset_] = -1
126
+
127
+ return is_inlier
128
+
129
+ # This had to be done because predict loses the queue when no
130
+ # argument is given and it is a dpctl tensor or dpnp array.
131
+ # This would cause issues in fit_predict. Also, available_if
132
+ # is hard to unwrap, and this is the most straighforward way.
133
+ @available_if(sklearn_LocalOutlierFactor._check_novelty_fit_predict)
134
+ @wrap_output_data
135
+ def fit_predict(self, X, y=None):
136
+ """Fit the model to the training set X and return the labels.
137
+
138
+ **Not available for novelty detection (when novelty is set to True).**
139
+ Label is 1 for an inlier and -1 for an outlier according to the LOF
140
+ score and the contamination parameter.
141
+
142
+ Parameters
143
+ ----------
144
+ X : {array-like, sparse matrix} of shape (n_samples, n_features), default=None
145
+ The query sample or samples to compute the Local Outlier Factor
146
+ w.r.t. the training samples.
147
+
148
+ y : Ignored
149
+ Not used, present for API consistency by convention.
150
+
151
+ Returns
152
+ -------
153
+ is_inlier : ndarray of shape (n_samples,)
154
+ Returns -1 for anomalies/outliers and 1 for inliers.
155
+ """
156
+ return self.fit(X)._predict()
157
+
158
+ def _kneighbors(self, X=None, n_neighbors=None, return_distance=True):
159
+ check_is_fitted(self)
160
+ if sklearn_check_version("1.0") and X is not None:
161
+ self._check_feature_names(X, reset=False)
162
+ return dispatch(
163
+ self,
164
+ "kneighbors",
165
+ {
166
+ "onedal": self.__class__._onedal_kneighbors,
167
+ "sklearn": sklearn_LocalOutlierFactor.kneighbors,
168
+ },
169
+ X,
170
+ n_neighbors=n_neighbors,
171
+ return_distance=return_distance,
172
+ )
173
+
174
+ kneighbors = wrap_output_data(_kneighbors)
175
+
176
+ @available_if(sklearn_LocalOutlierFactor._check_novelty_score_samples)
177
+ @wrap_output_data
178
+ def score_samples(self, X):
179
+ """Opposite of the Local Outlier Factor of X.
180
+
181
+ It is the opposite as bigger is better, i.e. large values correspond
182
+ to inliers.
183
+
184
+ **Only available for novelty detection (when novelty is set to True).**
185
+ The argument X is supposed to contain *new data*: if X contains a
186
+ point from training, it considers the later in its own neighborhood.
187
+ Also, the samples in X are not considered in the neighborhood of any
188
+ point. Because of this, the scores obtained via ``score_samples`` may
189
+ differ from the standard LOF scores.
190
+ The standard LOF scores for the training data is available via the
191
+ ``negative_outlier_factor_`` attribute.
192
+
193
+ Parameters
194
+ ----------
195
+ X : {array-like, sparse matrix} of shape (n_samples, n_features)
196
+ The query sample or samples to compute the Local Outlier Factor
197
+ w.r.t. the training samples.
198
+
199
+ Returns
200
+ -------
201
+ opposite_lof_scores : ndarray of shape (n_samples,)
202
+ The opposite of the Local Outlier Factor of each input samples.
203
+ The lower, the more abnormal.
204
+ """
205
+ check_is_fitted(self)
206
+
207
+ distances_X, neighbors_indices_X = self._kneighbors(
208
+ X, n_neighbors=self.n_neighbors_
209
+ )
210
+
211
+ X_lrd = self._local_reachability_density(
212
+ distances_X,
213
+ neighbors_indices_X,
214
+ )
215
+
216
+ lrd_ratios_array = self._lrd[neighbors_indices_X] / X_lrd[:, np.newaxis]
217
+
218
+ return -np.mean(lrd_ratios_array, axis=1)
219
+
220
+ fit.__doc__ = sklearn_LocalOutlierFactor.fit.__doc__
221
+ kneighbors.__doc__ = sklearn_LocalOutlierFactor.kneighbors.__doc__
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env python
2
1
  # ==============================================================================
3
2
  # Copyright 2023 Intel Corporation
4
3
  #
@@ -138,6 +137,9 @@ class KNeighborsDispatchingBase:
138
137
  self.n_features_in_ = X.data.shape[1]
139
138
 
140
139
  def _onedal_supported(self, device, method_name, *data):
140
+ if method_name == "fit":
141
+ self._fit_validation(data[0], data[1])
142
+
141
143
  class_name = self.__class__.__name__
142
144
  is_classifier = "Classifier" in class_name
143
145
  is_regressor = "Regressor" in class_name
@@ -177,7 +179,7 @@ class KNeighborsDispatchingBase:
177
179
  return patching_status
178
180
 
179
181
  if not patching_status.and_condition(
180
- not sp.isspmatrix(data[0]), "Sparse input is not supported."
182
+ not sp.issparse(data[0]), "Sparse input is not supported."
181
183
  ):
182
184
  return patching_status
183
185
 
@@ -250,7 +252,7 @@ class KNeighborsDispatchingBase:
250
252
  class_count >= 2, "One-class case is not supported."
251
253
  )
252
254
  return patching_status
253
- if method_name in ["predict", "predict_proba", "kneighbors"]:
255
+ if method_name in ["predict", "predict_proba", "kneighbors", "score"]:
254
256
  patching_status.and_condition(
255
257
  hasattr(self, "_onedal_estimator"), "oneDAL model was not trained."
256
258
  )
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env python
2
1
  # ===============================================================================
3
2
  # Copyright 2021 Intel Corporation
4
3
  #
@@ -15,135 +14,30 @@
15
14
  # limitations under the License.
16
15
  # ===============================================================================
17
16
 
18
- import warnings
19
-
20
- from sklearn.neighbors._ball_tree import BallTree
21
- from sklearn.neighbors._base import NeighborsBase as sklearn_NeighborsBase
22
- from sklearn.neighbors._kd_tree import KDTree
23
-
24
- from daal4py.sklearn._utils import sklearn_check_version
25
-
26
- if not sklearn_check_version("1.2"):
27
- from sklearn.neighbors._base import _check_weights
28
-
29
- import numpy as np
30
- from sklearn.neighbors._base import VALID_METRICS
17
+ from sklearn.metrics import accuracy_score
31
18
  from sklearn.neighbors._classification import (
32
19
  KNeighborsClassifier as sklearn_KNeighborsClassifier,
33
20
  )
34
21
  from sklearn.neighbors._unsupervised import NearestNeighbors as sklearn_NearestNeighbors
35
22
  from sklearn.utils.validation import _deprecate_positional_args, check_is_fitted
36
23
 
24
+ from daal4py.sklearn._n_jobs_support import control_n_jobs
25
+ from daal4py.sklearn._utils import sklearn_check_version
37
26
  from onedal.neighbors import KNeighborsClassifier as onedal_KNeighborsClassifier
38
- from onedal.utils import _check_array, _num_features, _num_samples
39
27
 
40
28
  from .._device_offload import dispatch, wrap_output_data
41
29
  from .common import KNeighborsDispatchingBase
42
30
 
43
- if sklearn_check_version("0.24"):
44
-
45
- class KNeighborsClassifier_(sklearn_KNeighborsClassifier):
46
- if sklearn_check_version("1.2"):
47
- _parameter_constraints: dict = {
48
- **sklearn_KNeighborsClassifier._parameter_constraints
49
- }
50
-
51
- @_deprecate_positional_args
52
- def __init__(
53
- self,
54
- n_neighbors=5,
55
- *,
56
- weights="uniform",
57
- algorithm="auto",
58
- leaf_size=30,
59
- p=2,
60
- metric="minkowski",
61
- metric_params=None,
62
- n_jobs=None,
63
- **kwargs,
64
- ):
65
- super().__init__(
66
- n_neighbors=n_neighbors,
67
- algorithm=algorithm,
68
- leaf_size=leaf_size,
69
- metric=metric,
70
- p=p,
71
- metric_params=metric_params,
72
- n_jobs=n_jobs,
73
- **kwargs,
74
- )
75
- self.weights = (
76
- weights if sklearn_check_version("1.0") else _check_weights(weights)
77
- )
78
-
79
- elif sklearn_check_version("0.22"):
80
- from sklearn.neighbors._base import (
81
- SupervisedIntegerMixin as BaseSupervisedIntegerMixin,
82
- )
83
31
 
84
- class KNeighborsClassifier_(sklearn_KNeighborsClassifier, BaseSupervisedIntegerMixin):
85
- @_deprecate_positional_args
86
- def __init__(
87
- self,
88
- n_neighbors=5,
89
- *,
90
- weights="uniform",
91
- algorithm="auto",
92
- leaf_size=30,
93
- p=2,
94
- metric="minkowski",
95
- metric_params=None,
96
- n_jobs=None,
97
- **kwargs,
98
- ):
99
- super().__init__(
100
- n_neighbors=n_neighbors,
101
- algorithm=algorithm,
102
- leaf_size=leaf_size,
103
- metric=metric,
104
- p=p,
105
- metric_params=metric_params,
106
- n_jobs=n_jobs,
107
- **kwargs,
108
- )
109
- self.weights = _check_weights(weights)
110
-
111
- else:
112
- from sklearn.neighbors.base import (
113
- SupervisedIntegerMixin as BaseSupervisedIntegerMixin,
114
- )
115
-
116
- class KNeighborsClassifier_(sklearn_KNeighborsClassifier, BaseSupervisedIntegerMixin):
117
- @_deprecate_positional_args
118
- def __init__(
119
- self,
120
- n_neighbors=5,
121
- *,
122
- weights="uniform",
123
- algorithm="auto",
124
- leaf_size=30,
125
- p=2,
126
- metric="minkowski",
127
- metric_params=None,
128
- n_jobs=None,
129
- **kwargs,
130
- ):
131
- super().__init__(
132
- n_neighbors=n_neighbors,
133
- algorithm=algorithm,
134
- leaf_size=leaf_size,
135
- metric=metric,
136
- p=p,
137
- metric_params=metric_params,
138
- n_jobs=n_jobs,
139
- **kwargs,
140
- )
141
- self.weights = _check_weights(weights)
142
-
143
-
144
- class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
32
+ @control_n_jobs(
33
+ decorated_methods=["fit", "predict", "predict_proba", "kneighbors", "score"]
34
+ )
35
+ class KNeighborsClassifier(sklearn_KNeighborsClassifier, KNeighborsDispatchingBase):
36
+ __doc__ = sklearn_KNeighborsClassifier.__doc__
145
37
  if sklearn_check_version("1.2"):
146
- _parameter_constraints: dict = {**KNeighborsClassifier_._parameter_constraints}
38
+ _parameter_constraints: dict = {
39
+ **sklearn_KNeighborsClassifier._parameter_constraints
40
+ }
147
41
 
148
42
  if sklearn_check_version("1.0"):
149
43
 
@@ -199,7 +93,6 @@ class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
199
93
  )
200
94
 
201
95
  def fit(self, X, y):
202
- self._fit_validation(X, y)
203
96
  dispatch(
204
97
  self,
205
98
  "fit",
@@ -243,10 +136,27 @@ class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
243
136
  )
244
137
 
245
138
  @wrap_output_data
246
- def kneighbors(self, X=None, n_neighbors=None, return_distance=True):
139
+ def score(self, X, y, sample_weight=None):
247
140
  check_is_fitted(self)
248
141
  if sklearn_check_version("1.0"):
249
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
+
155
+ @wrap_output_data
156
+ def kneighbors(self, X=None, n_neighbors=None, return_distance=True):
157
+ check_is_fitted(self)
158
+ if sklearn_check_version("1.0") and X is not None:
159
+ self._check_feature_names(X, reset=False)
250
160
  return dispatch(
251
161
  self,
252
162
  "kneighbors",
@@ -255,8 +165,8 @@ class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
255
165
  "sklearn": sklearn_KNeighborsClassifier.kneighbors,
256
166
  },
257
167
  X,
258
- n_neighbors,
259
- return_distance,
168
+ n_neighbors=n_neighbors,
169
+ return_distance=return_distance,
260
170
  )
261
171
 
262
172
  @wrap_output_data
@@ -270,18 +180,10 @@ class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
270
180
  or getattr(self, "_tree", 0) is None
271
181
  and self._fit_method == "kd_tree"
272
182
  ):
273
- if sklearn_check_version("0.24"):
274
- sklearn_NearestNeighbors.fit(self, self._fit_X, getattr(self, "_y", None))
275
- else:
276
- sklearn_NearestNeighbors.fit(self, self._fit_X)
277
- if sklearn_check_version("0.22"):
278
- result = sklearn_NearestNeighbors.radius_neighbors(
279
- self, X, radius, return_distance, sort_results
280
- )
281
- else:
282
- result = sklearn_NearestNeighbors.radius_neighbors(
283
- self, X, radius, return_distance
284
- )
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
+ )
285
187
 
286
188
  return result
287
189
 
@@ -320,6 +222,11 @@ class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
320
222
  X, n_neighbors, return_distance, queue=queue
321
223
  )
322
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
+
323
230
  def _save_attributes(self):
324
231
  self.classes_ = self._onedal_estimator.classes_
325
232
  self.n_features_in_ = self._onedal_estimator.n_features_in_
@@ -329,3 +236,10 @@ class KNeighborsClassifier(KNeighborsClassifier_, KNeighborsDispatchingBase):
329
236
  self._fit_method = self._onedal_estimator._fit_method
330
237
  self.outputs_2d_ = self._onedal_estimator.outputs_2d_
331
238
  self._tree = self._onedal_estimator._tree
239
+
240
+ fit.__doc__ = sklearn_KNeighborsClassifier.fit.__doc__
241
+ predict.__doc__ = sklearn_KNeighborsClassifier.predict.__doc__
242
+ predict_proba.__doc__ = sklearn_KNeighborsClassifier.predict_proba.__doc__
243
+ score.__doc__ = sklearn_KNeighborsClassifier.score.__doc__
244
+ kneighbors.__doc__ = sklearn_KNeighborsClassifier.kneighbors.__doc__
245
+ radius_neighbors.__doc__ = sklearn_NearestNeighbors.radius_neighbors.__doc__
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env python
2
1
  # ==============================================================================
3
2
  # Copyright 2021 Intel Corporation
4
3
  #
@@ -15,131 +14,27 @@
15
14
  # limitations under the License.
16
15
  # ==============================================================================
17
16
 
18
- import warnings
19
-
20
- from sklearn.neighbors._ball_tree import BallTree
21
- from sklearn.neighbors._base import NeighborsBase as sklearn_NeighborsBase
22
- from sklearn.neighbors._kd_tree import KDTree
23
-
24
- from daal4py.sklearn._utils import sklearn_check_version
25
-
26
- if not sklearn_check_version("1.2"):
27
- from sklearn.neighbors._base import _check_weights
28
-
29
- import numpy as np
30
- from sklearn.neighbors._base import VALID_METRICS
31
17
  from sklearn.neighbors._regression import (
32
18
  KNeighborsRegressor as sklearn_KNeighborsRegressor,
33
19
  )
34
20
  from sklearn.neighbors._unsupervised import NearestNeighbors as sklearn_NearestNeighbors
35
21
  from sklearn.utils.validation import _deprecate_positional_args, check_is_fitted
36
22
 
23
+ from daal4py.sklearn._n_jobs_support import control_n_jobs
24
+ from daal4py.sklearn._utils import sklearn_check_version
37
25
  from onedal.neighbors import KNeighborsRegressor as onedal_KNeighborsRegressor
38
- from onedal.utils import _check_array, _num_features, _num_samples
39
26
 
40
27
  from .._device_offload import dispatch, wrap_output_data
41
28
  from .common import KNeighborsDispatchingBase
42
29
 
43
- if sklearn_check_version("0.24"):
44
-
45
- class KNeighborsRegressor_(sklearn_KNeighborsRegressor):
46
- if sklearn_check_version("1.2"):
47
- _parameter_constraints: dict = {
48
- **sklearn_KNeighborsRegressor._parameter_constraints
49
- }
50
-
51
- @_deprecate_positional_args
52
- def __init__(
53
- self,
54
- n_neighbors=5,
55
- *,
56
- weights="uniform",
57
- algorithm="auto",
58
- leaf_size=30,
59
- p=2,
60
- metric="minkowski",
61
- metric_params=None,
62
- n_jobs=None,
63
- **kwargs,
64
- ):
65
- super().__init__(
66
- n_neighbors=n_neighbors,
67
- algorithm=algorithm,
68
- leaf_size=leaf_size,
69
- metric=metric,
70
- p=p,
71
- metric_params=metric_params,
72
- n_jobs=n_jobs,
73
- **kwargs,
74
- )
75
- self.weights = (
76
- weights if sklearn_check_version("1.0") else _check_weights(weights)
77
- )
78
-
79
- elif sklearn_check_version("0.22"):
80
- from sklearn.neighbors._base import SupervisedFloatMixin as BaseSupervisedFloatMixin
81
-
82
- class KNeighborsRegressor_(sklearn_KNeighborsRegressor, BaseSupervisedFloatMixin):
83
- @_deprecate_positional_args
84
- def __init__(
85
- self,
86
- n_neighbors=5,
87
- *,
88
- weights="uniform",
89
- algorithm="auto",
90
- leaf_size=30,
91
- p=2,
92
- metric="minkowski",
93
- metric_params=None,
94
- n_jobs=None,
95
- **kwargs,
96
- ):
97
- super().__init__(
98
- n_neighbors=n_neighbors,
99
- algorithm=algorithm,
100
- leaf_size=leaf_size,
101
- metric=metric,
102
- p=p,
103
- metric_params=metric_params,
104
- n_jobs=n_jobs,
105
- **kwargs,
106
- )
107
- self.weights = _check_weights(weights)
108
-
109
- else:
110
- from sklearn.neighbors.base import SupervisedFloatMixin as BaseSupervisedFloatMixin
111
-
112
- class KNeighborsRegressor_(sklearn_KNeighborsRegressor, BaseSupervisedFloatMixin):
113
- @_deprecate_positional_args
114
- def __init__(
115
- self,
116
- n_neighbors=5,
117
- *,
118
- weights="uniform",
119
- algorithm="auto",
120
- leaf_size=30,
121
- p=2,
122
- metric="minkowski",
123
- metric_params=None,
124
- n_jobs=None,
125
- **kwargs,
126
- ):
127
- super().__init__(
128
- n_neighbors=n_neighbors,
129
- algorithm=algorithm,
130
- leaf_size=leaf_size,
131
- metric=metric,
132
- p=p,
133
- metric_params=metric_params,
134
- n_jobs=n_jobs,
135
- **kwargs,
136
- )
137
- self.weights = _check_weights(weights)
138
-
139
30
 
140
- class KNeighborsRegressor(KNeighborsRegressor_, KNeighborsDispatchingBase):
31
+ @control_n_jobs(decorated_methods=["fit", "predict", "kneighbors"])
32
+ class KNeighborsRegressor(sklearn_KNeighborsRegressor, KNeighborsDispatchingBase):
33
+ __doc__ = sklearn_KNeighborsRegressor.__doc__
141
34
  if sklearn_check_version("1.2"):
142
- _parameter_constraints: dict = {**KNeighborsRegressor_._parameter_constraints}
35
+ _parameter_constraints: dict = {
36
+ **sklearn_KNeighborsRegressor._parameter_constraints
37
+ }
143
38
 
144
39
  if sklearn_check_version("1.0"):
145
40
 
@@ -195,7 +90,6 @@ class KNeighborsRegressor(KNeighborsRegressor_, KNeighborsDispatchingBase):
195
90
  )
196
91
 
197
92
  def fit(self, X, y):
198
- self._fit_validation(X, y)
199
93
  dispatch(
200
94
  self,
201
95
  "fit",
@@ -226,7 +120,7 @@ class KNeighborsRegressor(KNeighborsRegressor_, KNeighborsDispatchingBase):
226
120
  @wrap_output_data
227
121
  def kneighbors(self, X=None, n_neighbors=None, return_distance=True):
228
122
  check_is_fitted(self)
229
- if sklearn_check_version("1.0"):
123
+ if sklearn_check_version("1.0") and X is not None:
230
124
  self._check_feature_names(X, reset=False)
231
125
  return dispatch(
232
126
  self,
@@ -236,8 +130,8 @@ class KNeighborsRegressor(KNeighborsRegressor_, KNeighborsDispatchingBase):
236
130
  "sklearn": sklearn_KNeighborsRegressor.kneighbors,
237
131
  },
238
132
  X,
239
- n_neighbors,
240
- return_distance,
133
+ n_neighbors=n_neighbors,
134
+ return_distance=return_distance,
241
135
  )
242
136
 
243
137
  @wrap_output_data
@@ -251,18 +145,10 @@ class KNeighborsRegressor(KNeighborsRegressor_, KNeighborsDispatchingBase):
251
145
  or getattr(self, "_tree", 0) is None
252
146
  and self._fit_method == "kd_tree"
253
147
  ):
254
- if sklearn_check_version("0.24"):
255
- sklearn_NearestNeighbors.fit(self, self._fit_X, getattr(self, "_y", None))
256
- else:
257
- sklearn_NearestNeighbors.fit(self, self._fit_X)
258
- if sklearn_check_version("0.22"):
259
- result = sklearn_NearestNeighbors.radius_neighbors(
260
- self, X, radius, return_distance, sort_results
261
- )
262
- else:
263
- result = sklearn_NearestNeighbors.radius_neighbors(
264
- self, X, radius, return_distance
265
- )
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
+ )
266
152
 
267
153
  return result
268
154
 
@@ -305,3 +191,8 @@ class KNeighborsRegressor(KNeighborsRegressor_, KNeighborsDispatchingBase):
305
191
  self._y = self._onedal_estimator._y
306
192
  self._fit_method = self._onedal_estimator._fit_method
307
193
  self._tree = self._onedal_estimator._tree
194
+
195
+ fit.__doc__ = sklearn_KNeighborsRegressor.__doc__
196
+ predict.__doc__ = sklearn_KNeighborsRegressor.predict.__doc__
197
+ kneighbors.__doc__ = sklearn_KNeighborsRegressor.kneighbors.__doc__
198
+ radius_neighbors.__doc__ = sklearn_NearestNeighbors.radius_neighbors.__doc__