scikit-learn-intelex 2025.4.0__py313-none-manylinux_2_28_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.
- daal4py/__init__.py +73 -0
- daal4py/__main__.py +58 -0
- daal4py/_daal4py.cpython-313-x86_64-linux-gnu.so +0 -0
- daal4py/doc/third-party-programs.txt +424 -0
- daal4py/mb/__init__.py +19 -0
- daal4py/mb/model_builders.py +377 -0
- daal4py/mpi_transceiver.cpython-313-x86_64-linux-gnu.so +0 -0
- daal4py/sklearn/__init__.py +40 -0
- daal4py/sklearn/_n_jobs_support.py +248 -0
- daal4py/sklearn/_utils.py +245 -0
- daal4py/sklearn/cluster/__init__.py +20 -0
- daal4py/sklearn/cluster/dbscan.py +165 -0
- daal4py/sklearn/cluster/k_means.py +597 -0
- daal4py/sklearn/cluster/tests/test_dbscan.py +109 -0
- daal4py/sklearn/decomposition/__init__.py +19 -0
- daal4py/sklearn/decomposition/_pca.py +524 -0
- daal4py/sklearn/ensemble/AdaBoostClassifier.py +196 -0
- daal4py/sklearn/ensemble/GBTDAAL.py +337 -0
- daal4py/sklearn/ensemble/__init__.py +27 -0
- daal4py/sklearn/ensemble/_forest.py +1397 -0
- daal4py/sklearn/ensemble/tests/test_decision_forest.py +206 -0
- daal4py/sklearn/linear_model/__init__.py +29 -0
- daal4py/sklearn/linear_model/_coordinate_descent.py +848 -0
- daal4py/sklearn/linear_model/_linear.py +272 -0
- daal4py/sklearn/linear_model/_ridge.py +325 -0
- daal4py/sklearn/linear_model/coordinate_descent.py +17 -0
- daal4py/sklearn/linear_model/linear.py +17 -0
- daal4py/sklearn/linear_model/logistic_loss.py +195 -0
- daal4py/sklearn/linear_model/logistic_path.py +1026 -0
- daal4py/sklearn/linear_model/ridge.py +17 -0
- daal4py/sklearn/linear_model/tests/test_linear.py +208 -0
- daal4py/sklearn/linear_model/tests/test_ridge.py +69 -0
- daal4py/sklearn/manifold/__init__.py +19 -0
- daal4py/sklearn/manifold/_t_sne.py +405 -0
- daal4py/sklearn/metrics/__init__.py +20 -0
- daal4py/sklearn/metrics/_pairwise.py +236 -0
- daal4py/sklearn/metrics/_ranking.py +210 -0
- daal4py/sklearn/model_selection/__init__.py +19 -0
- daal4py/sklearn/model_selection/_split.py +309 -0
- daal4py/sklearn/model_selection/tests/test_split.py +56 -0
- daal4py/sklearn/monkeypatch/__init__.py +0 -0
- daal4py/sklearn/monkeypatch/dispatcher.py +232 -0
- daal4py/sklearn/monkeypatch/tests/_models_info.py +161 -0
- daal4py/sklearn/monkeypatch/tests/test_monkeypatch.py +71 -0
- daal4py/sklearn/monkeypatch/tests/test_patching.py +90 -0
- daal4py/sklearn/monkeypatch/tests/utils/_launch_algorithms.py +117 -0
- daal4py/sklearn/neighbors/__init__.py +21 -0
- daal4py/sklearn/neighbors/_base.py +503 -0
- daal4py/sklearn/neighbors/_classification.py +139 -0
- daal4py/sklearn/neighbors/_regression.py +74 -0
- daal4py/sklearn/neighbors/_unsupervised.py +55 -0
- daal4py/sklearn/neighbors/tests/test_kneighbors.py +113 -0
- daal4py/sklearn/svm/__init__.py +19 -0
- daal4py/sklearn/svm/svm.py +734 -0
- daal4py/sklearn/utils/__init__.py +21 -0
- daal4py/sklearn/utils/base.py +75 -0
- daal4py/sklearn/utils/tests/test_utils.py +51 -0
- daal4py/sklearn/utils/validation.py +696 -0
- onedal/__init__.py +83 -0
- onedal/_config.py +54 -0
- onedal/_device_offload.py +204 -0
- onedal/_onedal_py_dpc.cpython-313-x86_64-linux-gnu.so +0 -0
- onedal/_onedal_py_host.cpython-313-x86_64-linux-gnu.so +0 -0
- onedal/_onedal_py_spmd_dpc.cpython-313-x86_64-linux-gnu.so +0 -0
- onedal/basic_statistics/__init__.py +20 -0
- onedal/basic_statistics/basic_statistics.py +107 -0
- onedal/basic_statistics/incremental_basic_statistics.py +175 -0
- onedal/basic_statistics/tests/test_basic_statistics.py +242 -0
- onedal/basic_statistics/tests/test_incremental_basic_statistics.py +279 -0
- onedal/basic_statistics/tests/utils.py +50 -0
- onedal/cluster/__init__.py +27 -0
- onedal/cluster/dbscan.py +105 -0
- onedal/cluster/kmeans.py +557 -0
- onedal/cluster/kmeans_init.py +112 -0
- onedal/cluster/tests/test_dbscan.py +125 -0
- onedal/cluster/tests/test_kmeans.py +88 -0
- onedal/cluster/tests/test_kmeans_init.py +93 -0
- onedal/common/_base.py +38 -0
- onedal/common/_estimator_checks.py +47 -0
- onedal/common/_mixin.py +62 -0
- onedal/common/_policy.py +55 -0
- onedal/common/_spmd_policy.py +30 -0
- onedal/common/hyperparameters.py +125 -0
- onedal/common/tests/test_policy.py +76 -0
- onedal/common/tests/test_sycl.py +128 -0
- onedal/covariance/__init__.py +20 -0
- onedal/covariance/covariance.py +122 -0
- onedal/covariance/incremental_covariance.py +161 -0
- onedal/covariance/tests/test_covariance.py +50 -0
- onedal/covariance/tests/test_incremental_covariance.py +190 -0
- onedal/datatypes/__init__.py +19 -0
- onedal/datatypes/_data_conversion.py +121 -0
- onedal/datatypes/tests/common.py +126 -0
- onedal/datatypes/tests/test_data.py +475 -0
- onedal/decomposition/__init__.py +20 -0
- onedal/decomposition/incremental_pca.py +214 -0
- onedal/decomposition/pca.py +186 -0
- onedal/decomposition/tests/test_incremental_pca.py +285 -0
- onedal/ensemble/__init__.py +29 -0
- onedal/ensemble/forest.py +736 -0
- onedal/ensemble/tests/test_random_forest.py +97 -0
- onedal/linear_model/__init__.py +27 -0
- onedal/linear_model/incremental_linear_model.py +292 -0
- onedal/linear_model/linear_model.py +325 -0
- onedal/linear_model/logistic_regression.py +247 -0
- onedal/linear_model/tests/test_incremental_linear_regression.py +213 -0
- onedal/linear_model/tests/test_incremental_ridge_regression.py +171 -0
- onedal/linear_model/tests/test_linear_regression.py +259 -0
- onedal/linear_model/tests/test_logistic_regression.py +95 -0
- onedal/linear_model/tests/test_ridge.py +95 -0
- onedal/neighbors/__init__.py +19 -0
- onedal/neighbors/neighbors.py +763 -0
- onedal/neighbors/tests/test_knn_classification.py +49 -0
- onedal/primitives/__init__.py +27 -0
- onedal/primitives/get_tree.py +25 -0
- onedal/primitives/kernel_functions.py +152 -0
- onedal/primitives/tests/test_kernel_functions.py +159 -0
- onedal/spmd/__init__.py +25 -0
- onedal/spmd/_base.py +30 -0
- onedal/spmd/basic_statistics/__init__.py +20 -0
- onedal/spmd/basic_statistics/basic_statistics.py +30 -0
- onedal/spmd/basic_statistics/incremental_basic_statistics.py +71 -0
- onedal/spmd/cluster/__init__.py +28 -0
- onedal/spmd/cluster/dbscan.py +23 -0
- onedal/spmd/cluster/kmeans.py +56 -0
- onedal/spmd/covariance/__init__.py +20 -0
- onedal/spmd/covariance/covariance.py +26 -0
- onedal/spmd/covariance/incremental_covariance.py +83 -0
- onedal/spmd/decomposition/__init__.py +20 -0
- onedal/spmd/decomposition/incremental_pca.py +124 -0
- onedal/spmd/decomposition/pca.py +26 -0
- onedal/spmd/ensemble/__init__.py +19 -0
- onedal/spmd/ensemble/forest.py +28 -0
- onedal/spmd/linear_model/__init__.py +21 -0
- onedal/spmd/linear_model/incremental_linear_model.py +101 -0
- onedal/spmd/linear_model/linear_model.py +30 -0
- onedal/spmd/linear_model/logistic_regression.py +38 -0
- onedal/spmd/neighbors/__init__.py +19 -0
- onedal/spmd/neighbors/neighbors.py +75 -0
- onedal/svm/__init__.py +19 -0
- onedal/svm/svm.py +556 -0
- onedal/svm/tests/test_csr_svm.py +351 -0
- onedal/svm/tests/test_nusvc.py +204 -0
- onedal/svm/tests/test_nusvr.py +210 -0
- onedal/svm/tests/test_svc.py +176 -0
- onedal/svm/tests/test_svr.py +243 -0
- onedal/tests/test_common.py +57 -0
- onedal/tests/utils/_dataframes_support.py +162 -0
- onedal/tests/utils/_device_selection.py +102 -0
- onedal/utils/__init__.py +49 -0
- onedal/utils/_array_api.py +81 -0
- onedal/utils/_dpep_helpers.py +56 -0
- onedal/utils/tests/test_validation.py +142 -0
- onedal/utils/validation.py +464 -0
- scikit_learn_intelex-2025.4.0.dist-info/LICENSE.txt +202 -0
- scikit_learn_intelex-2025.4.0.dist-info/METADATA +190 -0
- scikit_learn_intelex-2025.4.0.dist-info/RECORD +282 -0
- scikit_learn_intelex-2025.4.0.dist-info/WHEEL +5 -0
- scikit_learn_intelex-2025.4.0.dist-info/top_level.txt +3 -0
- sklearnex/__init__.py +66 -0
- sklearnex/__main__.py +58 -0
- sklearnex/_config.py +116 -0
- sklearnex/_device_offload.py +126 -0
- sklearnex/_utils.py +177 -0
- sklearnex/basic_statistics/__init__.py +20 -0
- sklearnex/basic_statistics/basic_statistics.py +261 -0
- sklearnex/basic_statistics/incremental_basic_statistics.py +352 -0
- sklearnex/basic_statistics/tests/test_basic_statistics.py +405 -0
- sklearnex/basic_statistics/tests/test_incremental_basic_statistics.py +455 -0
- sklearnex/cluster/__init__.py +20 -0
- sklearnex/cluster/dbscan.py +197 -0
- sklearnex/cluster/k_means.py +397 -0
- sklearnex/cluster/tests/test_dbscan.py +38 -0
- sklearnex/cluster/tests/test_kmeans.py +157 -0
- sklearnex/conftest.py +82 -0
- sklearnex/covariance/__init__.py +19 -0
- sklearnex/covariance/incremental_covariance.py +405 -0
- sklearnex/covariance/tests/test_incremental_covariance.py +287 -0
- sklearnex/decomposition/__init__.py +19 -0
- sklearnex/decomposition/pca.py +427 -0
- sklearnex/decomposition/tests/test_pca.py +58 -0
- sklearnex/dispatcher.py +534 -0
- sklearnex/doc/third-party-programs.txt +424 -0
- sklearnex/ensemble/__init__.py +29 -0
- sklearnex/ensemble/_forest.py +2029 -0
- sklearnex/ensemble/tests/test_forest.py +140 -0
- sklearnex/glob/__main__.py +72 -0
- sklearnex/glob/dispatcher.py +101 -0
- sklearnex/linear_model/__init__.py +32 -0
- sklearnex/linear_model/coordinate_descent.py +30 -0
- sklearnex/linear_model/incremental_linear.py +495 -0
- sklearnex/linear_model/incremental_ridge.py +432 -0
- sklearnex/linear_model/linear.py +346 -0
- sklearnex/linear_model/logistic_regression.py +415 -0
- sklearnex/linear_model/ridge.py +390 -0
- sklearnex/linear_model/tests/test_incremental_linear.py +267 -0
- sklearnex/linear_model/tests/test_incremental_ridge.py +214 -0
- sklearnex/linear_model/tests/test_linear.py +142 -0
- sklearnex/linear_model/tests/test_logreg.py +134 -0
- sklearnex/linear_model/tests/test_ridge.py +256 -0
- sklearnex/manifold/__init__.py +19 -0
- sklearnex/manifold/t_sne.py +26 -0
- sklearnex/manifold/tests/test_tsne.py +250 -0
- sklearnex/metrics/__init__.py +23 -0
- sklearnex/metrics/pairwise.py +22 -0
- sklearnex/metrics/ranking.py +20 -0
- sklearnex/metrics/tests/test_metrics.py +39 -0
- sklearnex/model_selection/__init__.py +21 -0
- sklearnex/model_selection/split.py +22 -0
- sklearnex/model_selection/tests/test_model_selection.py +34 -0
- sklearnex/neighbors/__init__.py +27 -0
- sklearnex/neighbors/_lof.py +236 -0
- sklearnex/neighbors/common.py +310 -0
- sklearnex/neighbors/knn_classification.py +231 -0
- sklearnex/neighbors/knn_regression.py +207 -0
- sklearnex/neighbors/knn_unsupervised.py +178 -0
- sklearnex/neighbors/tests/test_neighbors.py +82 -0
- sklearnex/preview/__init__.py +17 -0
- sklearnex/preview/covariance/__init__.py +19 -0
- sklearnex/preview/covariance/covariance.py +142 -0
- sklearnex/preview/covariance/tests/test_covariance.py +66 -0
- sklearnex/preview/decomposition/__init__.py +19 -0
- sklearnex/preview/decomposition/incremental_pca.py +244 -0
- sklearnex/preview/decomposition/tests/test_incremental_pca.py +336 -0
- sklearnex/spmd/__init__.py +25 -0
- sklearnex/spmd/basic_statistics/__init__.py +20 -0
- sklearnex/spmd/basic_statistics/basic_statistics.py +21 -0
- sklearnex/spmd/basic_statistics/incremental_basic_statistics.py +30 -0
- sklearnex/spmd/basic_statistics/tests/test_basic_statistics_spmd.py +107 -0
- sklearnex/spmd/basic_statistics/tests/test_incremental_basic_statistics_spmd.py +306 -0
- sklearnex/spmd/cluster/__init__.py +30 -0
- sklearnex/spmd/cluster/dbscan.py +50 -0
- sklearnex/spmd/cluster/kmeans.py +21 -0
- sklearnex/spmd/cluster/tests/test_dbscan_spmd.py +97 -0
- sklearnex/spmd/cluster/tests/test_kmeans_spmd.py +173 -0
- sklearnex/spmd/covariance/__init__.py +20 -0
- sklearnex/spmd/covariance/covariance.py +21 -0
- sklearnex/spmd/covariance/incremental_covariance.py +37 -0
- sklearnex/spmd/covariance/tests/test_covariance_spmd.py +107 -0
- sklearnex/spmd/covariance/tests/test_incremental_covariance_spmd.py +184 -0
- sklearnex/spmd/decomposition/__init__.py +20 -0
- sklearnex/spmd/decomposition/incremental_pca.py +30 -0
- sklearnex/spmd/decomposition/pca.py +21 -0
- sklearnex/spmd/decomposition/tests/test_incremental_pca_spmd.py +269 -0
- sklearnex/spmd/decomposition/tests/test_pca_spmd.py +128 -0
- sklearnex/spmd/ensemble/__init__.py +19 -0
- sklearnex/spmd/ensemble/forest.py +71 -0
- sklearnex/spmd/ensemble/tests/test_forest_spmd.py +265 -0
- sklearnex/spmd/linear_model/__init__.py +21 -0
- sklearnex/spmd/linear_model/incremental_linear_model.py +35 -0
- sklearnex/spmd/linear_model/linear_model.py +21 -0
- sklearnex/spmd/linear_model/logistic_regression.py +21 -0
- sklearnex/spmd/linear_model/tests/test_incremental_linear_spmd.py +331 -0
- sklearnex/spmd/linear_model/tests/test_linear_regression_spmd.py +145 -0
- sklearnex/spmd/linear_model/tests/test_logistic_regression_spmd.py +162 -0
- sklearnex/spmd/neighbors/__init__.py +19 -0
- sklearnex/spmd/neighbors/neighbors.py +25 -0
- sklearnex/spmd/neighbors/tests/test_neighbors_spmd.py +288 -0
- sklearnex/svm/__init__.py +29 -0
- sklearnex/svm/_common.py +339 -0
- sklearnex/svm/nusvc.py +371 -0
- sklearnex/svm/nusvr.py +170 -0
- sklearnex/svm/svc.py +399 -0
- sklearnex/svm/svr.py +167 -0
- sklearnex/svm/tests/test_svm.py +93 -0
- sklearnex/tests/test_common.py +491 -0
- sklearnex/tests/test_config.py +123 -0
- sklearnex/tests/test_hyperparameters.py +43 -0
- sklearnex/tests/test_memory_usage.py +347 -0
- sklearnex/tests/test_monkeypatch.py +269 -0
- sklearnex/tests/test_n_jobs_support.py +108 -0
- sklearnex/tests/test_parallel.py +48 -0
- sklearnex/tests/test_patching.py +377 -0
- sklearnex/tests/test_run_to_run_stability.py +326 -0
- sklearnex/tests/utils/__init__.py +48 -0
- sklearnex/tests/utils/base.py +436 -0
- sklearnex/tests/utils/spmd.py +198 -0
- sklearnex/utils/__init__.py +19 -0
- sklearnex/utils/_array_api.py +82 -0
- sklearnex/utils/parallel.py +59 -0
- sklearnex/utils/tests/test_validation.py +238 -0
- sklearnex/utils/validation.py +208 -0
|
@@ -0,0 +1,390 @@
|
|
|
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 logging
|
|
18
|
+
|
|
19
|
+
from daal4py.sklearn._utils import daal_check_version, sklearn_check_version
|
|
20
|
+
|
|
21
|
+
if daal_check_version((2024, "P", 600)):
|
|
22
|
+
import numbers
|
|
23
|
+
|
|
24
|
+
import numpy as np
|
|
25
|
+
from scipy.sparse import issparse
|
|
26
|
+
from sklearn.linear_model import Ridge as _sklearn_Ridge
|
|
27
|
+
from sklearn.metrics import r2_score
|
|
28
|
+
from sklearn.utils.validation import check_is_fitted
|
|
29
|
+
|
|
30
|
+
from daal4py.sklearn._n_jobs_support import control_n_jobs
|
|
31
|
+
|
|
32
|
+
if not sklearn_check_version("1.2"):
|
|
33
|
+
from sklearn.linear_model._base import _deprecate_normalize
|
|
34
|
+
if sklearn_check_version("1.1") and not sklearn_check_version("1.2"):
|
|
35
|
+
from sklearn.utils import check_scalar
|
|
36
|
+
|
|
37
|
+
from onedal.linear_model import Ridge as onedal_Ridge
|
|
38
|
+
from onedal.utils import _num_features, _num_samples
|
|
39
|
+
|
|
40
|
+
from .._device_offload import dispatch, wrap_output_data
|
|
41
|
+
from .._utils import PatchableEstimator, PatchingConditionsChain
|
|
42
|
+
|
|
43
|
+
if sklearn_check_version("1.6"):
|
|
44
|
+
from sklearn.utils.validation import validate_data
|
|
45
|
+
else:
|
|
46
|
+
validate_data = _sklearn_Ridge._validate_data
|
|
47
|
+
|
|
48
|
+
@control_n_jobs(decorated_methods=["fit", "predict", "score"])
|
|
49
|
+
class Ridge(PatchableEstimator, _sklearn_Ridge):
|
|
50
|
+
__doc__ = _sklearn_Ridge.__doc__
|
|
51
|
+
|
|
52
|
+
if sklearn_check_version("1.2"):
|
|
53
|
+
_parameter_constraints: dict = {**_sklearn_Ridge._parameter_constraints}
|
|
54
|
+
|
|
55
|
+
def __init__(
|
|
56
|
+
self,
|
|
57
|
+
alpha=1.0,
|
|
58
|
+
fit_intercept=True,
|
|
59
|
+
copy_X=True,
|
|
60
|
+
max_iter=None,
|
|
61
|
+
tol=1e-4,
|
|
62
|
+
solver="auto",
|
|
63
|
+
positive=False,
|
|
64
|
+
random_state=None,
|
|
65
|
+
):
|
|
66
|
+
super().__init__(
|
|
67
|
+
alpha=alpha,
|
|
68
|
+
fit_intercept=fit_intercept,
|
|
69
|
+
copy_X=copy_X,
|
|
70
|
+
max_iter=max_iter,
|
|
71
|
+
tol=tol,
|
|
72
|
+
solver=solver,
|
|
73
|
+
positive=positive,
|
|
74
|
+
random_state=random_state,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
else:
|
|
78
|
+
|
|
79
|
+
def __init__(
|
|
80
|
+
self,
|
|
81
|
+
alpha=1.0,
|
|
82
|
+
fit_intercept=True,
|
|
83
|
+
normalize="deprecated",
|
|
84
|
+
copy_X=True,
|
|
85
|
+
max_iter=None,
|
|
86
|
+
tol=1e-3,
|
|
87
|
+
solver="auto",
|
|
88
|
+
positive=False,
|
|
89
|
+
random_state=None,
|
|
90
|
+
):
|
|
91
|
+
super().__init__(
|
|
92
|
+
alpha=alpha,
|
|
93
|
+
fit_intercept=fit_intercept,
|
|
94
|
+
normalize=normalize,
|
|
95
|
+
copy_X=copy_X,
|
|
96
|
+
max_iter=max_iter,
|
|
97
|
+
solver=solver,
|
|
98
|
+
tol=tol,
|
|
99
|
+
positive=positive,
|
|
100
|
+
random_state=random_state,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
def fit(self, X, y, sample_weight=None):
|
|
104
|
+
if sklearn_check_version("1.2"):
|
|
105
|
+
self._validate_params()
|
|
106
|
+
|
|
107
|
+
# It is necessary to properly update coefs for predict if we
|
|
108
|
+
# fallback to sklearn in dispatch
|
|
109
|
+
if hasattr(self, "_onedal_estimator"):
|
|
110
|
+
del self._onedal_estimator
|
|
111
|
+
|
|
112
|
+
dispatch(
|
|
113
|
+
self,
|
|
114
|
+
"fit",
|
|
115
|
+
{
|
|
116
|
+
"onedal": self.__class__._onedal_fit,
|
|
117
|
+
"sklearn": _sklearn_Ridge.fit,
|
|
118
|
+
},
|
|
119
|
+
X,
|
|
120
|
+
y,
|
|
121
|
+
sample_weight,
|
|
122
|
+
)
|
|
123
|
+
return self
|
|
124
|
+
|
|
125
|
+
@wrap_output_data
|
|
126
|
+
def predict(self, X):
|
|
127
|
+
check_is_fitted(self)
|
|
128
|
+
|
|
129
|
+
return dispatch(
|
|
130
|
+
self,
|
|
131
|
+
"predict",
|
|
132
|
+
{
|
|
133
|
+
"onedal": self.__class__._onedal_predict,
|
|
134
|
+
"sklearn": _sklearn_Ridge.predict,
|
|
135
|
+
},
|
|
136
|
+
X,
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
@wrap_output_data
|
|
140
|
+
def score(self, X, y, sample_weight=None):
|
|
141
|
+
check_is_fitted(self)
|
|
142
|
+
|
|
143
|
+
return dispatch(
|
|
144
|
+
self,
|
|
145
|
+
"score",
|
|
146
|
+
{
|
|
147
|
+
"onedal": self.__class__._onedal_score,
|
|
148
|
+
"sklearn": _sklearn_Ridge.score,
|
|
149
|
+
},
|
|
150
|
+
X,
|
|
151
|
+
y,
|
|
152
|
+
sample_weight=sample_weight,
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
def _onedal_fit_supported(self, patching_status, method_name, *data):
|
|
156
|
+
assert method_name == "fit"
|
|
157
|
+
assert len(data) == 3
|
|
158
|
+
X, y, sample_weight = data
|
|
159
|
+
|
|
160
|
+
normalize_is_set = (
|
|
161
|
+
hasattr(self, "normalize")
|
|
162
|
+
and self.normalize
|
|
163
|
+
and self.normalize != "deprecated"
|
|
164
|
+
)
|
|
165
|
+
positive_is_set = hasattr(self, "positive") and self.positive
|
|
166
|
+
|
|
167
|
+
patching_status.and_conditions(
|
|
168
|
+
[
|
|
169
|
+
(
|
|
170
|
+
self.solver == "auto",
|
|
171
|
+
f"'{self.solver}' solver is not supported. "
|
|
172
|
+
"Only 'auto' solver is supported.",
|
|
173
|
+
),
|
|
174
|
+
(
|
|
175
|
+
not issparse(X) and not issparse(y),
|
|
176
|
+
"Sparse input is not supported.",
|
|
177
|
+
),
|
|
178
|
+
(sample_weight is None, "Sample weight is not supported."),
|
|
179
|
+
(not normalize_is_set, "Normalization is not supported."),
|
|
180
|
+
(
|
|
181
|
+
not positive_is_set,
|
|
182
|
+
"Forced positive coefficients are not supported.",
|
|
183
|
+
),
|
|
184
|
+
(
|
|
185
|
+
isinstance(self.alpha, numbers.Real),
|
|
186
|
+
"Non-scalar alpha is not supported yet.",
|
|
187
|
+
),
|
|
188
|
+
]
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
return patching_status
|
|
192
|
+
|
|
193
|
+
def _onedal_predict_supported(self, patching_status, method_name, *data):
|
|
194
|
+
assert method_name in ["predict", "score"]
|
|
195
|
+
assert len(data) <= 2
|
|
196
|
+
|
|
197
|
+
n_samples = _num_samples(data[0])
|
|
198
|
+
model_is_sparse = issparse(self.coef_) or (
|
|
199
|
+
self.fit_intercept and issparse(self.intercept_)
|
|
200
|
+
)
|
|
201
|
+
patching_status.and_conditions(
|
|
202
|
+
[
|
|
203
|
+
(
|
|
204
|
+
self.solver == "auto",
|
|
205
|
+
f"'{self.solver}' solver is not supported. "
|
|
206
|
+
"Only 'auto' solver is supported.",
|
|
207
|
+
),
|
|
208
|
+
(n_samples > 0, "Number of samples is less than 1."),
|
|
209
|
+
(not issparse(data[0]), "Sparse input is not supported."),
|
|
210
|
+
(not model_is_sparse, "Sparse coefficients are not supported."),
|
|
211
|
+
]
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
return patching_status
|
|
215
|
+
|
|
216
|
+
def _onedal_gpu_supported(self, method_name, *data):
|
|
217
|
+
patching_status = PatchingConditionsChain(
|
|
218
|
+
f"sklearn.linear_model.{self.__class__.__name__}.{method_name}"
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
if method_name == "fit":
|
|
222
|
+
if not daal_check_version((2025, "P", 200)):
|
|
223
|
+
n_samples = _num_samples(data[0])
|
|
224
|
+
n_features = _num_features(data[0], fallback_1d=True)
|
|
225
|
+
is_underdetermined = n_samples < (
|
|
226
|
+
n_features + int(self.fit_intercept)
|
|
227
|
+
)
|
|
228
|
+
is_zero_alpha = isinstance(self.alpha, numbers.Real) and np.isclose(
|
|
229
|
+
self.alpha, 0, atol=1e-5
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
patching_status.and_condition(
|
|
233
|
+
not is_underdetermined or not is_zero_alpha,
|
|
234
|
+
"The shape of X (fitting) does not satisfy oneDAL requirements:"
|
|
235
|
+
"Number of features + 1 >= number of samples and alpha = 0.",
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
return self._onedal_fit_supported(patching_status, method_name, *data)
|
|
239
|
+
|
|
240
|
+
if method_name in ["predict", "score"]:
|
|
241
|
+
return self._onedal_predict_supported(patching_status, method_name, *data)
|
|
242
|
+
|
|
243
|
+
raise RuntimeError(
|
|
244
|
+
f"Unknown method {method_name} in {self.__class__.__name__}"
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
def _onedal_cpu_supported(self, method_name, *data):
|
|
248
|
+
patching_status = PatchingConditionsChain(
|
|
249
|
+
f"sklearn.linear_model.{self.__class__.__name__}.{method_name}"
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
if method_name == "fit":
|
|
253
|
+
if not daal_check_version((2025, "P", 100)):
|
|
254
|
+
n_samples = _num_samples(data[0])
|
|
255
|
+
n_features = _num_features(data[0], fallback_1d=True)
|
|
256
|
+
is_underdetermined = n_samples < (
|
|
257
|
+
n_features + int(self.fit_intercept)
|
|
258
|
+
)
|
|
259
|
+
is_zero_alpha = isinstance(self.alpha, numbers.Real) and np.isclose(
|
|
260
|
+
self.alpha, 0, atol=1e-5
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
patching_status.and_condition(
|
|
264
|
+
not is_underdetermined or not is_zero_alpha,
|
|
265
|
+
"The shape of X (fitting) does not satisfy oneDAL requirements:"
|
|
266
|
+
"Number of features + 1 >= number of samples and alpha = 0.",
|
|
267
|
+
)
|
|
268
|
+
return self._onedal_fit_supported(patching_status, method_name, *data)
|
|
269
|
+
|
|
270
|
+
if method_name in ["predict", "score"]:
|
|
271
|
+
return self._onedal_predict_supported(patching_status, method_name, *data)
|
|
272
|
+
|
|
273
|
+
raise RuntimeError(
|
|
274
|
+
f"Unknown method {method_name} in {self.__class__.__name__}"
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
def _initialize_onedal_estimator(self):
|
|
278
|
+
onedal_params = {
|
|
279
|
+
"fit_intercept": self.fit_intercept,
|
|
280
|
+
"alpha": self.alpha,
|
|
281
|
+
"copy_X": self.copy_X,
|
|
282
|
+
}
|
|
283
|
+
self._onedal_estimator = onedal_Ridge(**onedal_params)
|
|
284
|
+
|
|
285
|
+
def _onedal_fit(self, X, y, sample_weight, queue=None):
|
|
286
|
+
# `Sample weight` is not supported. Expected to be None value.
|
|
287
|
+
assert sample_weight is None
|
|
288
|
+
|
|
289
|
+
if sklearn_check_version("1.2"):
|
|
290
|
+
self._validate_params()
|
|
291
|
+
elif sklearn_check_version("1.1"):
|
|
292
|
+
if self.max_iter is not None:
|
|
293
|
+
self.max_iter = check_scalar(
|
|
294
|
+
self.max_iter, "max_iter", target_type=numbers.Integral, min_val=1
|
|
295
|
+
)
|
|
296
|
+
self.tol = check_scalar(
|
|
297
|
+
self.tol, "tol", target_type=numbers.Real, min_val=0.0
|
|
298
|
+
)
|
|
299
|
+
if self.alpha is not None and not isinstance(
|
|
300
|
+
self.alpha, (np.ndarray, tuple)
|
|
301
|
+
):
|
|
302
|
+
self.alpha = check_scalar(
|
|
303
|
+
self.alpha,
|
|
304
|
+
"alpha",
|
|
305
|
+
target_type=numbers.Real,
|
|
306
|
+
min_val=0.0,
|
|
307
|
+
include_boundaries="left",
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
check_params = {
|
|
311
|
+
"X": X,
|
|
312
|
+
"y": y,
|
|
313
|
+
"dtype": [np.float64, np.float32],
|
|
314
|
+
"accept_sparse": ["csr", "csc", "coo"],
|
|
315
|
+
"y_numeric": True,
|
|
316
|
+
"multi_output": True,
|
|
317
|
+
}
|
|
318
|
+
X, y = validate_data(self, **check_params)
|
|
319
|
+
|
|
320
|
+
if not sklearn_check_version("1.2"):
|
|
321
|
+
self._normalize = _deprecate_normalize(
|
|
322
|
+
self.normalize,
|
|
323
|
+
default=False,
|
|
324
|
+
estimator_name=self.__class__.__name__,
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
self._initialize_onedal_estimator()
|
|
328
|
+
self._onedal_estimator.fit(X, y, queue=queue)
|
|
329
|
+
self._save_attributes()
|
|
330
|
+
|
|
331
|
+
def _onedal_predict(self, X, queue=None):
|
|
332
|
+
X = validate_data(self, X, accept_sparse=False, reset=False)
|
|
333
|
+
|
|
334
|
+
if not hasattr(self, "_onedal_estimator"):
|
|
335
|
+
self._initialize_onedal_estimator()
|
|
336
|
+
self._onedal_estimator.coef_ = self.coef_
|
|
337
|
+
self._onedal_estimator.intercept_ = self.intercept_
|
|
338
|
+
|
|
339
|
+
res = self._onedal_estimator.predict(X, queue=queue)
|
|
340
|
+
return res
|
|
341
|
+
|
|
342
|
+
def _onedal_score(self, X, y, sample_weight=None, queue=None):
|
|
343
|
+
return r2_score(
|
|
344
|
+
y, self._onedal_predict(X, queue=queue), sample_weight=sample_weight
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
@property
|
|
348
|
+
def coef_(self):
|
|
349
|
+
return self._coef
|
|
350
|
+
|
|
351
|
+
@coef_.setter
|
|
352
|
+
def coef_(self, value):
|
|
353
|
+
if hasattr(self, "_onedal_estimator"):
|
|
354
|
+
self._onedal_estimator.coef_ = value
|
|
355
|
+
# checking if the model is already fitted and if so, deleting the model
|
|
356
|
+
if hasattr(self._onedal_estimator, "_onedal_model"):
|
|
357
|
+
del self._onedal_estimator._onedal_model
|
|
358
|
+
self._coef = value
|
|
359
|
+
|
|
360
|
+
@property
|
|
361
|
+
def intercept_(self):
|
|
362
|
+
return self._intercept
|
|
363
|
+
|
|
364
|
+
@intercept_.setter
|
|
365
|
+
def intercept_(self, value):
|
|
366
|
+
if hasattr(self, "_onedal_estimator"):
|
|
367
|
+
self._onedal_estimator.intercept_ = value
|
|
368
|
+
# checking if the model is already fitted and if so, deleting the model
|
|
369
|
+
if hasattr(self._onedal_estimator, "_onedal_model"):
|
|
370
|
+
del self._onedal_estimator._onedal_model
|
|
371
|
+
self._intercept = value
|
|
372
|
+
|
|
373
|
+
def _save_attributes(self):
|
|
374
|
+
self.n_features_in_ = self._onedal_estimator.n_features_in_
|
|
375
|
+
self._coef = self._onedal_estimator.coef_
|
|
376
|
+
self._intercept = self._onedal_estimator.intercept_
|
|
377
|
+
|
|
378
|
+
fit.__doc__ = _sklearn_Ridge.fit.__doc__
|
|
379
|
+
predict.__doc__ = _sklearn_Ridge.predict.__doc__
|
|
380
|
+
score.__doc__ = _sklearn_Ridge.score.__doc__
|
|
381
|
+
|
|
382
|
+
else:
|
|
383
|
+
from daal4py.sklearn.linear_model import Ridge
|
|
384
|
+
from onedal._device_offload import support_input_format
|
|
385
|
+
|
|
386
|
+
Ridge.fit = support_input_format(queue_param=False)(Ridge.fit)
|
|
387
|
+
Ridge.predict = support_input_format(queue_param=False)(Ridge.predict)
|
|
388
|
+
Ridge.score = support_input_format(queue_param=False)(Ridge.score)
|
|
389
|
+
|
|
390
|
+
logging.warning("Ridge requires oneDAL version >= 2024.6 but it was not found")
|
|
@@ -0,0 +1,267 @@
|
|
|
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
|
+
_as_numpy,
|
|
23
|
+
_convert_to_dataframe,
|
|
24
|
+
get_dataframes_and_queues,
|
|
25
|
+
)
|
|
26
|
+
from sklearnex.linear_model import IncrementalLinearRegression
|
|
27
|
+
from sklearnex.tests.utils import _IS_INTEL
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
31
|
+
@pytest.mark.parametrize("fit_intercept", [True, False])
|
|
32
|
+
@pytest.mark.parametrize("macro_block", [None, 1024])
|
|
33
|
+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
|
|
34
|
+
def test_sklearnex_fit_on_gold_data(dataframe, queue, fit_intercept, macro_block, dtype):
|
|
35
|
+
X = np.array([[1], [2]])
|
|
36
|
+
X = X.astype(dtype=dtype)
|
|
37
|
+
X_df = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
|
|
38
|
+
y = np.array([[1], [2]])
|
|
39
|
+
y = y.astype(dtype=dtype)
|
|
40
|
+
y_df = _convert_to_dataframe(y, sycl_queue=queue, target_df=dataframe)
|
|
41
|
+
|
|
42
|
+
inclin = IncrementalLinearRegression(fit_intercept=fit_intercept)
|
|
43
|
+
if macro_block is not None:
|
|
44
|
+
hparams = IncrementalLinearRegression.get_hyperparameters("fit")
|
|
45
|
+
hparams.cpu_macro_block = macro_block
|
|
46
|
+
hparams.gpu_macro_block = macro_block
|
|
47
|
+
inclin.fit(X_df, y_df)
|
|
48
|
+
|
|
49
|
+
y_pred = inclin.predict(X_df)
|
|
50
|
+
np_y_pred = _as_numpy(y_pred)
|
|
51
|
+
|
|
52
|
+
tol = 5e-5 if dtype == np.float32 else 1e-7
|
|
53
|
+
assert_allclose(inclin.coef_, [1], atol=tol)
|
|
54
|
+
if fit_intercept:
|
|
55
|
+
assert_allclose(inclin.intercept_, [0], atol=tol)
|
|
56
|
+
assert_allclose(np_y_pred, y, atol=tol)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
60
|
+
@pytest.mark.parametrize("fit_intercept", [True, False])
|
|
61
|
+
@pytest.mark.parametrize("macro_block", [None, 1024])
|
|
62
|
+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
|
|
63
|
+
def test_sklearnex_partial_fit_on_gold_data(
|
|
64
|
+
dataframe, queue, fit_intercept, macro_block, dtype
|
|
65
|
+
):
|
|
66
|
+
X = np.array([[1], [2], [3], [4]])
|
|
67
|
+
X = X.astype(dtype=dtype)
|
|
68
|
+
y = X + 3
|
|
69
|
+
y = y.astype(dtype=dtype)
|
|
70
|
+
X_split = np.array_split(X, 2)
|
|
71
|
+
y_split = np.array_split(y, 2)
|
|
72
|
+
|
|
73
|
+
inclin = IncrementalLinearRegression()
|
|
74
|
+
if macro_block is not None:
|
|
75
|
+
hparams = IncrementalLinearRegression.get_hyperparameters("fit")
|
|
76
|
+
hparams.cpu_macro_block = macro_block
|
|
77
|
+
hparams.gpu_macro_block = macro_block
|
|
78
|
+
for i in range(2):
|
|
79
|
+
X_split_df = _convert_to_dataframe(
|
|
80
|
+
X_split[i], sycl_queue=queue, target_df=dataframe
|
|
81
|
+
)
|
|
82
|
+
y_split_df = _convert_to_dataframe(
|
|
83
|
+
y_split[i], sycl_queue=queue, target_df=dataframe
|
|
84
|
+
)
|
|
85
|
+
inclin.partial_fit(X_split_df, y_split_df)
|
|
86
|
+
|
|
87
|
+
X_df = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
|
|
88
|
+
y_pred = inclin.predict(X_df)
|
|
89
|
+
np_y_pred = _as_numpy(y_pred)
|
|
90
|
+
|
|
91
|
+
assert inclin.n_features_in_ == 1
|
|
92
|
+
tol = 1e-5 if dtype == np.float32 else 1e-7
|
|
93
|
+
assert_allclose(inclin.coef_, [[1]], atol=tol)
|
|
94
|
+
if fit_intercept:
|
|
95
|
+
assert_allclose(inclin.intercept_, 3, atol=tol)
|
|
96
|
+
|
|
97
|
+
assert_allclose(np_y_pred, y, atol=tol)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
101
|
+
@pytest.mark.parametrize("fit_intercept", [True, False])
|
|
102
|
+
@pytest.mark.parametrize("macro_block", [None, 1024])
|
|
103
|
+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
|
|
104
|
+
def test_sklearnex_partial_fit_multitarget_on_gold_data(
|
|
105
|
+
dataframe, queue, fit_intercept, macro_block, dtype
|
|
106
|
+
):
|
|
107
|
+
X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
|
|
108
|
+
X = X.astype(dtype=dtype)
|
|
109
|
+
y = np.dot(X, [1, 2]) + 3
|
|
110
|
+
y = y.astype(dtype=dtype)
|
|
111
|
+
X_split = np.array_split(X, 2)
|
|
112
|
+
y_split = np.array_split(y, 2)
|
|
113
|
+
|
|
114
|
+
inclin = IncrementalLinearRegression()
|
|
115
|
+
if macro_block is not None:
|
|
116
|
+
hparams = IncrementalLinearRegression.get_hyperparameters("fit")
|
|
117
|
+
hparams.cpu_macro_block = macro_block
|
|
118
|
+
hparams.gpu_macro_block = macro_block
|
|
119
|
+
for i in range(2):
|
|
120
|
+
X_split_df = _convert_to_dataframe(
|
|
121
|
+
X_split[i], sycl_queue=queue, target_df=dataframe
|
|
122
|
+
)
|
|
123
|
+
y_split_df = _convert_to_dataframe(
|
|
124
|
+
y_split[i], sycl_queue=queue, target_df=dataframe
|
|
125
|
+
)
|
|
126
|
+
inclin.partial_fit(X_split_df, y_split_df)
|
|
127
|
+
|
|
128
|
+
X_df = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
|
|
129
|
+
y_pred = inclin.predict(X_df)
|
|
130
|
+
np_y_pred = _as_numpy(y_pred)
|
|
131
|
+
|
|
132
|
+
assert inclin.n_features_in_ == 2
|
|
133
|
+
tol = 1e-7
|
|
134
|
+
if dtype == np.float32:
|
|
135
|
+
tol = 7e-6 if _IS_INTEL else 2e-5
|
|
136
|
+
|
|
137
|
+
assert_allclose(inclin.coef_, [1.0, 2.0], atol=tol)
|
|
138
|
+
if fit_intercept:
|
|
139
|
+
assert_allclose(inclin.intercept_, 3.0, atol=tol)
|
|
140
|
+
|
|
141
|
+
assert_allclose(np_y_pred, y, atol=tol)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
145
|
+
@pytest.mark.parametrize("fit_intercept", [True, False])
|
|
146
|
+
@pytest.mark.parametrize("num_samples", [100, 1000])
|
|
147
|
+
@pytest.mark.parametrize("num_features", [5, 10])
|
|
148
|
+
@pytest.mark.parametrize("num_targets", [1, 2])
|
|
149
|
+
@pytest.mark.parametrize("num_blocks", [1, 10])
|
|
150
|
+
@pytest.mark.parametrize("macro_block", [None, 1024])
|
|
151
|
+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
|
|
152
|
+
def test_sklearnex_partial_fit_on_random_data(
|
|
153
|
+
dataframe,
|
|
154
|
+
queue,
|
|
155
|
+
fit_intercept,
|
|
156
|
+
num_samples,
|
|
157
|
+
num_features,
|
|
158
|
+
num_targets,
|
|
159
|
+
num_blocks,
|
|
160
|
+
macro_block,
|
|
161
|
+
dtype,
|
|
162
|
+
):
|
|
163
|
+
seed = 42
|
|
164
|
+
gen = np.random.default_rng(seed)
|
|
165
|
+
intercept = gen.random(size=num_targets, dtype=dtype)
|
|
166
|
+
coef = gen.random(size=(num_targets, num_features), dtype=dtype).T
|
|
167
|
+
|
|
168
|
+
X = gen.random(size=(num_samples, num_features), dtype=dtype)
|
|
169
|
+
if fit_intercept:
|
|
170
|
+
y = X @ coef + intercept[np.newaxis, :]
|
|
171
|
+
else:
|
|
172
|
+
y = X @ coef
|
|
173
|
+
|
|
174
|
+
X_split = np.array_split(X, num_blocks)
|
|
175
|
+
y_split = np.array_split(y, num_blocks)
|
|
176
|
+
|
|
177
|
+
inclin = IncrementalLinearRegression(fit_intercept=fit_intercept)
|
|
178
|
+
if macro_block is not None:
|
|
179
|
+
hparams = IncrementalLinearRegression.get_hyperparameters("fit")
|
|
180
|
+
hparams.cpu_macro_block = macro_block
|
|
181
|
+
hparams.gpu_macro_block = macro_block
|
|
182
|
+
for i in range(num_blocks):
|
|
183
|
+
X_split_df = _convert_to_dataframe(
|
|
184
|
+
X_split[i], sycl_queue=queue, target_df=dataframe
|
|
185
|
+
)
|
|
186
|
+
y_split_df = _convert_to_dataframe(
|
|
187
|
+
y_split[i], sycl_queue=queue, target_df=dataframe
|
|
188
|
+
)
|
|
189
|
+
inclin.partial_fit(X_split_df, y_split_df)
|
|
190
|
+
|
|
191
|
+
tol = 1e-4 if inclin.coef_.dtype == np.float32 else 1e-7
|
|
192
|
+
assert_allclose(coef.T.squeeze(), inclin.coef_, atol=tol)
|
|
193
|
+
|
|
194
|
+
if fit_intercept:
|
|
195
|
+
assert_allclose(intercept, inclin.intercept_, atol=tol)
|
|
196
|
+
|
|
197
|
+
X_test = gen.random(size=(num_samples, num_features), dtype=dtype)
|
|
198
|
+
if fit_intercept:
|
|
199
|
+
expected_y_pred = (X_test @ coef + intercept[np.newaxis, :]).squeeze()
|
|
200
|
+
else:
|
|
201
|
+
expected_y_pred = (X_test @ coef).squeeze()
|
|
202
|
+
|
|
203
|
+
X_test_df = _convert_to_dataframe(X_test, sycl_queue=queue, target_df=dataframe)
|
|
204
|
+
|
|
205
|
+
y_pred = inclin.predict(X_test_df)
|
|
206
|
+
|
|
207
|
+
assert_allclose(expected_y_pred, _as_numpy(y_pred), atol=tol)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
211
|
+
@pytest.mark.parametrize("fit_intercept", [True, False])
|
|
212
|
+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
|
|
213
|
+
def test_sklearnex_incremental_estimatior_pickle(dataframe, queue, fit_intercept, dtype):
|
|
214
|
+
import pickle
|
|
215
|
+
|
|
216
|
+
from sklearnex.linear_model import IncrementalLinearRegression
|
|
217
|
+
|
|
218
|
+
inclin = IncrementalLinearRegression()
|
|
219
|
+
|
|
220
|
+
# Check that estimator can be serialized without any data.
|
|
221
|
+
dump = pickle.dumps(inclin)
|
|
222
|
+
inclin_loaded = pickle.loads(dump)
|
|
223
|
+
|
|
224
|
+
seed = 77
|
|
225
|
+
gen = np.random.default_rng(seed)
|
|
226
|
+
intercept = gen.random(size=1, dtype=dtype)
|
|
227
|
+
coef = gen.random(size=(1, 10), dtype=dtype).T
|
|
228
|
+
X = gen.uniform(low=-0.3, high=+0.7, size=(30, 10))
|
|
229
|
+
X = X.astype(dtype)
|
|
230
|
+
if fit_intercept:
|
|
231
|
+
y = X @ coef + intercept[np.newaxis, :]
|
|
232
|
+
else:
|
|
233
|
+
y = X @ coef
|
|
234
|
+
X_split = np.array_split(X, 2)
|
|
235
|
+
y_split = np.array_split(y, 2)
|
|
236
|
+
X_split_df = _convert_to_dataframe(X_split[0], sycl_queue=queue, target_df=dataframe)
|
|
237
|
+
y_split_df = _convert_to_dataframe(y_split[0], sycl_queue=queue, target_df=dataframe)
|
|
238
|
+
inclin.partial_fit(X_split_df, y_split_df)
|
|
239
|
+
inclin_loaded.partial_fit(X_split_df, y_split_df)
|
|
240
|
+
|
|
241
|
+
# Check that estimator can be serialized after partial_fit call.
|
|
242
|
+
dump = pickle.dumps(inclin_loaded)
|
|
243
|
+
inclin_loaded = pickle.loads(dump)
|
|
244
|
+
|
|
245
|
+
assert inclin.batch_size == inclin_loaded.batch_size
|
|
246
|
+
assert inclin.n_features_in_ == inclin_loaded.n_features_in_
|
|
247
|
+
assert inclin.n_samples_seen_ == inclin_loaded.n_samples_seen_
|
|
248
|
+
if hasattr(inclin, "_parameter_constraints"):
|
|
249
|
+
assert inclin._parameter_constraints == inclin_loaded._parameter_constraints
|
|
250
|
+
assert inclin.n_jobs == inclin_loaded.n_jobs
|
|
251
|
+
|
|
252
|
+
X_split_df = _convert_to_dataframe(X_split[1], sycl_queue=queue, target_df=dataframe)
|
|
253
|
+
y_split_df = _convert_to_dataframe(y_split[1], sycl_queue=queue, target_df=dataframe)
|
|
254
|
+
inclin.partial_fit(X_split_df, y_split_df)
|
|
255
|
+
inclin_loaded.partial_fit(X_split_df, y_split_df)
|
|
256
|
+
dump = pickle.dumps(inclin)
|
|
257
|
+
inclin_loaded = pickle.loads(dump)
|
|
258
|
+
|
|
259
|
+
assert_allclose(inclin.coef_, inclin_loaded.coef_, atol=1e-6)
|
|
260
|
+
assert_allclose(inclin.intercept_, inclin_loaded.intercept_, atol=1e-6)
|
|
261
|
+
|
|
262
|
+
# Check that finalized estimator can be serialized.
|
|
263
|
+
dump = pickle.dumps(inclin_loaded)
|
|
264
|
+
inclin_loaded = pickle.loads(dump)
|
|
265
|
+
|
|
266
|
+
assert_allclose(inclin.coef_, inclin_loaded.coef_, atol=1e-6)
|
|
267
|
+
assert_allclose(inclin.intercept_, inclin_loaded.intercept_, atol=1e-6)
|