scikit-learn-intelex 2024.3.0__py310-none-manylinux1_x86_64.whl → 2024.5.0__py310-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 (43) hide show
  1. {scikit_learn_intelex-2024.3.0.dist-info → scikit_learn_intelex-2024.5.0.dist-info}/METADATA +2 -2
  2. {scikit_learn_intelex-2024.3.0.dist-info → scikit_learn_intelex-2024.5.0.dist-info}/RECORD +43 -37
  3. sklearnex/_device_offload.py +39 -5
  4. sklearnex/basic_statistics/__init__.py +2 -1
  5. sklearnex/basic_statistics/incremental_basic_statistics.py +288 -0
  6. sklearnex/basic_statistics/tests/test_incremental_basic_statistics.py +384 -0
  7. sklearnex/covariance/incremental_covariance.py +217 -30
  8. sklearnex/covariance/tests/test_incremental_covariance.py +54 -17
  9. sklearnex/decomposition/pca.py +71 -19
  10. sklearnex/decomposition/tests/test_pca.py +2 -2
  11. sklearnex/dispatcher.py +33 -2
  12. sklearnex/ensemble/_forest.py +73 -79
  13. sklearnex/linear_model/__init__.py +5 -3
  14. sklearnex/linear_model/incremental_linear.py +387 -0
  15. sklearnex/linear_model/linear.py +275 -340
  16. sklearnex/linear_model/logistic_regression.py +50 -9
  17. sklearnex/linear_model/tests/test_incremental_linear.py +200 -0
  18. sklearnex/linear_model/tests/test_linear.py +40 -5
  19. sklearnex/neighbors/_lof.py +53 -36
  20. sklearnex/neighbors/common.py +4 -1
  21. sklearnex/neighbors/knn_classification.py +37 -122
  22. sklearnex/neighbors/knn_regression.py +10 -117
  23. sklearnex/neighbors/knn_unsupervised.py +6 -78
  24. sklearnex/neighbors/tests/test_neighbors.py +2 -2
  25. sklearnex/preview/cluster/k_means.py +5 -73
  26. sklearnex/preview/covariance/covariance.py +6 -5
  27. sklearnex/preview/covariance/tests/test_covariance.py +18 -5
  28. sklearnex/svm/_common.py +4 -7
  29. sklearnex/svm/nusvc.py +66 -50
  30. sklearnex/svm/nusvr.py +3 -49
  31. sklearnex/svm/svc.py +66 -51
  32. sklearnex/svm/svr.py +3 -49
  33. sklearnex/tests/_utils.py +34 -16
  34. sklearnex/tests/test_memory_usage.py +5 -1
  35. sklearnex/tests/test_n_jobs_support.py +12 -2
  36. sklearnex/tests/test_patching.py +87 -58
  37. sklearnex/tests/test_run_to_run_stability_tests.py +1 -1
  38. sklearnex/utils/__init__.py +2 -1
  39. sklearnex/utils/_namespace.py +97 -0
  40. sklearnex/utils/tests/test_finite.py +89 -0
  41. {scikit_learn_intelex-2024.3.0.dist-info → scikit_learn_intelex-2024.5.0.dist-info}/LICENSE.txt +0 -0
  42. {scikit_learn_intelex-2024.3.0.dist-info → scikit_learn_intelex-2024.5.0.dist-info}/WHEEL +0 -0
  43. {scikit_learn_intelex-2024.3.0.dist-info → scikit_learn_intelex-2024.5.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,387 @@
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 numbers
18
+ import warnings
19
+
20
+ import numpy as np
21
+ from sklearn.base import BaseEstimator, MultiOutputMixin, RegressorMixin
22
+ from sklearn.exceptions import NotFittedError
23
+ from sklearn.utils import check_array, gen_batches
24
+
25
+ from daal4py.sklearn._n_jobs_support import control_n_jobs
26
+ from daal4py.sklearn._utils import sklearn_check_version
27
+ from onedal.linear_model import (
28
+ IncrementalLinearRegression as onedal_IncrementalLinearRegression,
29
+ )
30
+
31
+ if sklearn_check_version("1.2"):
32
+ from sklearn.utils._param_validation import Interval
33
+
34
+ from onedal.common.hyperparameters import get_hyperparameters
35
+
36
+ from .._device_offload import dispatch, wrap_output_data
37
+ from .._utils import PatchingConditionsChain, register_hyperparameters
38
+
39
+
40
+ @register_hyperparameters(
41
+ {
42
+ "fit": get_hyperparameters("linear_regression", "train"),
43
+ "partial_fit": get_hyperparameters("linear_regression", "train"),
44
+ }
45
+ )
46
+ @control_n_jobs(
47
+ decorated_methods=["fit", "partial_fit", "predict", "_onedal_finalize_fit"]
48
+ )
49
+ class IncrementalLinearRegression(MultiOutputMixin, RegressorMixin, BaseEstimator):
50
+ """
51
+ Incremental estimator for linear regression.
52
+ Allows to train linear regression if data are splitted into batches.
53
+
54
+ Parameters
55
+ ----------
56
+ fit_intercept : bool, default=True
57
+ Whether to calculate the intercept for this model. If set
58
+ to False, no intercept will be used in calculations
59
+ (i.e. data is expected to be centered).
60
+
61
+ copy_X : bool, default=True
62
+ If True, X will be copied; else, it may be overwritten.
63
+
64
+ n_jobs : int, default=None
65
+ The number of jobs to use for the computation.
66
+
67
+ batch_size : int, default=None
68
+ The number of samples to use for each batch. Only used when calling
69
+ ``fit``. If ``batch_size`` is ``None``, then ``batch_size``
70
+ is inferred from the data and set to ``5 * n_features``, to provide a
71
+ balance between approximation accuracy and memory consumption.
72
+
73
+ Attributes
74
+ ----------
75
+ coef_ : array of shape (n_features, ) or (n_targets, n_features)
76
+ Estimated coefficients for the linear regression problem.
77
+ If multiple targets are passed during the fit (y 2D), this
78
+ is a 2D array of shape (n_targets, n_features), while if only
79
+ one target is passed, this is a 1D array of length n_features.
80
+
81
+ intercept_ : float or array of shape (n_targets,)
82
+ Independent term in the linear model. Set to 0.0 if
83
+ `fit_intercept = False`.
84
+
85
+ n_features_in_ : int
86
+ Number of features seen during :term:`fit`.
87
+
88
+ n_samples_seen_ : int
89
+ The number of samples processed by the estimator. Will be reset on
90
+ new calls to fit, but increments across ``partial_fit`` calls.
91
+ It should be not less than `n_features_in_` if `fit_intercept`
92
+ is False and not less than `n_features_in_` + 1 if `fit_intercept`
93
+ is True to obtain regression coefficients.
94
+
95
+ batch_size_ : int
96
+ Inferred batch size from ``batch_size``.
97
+
98
+ n_features_in_ : int
99
+ Number of features seen during :term:`fit` `partial_fit`.
100
+
101
+ """
102
+
103
+ _onedal_incremental_linear = staticmethod(onedal_IncrementalLinearRegression)
104
+
105
+ if sklearn_check_version("1.2"):
106
+ _parameter_constraints: dict = {
107
+ "fit_intercept": ["boolean"],
108
+ "copy_X": ["boolean"],
109
+ "n_jobs": [Interval(numbers.Integral, -1, None, closed="left"), None],
110
+ "batch_size": [Interval(numbers.Integral, 1, None, closed="left"), None],
111
+ }
112
+
113
+ def __init__(self, *, fit_intercept=True, copy_X=True, n_jobs=None, batch_size=None):
114
+ self.fit_intercept = fit_intercept
115
+ self.copy_X = copy_X
116
+ self.n_jobs = n_jobs
117
+ self.batch_size = batch_size
118
+
119
+ def _onedal_supported(self, method_name, *data):
120
+ patching_status = PatchingConditionsChain(
121
+ f"sklearn.linear_model.{self.__class__.__name__}.{method_name}"
122
+ )
123
+ return patching_status
124
+
125
+ _onedal_cpu_supported = _onedal_supported
126
+ _onedal_gpu_supported = _onedal_supported
127
+
128
+ def _onedal_predict(self, X, queue=None):
129
+ if sklearn_check_version("1.2"):
130
+ self._validate_params()
131
+
132
+ if sklearn_check_version("1.0"):
133
+ X = self._validate_data(
134
+ X,
135
+ dtype=[np.float64, np.float32],
136
+ copy=self.copy_X,
137
+ )
138
+ else:
139
+ X = check_array(
140
+ X,
141
+ dtype=[np.float64, np.float32],
142
+ copy=self.copy_X,
143
+ )
144
+
145
+ assert hasattr(self, "_onedal_estimator")
146
+ if self._need_to_finalize:
147
+ self._onedal_finalize_fit()
148
+ return self._onedal_estimator.predict(X, queue)
149
+
150
+ def _onedal_partial_fit(self, X, y, queue=None):
151
+ first_pass = not hasattr(self, "n_samples_seen_") or self.n_samples_seen_ == 0
152
+
153
+ if sklearn_check_version("1.2"):
154
+ self._validate_params()
155
+
156
+ if sklearn_check_version("1.0"):
157
+ X, y = self._validate_data(
158
+ X,
159
+ y,
160
+ dtype=[np.float64, np.float32],
161
+ reset=first_pass,
162
+ copy=self.copy_X,
163
+ multi_output=True,
164
+ )
165
+ else:
166
+ X = check_array(
167
+ X,
168
+ dtype=[np.float64, np.float32],
169
+ copy=self.copy_X,
170
+ )
171
+ y = check_array(
172
+ y,
173
+ dtype=[np.float64, np.float32],
174
+ copy=False,
175
+ ensure_2d=False,
176
+ )
177
+
178
+ if first_pass:
179
+ self.n_samples_seen_ = X.shape[0]
180
+ self.n_features_in_ = X.shape[1]
181
+ else:
182
+ self.n_samples_seen_ += X.shape[0]
183
+ onedal_params = {"fit_intercept": self.fit_intercept, "copy_X": self.copy_X}
184
+ if not hasattr(self, "_onedal_estimator"):
185
+ self._onedal_estimator = self._onedal_incremental_linear(**onedal_params)
186
+ self._onedal_estimator.partial_fit(X, y, queue)
187
+ self._need_to_finalize = True
188
+
189
+ def _onedal_finalize_fit(self):
190
+ assert hasattr(self, "_onedal_estimator")
191
+ is_underdetermined = self.n_samples_seen_ < self.n_features_in_ + int(
192
+ self.fit_intercept
193
+ )
194
+ if is_underdetermined:
195
+ raise ValueError("Not enough samples to finalize")
196
+ self._onedal_estimator.finalize_fit()
197
+ self._need_to_finalize = False
198
+
199
+ def _onedal_fit(self, X, y, queue=None):
200
+ if sklearn_check_version("1.2"):
201
+ self._validate_params()
202
+
203
+ if sklearn_check_version("1.0"):
204
+ X, y = self._validate_data(
205
+ X, y, dtype=[np.float64, np.float32], copy=self.copy_X, multi_output=True
206
+ )
207
+ else:
208
+ X = check_array(
209
+ X,
210
+ dtype=[np.float64, np.float32],
211
+ copy=self.copy_X,
212
+ )
213
+ y = check_array(
214
+ y,
215
+ dtype=[np.float64, np.float32],
216
+ copy=False,
217
+ ensure_2d=False,
218
+ )
219
+
220
+ n_samples, n_features = X.shape
221
+
222
+ is_underdetermined = n_samples < n_features + int(self.fit_intercept)
223
+ if is_underdetermined:
224
+ raise ValueError("Not enough samples to run oneDAL backend")
225
+
226
+ if self.batch_size is None:
227
+ self.batch_size_ = 5 * n_features
228
+ else:
229
+ self.batch_size_ = self.batch_size
230
+
231
+ self.n_samples_seen_ = 0
232
+ if hasattr(self, "_onedal_estimator"):
233
+ self._onedal_estimator._reset()
234
+
235
+ for batch in gen_batches(n_samples, self.batch_size_):
236
+ X_batch, y_batch = X[batch], y[batch]
237
+ self._onedal_partial_fit(X_batch, y_batch, queue=queue)
238
+
239
+ if sklearn_check_version("1.2"):
240
+ self._validate_params()
241
+
242
+ # finite check occurs on onedal side
243
+ self.n_features_in_ = n_features
244
+
245
+ if n_samples == 1:
246
+ warnings.warn(
247
+ "Only one sample available. You may want to reshape your data array"
248
+ )
249
+
250
+ self._onedal_finalize_fit()
251
+
252
+ return self
253
+
254
+ def get_intercept_(self):
255
+ if hasattr(self, "_onedal_estimator"):
256
+ if self._need_to_finalize:
257
+ self._onedal_finalize_fit()
258
+
259
+ return self._onedal_estimator.intercept_
260
+ else:
261
+ raise AttributeError(
262
+ f"'{self.__class__.__name__}' object has no attribute 'intercept_'"
263
+ )
264
+
265
+ def set_intercept_(self, value):
266
+ self.__dict__["intercept_"] = value
267
+ if hasattr(self, "_onedal_estimator"):
268
+ self._onedal_estimator.intercept_ = value
269
+ del self._onedal_estimator._onedal_model
270
+
271
+ def get_coef_(self):
272
+ if hasattr(self, "_onedal_estimator"):
273
+ if self._need_to_finalize:
274
+ self._onedal_finalize_fit()
275
+
276
+ return self._onedal_estimator.coef_
277
+ else:
278
+ raise AttributeError(
279
+ f"'{self.__class__.__name__}' object has no attribute 'coef_'"
280
+ )
281
+
282
+ def set_coef_(self, value):
283
+ self.__dict__["coef_"] = value
284
+ if hasattr(self, "_onedal_estimator"):
285
+ self._onedal_estimator.coef_ = value
286
+ del self._onedal_estimator._onedal_model
287
+
288
+ coef_ = property(get_coef_, set_coef_)
289
+ intercept_ = property(get_intercept_, set_intercept_)
290
+
291
+ def partial_fit(self, X, y):
292
+ """
293
+ Incremental fit linear model with X and y. All of X and y is
294
+ processed as a single batch.
295
+
296
+ Parameters
297
+ ----------
298
+ X : array-like of shape (n_samples, n_features)
299
+ Training data, where `n_samples` is the number of samples and
300
+ `n_features` is the number of features.
301
+
302
+ y : array-like of shape (n_samples,) or (n_samples, n_targets)
303
+ Target values, where `n_samples` is the number of samples and
304
+ `n_targets` is the number of targets.
305
+
306
+ Returns
307
+ -------
308
+ self : object
309
+ Returns the instance itself.
310
+ """
311
+
312
+ dispatch(
313
+ self,
314
+ "partial_fit",
315
+ {
316
+ "onedal": self.__class__._onedal_partial_fit,
317
+ "sklearn": None,
318
+ },
319
+ X,
320
+ y,
321
+ )
322
+ return self
323
+
324
+ def fit(self, X, y):
325
+ """
326
+ Fit the model with X and y, using minibatches of size batch_size.
327
+
328
+ Parameters
329
+ ----------
330
+ X : array-like of shape (n_samples, n_features)
331
+ Training data, where `n_samples` is the number of samples and
332
+ `n_features` is the number of features. It is necessary for
333
+ `n_samples` to be not less than `n_features` if `fit_intercept`
334
+ is False and not less than `n_features` + 1 if `fit_intercept`
335
+ is True
336
+
337
+ y : array-like of shape (n_samples,) or (n_samples, n_targets)
338
+ Target values, where `n_samples` is the number of samples and
339
+ `n_targets` is the number of targets.
340
+
341
+ Returns
342
+ -------
343
+ self : object
344
+ Returns the instance itself.
345
+ """
346
+
347
+ dispatch(
348
+ self,
349
+ "fit",
350
+ {
351
+ "onedal": self.__class__._onedal_fit,
352
+ "sklearn": None,
353
+ },
354
+ X,
355
+ y,
356
+ )
357
+ return self
358
+
359
+ @wrap_output_data
360
+ def predict(self, X, y=None):
361
+ """
362
+ Predict using the linear model.
363
+ Parameters
364
+ ----------
365
+ X : array-like or sparse matrix, shape (n_samples, n_features)
366
+ Samples.
367
+ Returns
368
+ -------
369
+ C : array, shape (n_samples, n_targets)
370
+ Returns predicted values.
371
+ """
372
+ if not hasattr(self, "coef_"):
373
+ msg = (
374
+ "This %(name)s instance is not fitted yet. Call 'fit' or 'partial_fit' "
375
+ "with appropriate arguments before using this estimator."
376
+ )
377
+ raise NotFittedError(msg % {"name": self.__class__.__name__})
378
+
379
+ return dispatch(
380
+ self,
381
+ "predict",
382
+ {
383
+ "onedal": self.__class__._onedal_predict,
384
+ "sklearn": None,
385
+ },
386
+ X,
387
+ )