scikit-learn-intelex 2025.0.0__py310-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-310-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-310-x86_64-linux-gnu.so +0 -0
- daal4py/sklearn/__init__.py +40 -0
- daal4py/sklearn/_n_jobs_support.py +242 -0
- daal4py/sklearn/_utils.py +241 -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 +192 -0
- daal4py/sklearn/ensemble/GBTDAAL.py +318 -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 +196 -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 +155 -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 +87 -0
- daal4py/sklearn/monkeypatch/tests/utils/_launch_algorithms.py +118 -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 +693 -0
- onedal/__init__.py +83 -0
- onedal/_config.py +53 -0
- onedal/_device_offload.py +229 -0
- onedal/_onedal_py_dpc.cpython-310-x86_64-linux-gnu.so +0 -0
- onedal/_onedal_py_host.cpython-310-x86_64-linux-gnu.so +0 -0
- onedal/_onedal_py_spmd_dpc.cpython-310-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 +160 -0
- onedal/basic_statistics/tests/test_basic_statistics.py +298 -0
- onedal/basic_statistics/tests/test_incremental_basic_statistics.py +196 -0
- onedal/cluster/__init__.py +27 -0
- onedal/cluster/dbscan.py +110 -0
- onedal/cluster/kmeans.py +560 -0
- onedal/cluster/kmeans_init.py +115 -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 +59 -0
- onedal/common/_spmd_policy.py +30 -0
- onedal/common/hyperparameters.py +116 -0
- onedal/common/tests/test_policy.py +75 -0
- onedal/covariance/__init__.py +20 -0
- onedal/covariance/covariance.py +125 -0
- onedal/covariance/incremental_covariance.py +146 -0
- onedal/covariance/tests/test_covariance.py +50 -0
- onedal/covariance/tests/test_incremental_covariance.py +122 -0
- onedal/datatypes/__init__.py +19 -0
- onedal/datatypes/_data_conversion.py +95 -0
- onedal/datatypes/tests/test_data.py +235 -0
- onedal/decomposition/__init__.py +20 -0
- onedal/decomposition/incremental_pca.py +204 -0
- onedal/decomposition/pca.py +186 -0
- onedal/decomposition/tests/test_incremental_pca.py +198 -0
- onedal/ensemble/__init__.py +29 -0
- onedal/ensemble/forest.py +720 -0
- onedal/ensemble/tests/test_random_forest.py +97 -0
- onedal/linear_model/__init__.py +27 -0
- onedal/linear_model/incremental_linear_model.py +258 -0
- onedal/linear_model/linear_model.py +329 -0
- onedal/linear_model/logistic_regression.py +249 -0
- onedal/linear_model/tests/test_incremental_linear_regression.py +168 -0
- onedal/linear_model/tests/test_incremental_ridge_regression.py +107 -0
- onedal/linear_model/tests/test_linear_regression.py +149 -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 +778 -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 +153 -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 +69 -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 +82 -0
- onedal/spmd/decomposition/__init__.py +20 -0
- onedal/spmd/decomposition/incremental_pca.py +117 -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 +97 -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 +168 -0
- onedal/svm/tests/test_svr.py +243 -0
- onedal/tests/test_common.py +41 -0
- onedal/tests/utils/_dataframes_support.py +168 -0
- onedal/tests/utils/_device_selection.py +107 -0
- onedal/utils/__init__.py +49 -0
- onedal/utils/_array_api.py +91 -0
- onedal/utils/validation.py +432 -0
- scikit_learn_intelex-2025.0.0.dist-info/LICENSE.txt +202 -0
- scikit_learn_intelex-2025.0.0.dist-info/METADATA +231 -0
- scikit_learn_intelex-2025.0.0.dist-info/RECORD +278 -0
- scikit_learn_intelex-2025.0.0.dist-info/WHEEL +5 -0
- scikit_learn_intelex-2025.0.0.dist-info/top_level.txt +3 -0
- sklearnex/__init__.py +65 -0
- sklearnex/__main__.py +58 -0
- sklearnex/_config.py +98 -0
- sklearnex/_device_offload.py +121 -0
- sklearnex/_utils.py +109 -0
- sklearnex/basic_statistics/__init__.py +20 -0
- sklearnex/basic_statistics/basic_statistics.py +140 -0
- sklearnex/basic_statistics/incremental_basic_statistics.py +288 -0
- sklearnex/basic_statistics/tests/test_basic_statistics.py +251 -0
- sklearnex/basic_statistics/tests/test_incremental_basic_statistics.py +384 -0
- sklearnex/cluster/__init__.py +20 -0
- sklearnex/cluster/dbscan.py +192 -0
- sklearnex/cluster/k_means.py +383 -0
- sklearnex/cluster/tests/test_dbscan.py +38 -0
- sklearnex/cluster/tests/test_kmeans.py +153 -0
- sklearnex/conftest.py +73 -0
- sklearnex/covariance/__init__.py +19 -0
- sklearnex/covariance/incremental_covariance.py +368 -0
- sklearnex/covariance/tests/test_incremental_covariance.py +226 -0
- sklearnex/decomposition/__init__.py +19 -0
- sklearnex/decomposition/pca.py +414 -0
- sklearnex/decomposition/tests/test_pca.py +58 -0
- sklearnex/dispatcher.py +543 -0
- sklearnex/doc/third-party-programs.txt +424 -0
- sklearnex/ensemble/__init__.py +29 -0
- sklearnex/ensemble/_forest.py +2016 -0
- sklearnex/ensemble/tests/test_forest.py +120 -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 +463 -0
- sklearnex/linear_model/incremental_ridge.py +418 -0
- sklearnex/linear_model/linear.py +302 -0
- sklearnex/linear_model/logistic_path.py +17 -0
- sklearnex/linear_model/logistic_regression.py +403 -0
- sklearnex/linear_model/ridge.py +24 -0
- sklearnex/linear_model/tests/test_incremental_linear.py +203 -0
- sklearnex/linear_model/tests/test_incremental_ridge.py +153 -0
- sklearnex/linear_model/tests/test_linear.py +142 -0
- sklearnex/linear_model/tests/test_logreg.py +134 -0
- sklearnex/manifold/__init__.py +19 -0
- sklearnex/manifold/t_sne.py +21 -0
- sklearnex/manifold/tests/test_tsne.py +26 -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 +231 -0
- sklearnex/neighbors/common.py +310 -0
- sklearnex/neighbors/knn_classification.py +226 -0
- sklearnex/neighbors/knn_regression.py +203 -0
- sklearnex/neighbors/knn_unsupervised.py +170 -0
- sklearnex/neighbors/tests/test_neighbors.py +80 -0
- sklearnex/preview/__init__.py +17 -0
- sklearnex/preview/covariance/__init__.py +19 -0
- sklearnex/preview/covariance/covariance.py +133 -0
- sklearnex/preview/covariance/tests/test_covariance.py +66 -0
- sklearnex/preview/decomposition/__init__.py +19 -0
- sklearnex/preview/decomposition/incremental_pca.py +228 -0
- sklearnex/preview/decomposition/tests/test_incremental_pca.py +266 -0
- sklearnex/preview/linear_model/__init__.py +19 -0
- sklearnex/preview/linear_model/ridge.py +419 -0
- sklearnex/preview/linear_model/tests/test_ridge.py +102 -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 +307 -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 +172 -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 +329 -0
- sklearnex/spmd/linear_model/tests/test_linear_regression_spmd.py +145 -0
- sklearnex/spmd/linear_model/tests/test_logistic_regression_spmd.py +166 -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 +328 -0
- sklearnex/svm/nusvc.py +332 -0
- sklearnex/svm/nusvr.py +148 -0
- sklearnex/svm/svc.py +360 -0
- sklearnex/svm/svr.py +149 -0
- sklearnex/svm/tests/test_svm.py +93 -0
- sklearnex/tests/_utils.py +328 -0
- sklearnex/tests/_utils_spmd.py +198 -0
- sklearnex/tests/test_common.py +54 -0
- sklearnex/tests/test_config.py +43 -0
- sklearnex/tests/test_memory_usage.py +291 -0
- sklearnex/tests/test_monkeypatch.py +276 -0
- sklearnex/tests/test_n_jobs_support.py +103 -0
- sklearnex/tests/test_parallel.py +48 -0
- sklearnex/tests/test_patching.py +385 -0
- sklearnex/tests/test_run_to_run_stability.py +296 -0
- sklearnex/utils/__init__.py +19 -0
- sklearnex/utils/_array_api.py +82 -0
- sklearnex/utils/parallel.py +59 -0
- sklearnex/utils/tests/test_finite.py +89 -0
- sklearnex/utils/validation.py +17 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
# ==============================================================================
|
|
2
|
+
# Copyright 2023 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
|
+
from abc import ABCMeta, abstractmethod
|
|
18
|
+
from numbers import Number
|
|
19
|
+
|
|
20
|
+
import numpy as np
|
|
21
|
+
|
|
22
|
+
from daal4py.sklearn._utils import daal_check_version, get_dtype, make2d
|
|
23
|
+
|
|
24
|
+
from ..common._base import BaseEstimator
|
|
25
|
+
from ..common._estimator_checks import _check_is_fitted
|
|
26
|
+
from ..common.hyperparameters import get_hyperparameters
|
|
27
|
+
from ..datatypes import _convert_to_supported, from_table, to_table
|
|
28
|
+
from ..utils import _check_array, _check_n_features, _check_X_y, _num_features
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class BaseLinearRegression(BaseEstimator, metaclass=ABCMeta):
|
|
32
|
+
"""
|
|
33
|
+
Base class for LinearRegression oneDAL implementation.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
@abstractmethod
|
|
37
|
+
def __init__(self, fit_intercept, copy_X, algorithm, alpha=0.0):
|
|
38
|
+
self.fit_intercept = fit_intercept
|
|
39
|
+
self.alpha = alpha
|
|
40
|
+
self.copy_X = copy_X
|
|
41
|
+
self.algorithm = algorithm
|
|
42
|
+
|
|
43
|
+
def _get_onedal_params(self, dtype=np.float32):
|
|
44
|
+
intercept = "intercept|" if self.fit_intercept else ""
|
|
45
|
+
params = {
|
|
46
|
+
"fptype": "float" if dtype == np.float32 else "double",
|
|
47
|
+
"method": self.algorithm,
|
|
48
|
+
"intercept": self.fit_intercept,
|
|
49
|
+
"result_option": (intercept + "coefficients"),
|
|
50
|
+
}
|
|
51
|
+
if daal_check_version((2024, "P", 600)):
|
|
52
|
+
params["alpha"] = self.alpha
|
|
53
|
+
|
|
54
|
+
return params
|
|
55
|
+
|
|
56
|
+
def _create_model(self, policy):
|
|
57
|
+
module = self._get_backend("linear_model", "regression")
|
|
58
|
+
model = module.model()
|
|
59
|
+
|
|
60
|
+
coefficients = self.coef_
|
|
61
|
+
dtype = get_dtype(coefficients)
|
|
62
|
+
|
|
63
|
+
if coefficients.ndim == 2:
|
|
64
|
+
n_features_in = coefficients.shape[1]
|
|
65
|
+
n_targets_in = coefficients.shape[0]
|
|
66
|
+
else:
|
|
67
|
+
n_features_in = coefficients.size
|
|
68
|
+
n_targets_in = 1
|
|
69
|
+
|
|
70
|
+
intercept = self.intercept_
|
|
71
|
+
if isinstance(intercept, Number):
|
|
72
|
+
assert n_targets_in == 1
|
|
73
|
+
else:
|
|
74
|
+
if not isinstance(intercept, np.ndarray):
|
|
75
|
+
intercept = np.asarray(intercept, dtype=dtype)
|
|
76
|
+
assert n_targets_in == intercept.size
|
|
77
|
+
|
|
78
|
+
coefficients, intercept = make2d(coefficients), make2d(intercept)
|
|
79
|
+
coefficients = coefficients.T if n_targets_in == 1 else coefficients
|
|
80
|
+
|
|
81
|
+
assert coefficients.shape == (
|
|
82
|
+
n_targets_in,
|
|
83
|
+
n_features_in,
|
|
84
|
+
), f"{coefficients.shape}, {n_targets_in}, {n_features_in}"
|
|
85
|
+
assert intercept.shape == (n_targets_in, 1), f"{intercept.shape}, {n_targets_in}"
|
|
86
|
+
|
|
87
|
+
desired_shape = (n_targets_in, n_features_in + 1)
|
|
88
|
+
packed_coefficients = np.zeros(desired_shape, dtype=dtype)
|
|
89
|
+
|
|
90
|
+
packed_coefficients[:, 1:] = coefficients
|
|
91
|
+
if self.fit_intercept:
|
|
92
|
+
packed_coefficients[:, 0][:, np.newaxis] = intercept
|
|
93
|
+
|
|
94
|
+
packed_coefficients = _convert_to_supported(policy, packed_coefficients)
|
|
95
|
+
|
|
96
|
+
model.packed_coefficients = to_table(packed_coefficients)
|
|
97
|
+
|
|
98
|
+
self._onedal_model = model
|
|
99
|
+
|
|
100
|
+
return model
|
|
101
|
+
|
|
102
|
+
def predict(self, X, queue=None):
|
|
103
|
+
"""
|
|
104
|
+
Predict using the linear model.
|
|
105
|
+
Parameters
|
|
106
|
+
----------
|
|
107
|
+
X : array-like or sparse matrix, shape (n_samples, n_features)
|
|
108
|
+
Samples.
|
|
109
|
+
|
|
110
|
+
queue : dpctl.SyclQueue
|
|
111
|
+
If not None, uses this queue for computations.
|
|
112
|
+
|
|
113
|
+
Returns
|
|
114
|
+
-------
|
|
115
|
+
C : array, shape (n_samples, n_targets)
|
|
116
|
+
Returns predicted values.
|
|
117
|
+
"""
|
|
118
|
+
module = self._get_backend("linear_model", "regression")
|
|
119
|
+
|
|
120
|
+
_check_is_fitted(self)
|
|
121
|
+
|
|
122
|
+
policy = self._get_policy(queue, X)
|
|
123
|
+
|
|
124
|
+
X = _check_array(
|
|
125
|
+
X, dtype=[np.float64, np.float32], force_all_finite=False, ensure_2d=False
|
|
126
|
+
)
|
|
127
|
+
_check_n_features(self, X, False)
|
|
128
|
+
|
|
129
|
+
if hasattr(self, "_onedal_model"):
|
|
130
|
+
model = self._onedal_model
|
|
131
|
+
else:
|
|
132
|
+
model = self._create_model(policy)
|
|
133
|
+
|
|
134
|
+
X = make2d(X)
|
|
135
|
+
X = _convert_to_supported(policy, X)
|
|
136
|
+
params = self._get_onedal_params(get_dtype(X))
|
|
137
|
+
|
|
138
|
+
X_table = to_table(X)
|
|
139
|
+
result = module.infer(policy, params, model, X_table)
|
|
140
|
+
y = from_table(result.responses)
|
|
141
|
+
|
|
142
|
+
if y.shape[1] == 1 and self.coef_.ndim == 1:
|
|
143
|
+
return y.ravel()
|
|
144
|
+
else:
|
|
145
|
+
return y
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class LinearRegression(BaseLinearRegression):
|
|
149
|
+
"""
|
|
150
|
+
Linear Regression oneDAL implementation.
|
|
151
|
+
|
|
152
|
+
Parameters
|
|
153
|
+
----------
|
|
154
|
+
fit_intercept : bool, default=True
|
|
155
|
+
Whether to calculate the intercept for this model. If set
|
|
156
|
+
to False, no intercept will be used in calculations
|
|
157
|
+
(i.e. data is expected to be centered).
|
|
158
|
+
|
|
159
|
+
copy_X : bool, default=True
|
|
160
|
+
If True, X will be copied; else, it may be overwritten.
|
|
161
|
+
|
|
162
|
+
algorithm : string, default="norm_eq"
|
|
163
|
+
Algorithm used for computation on oneDAL side
|
|
164
|
+
"""
|
|
165
|
+
|
|
166
|
+
def __init__(
|
|
167
|
+
self,
|
|
168
|
+
fit_intercept=True,
|
|
169
|
+
copy_X=False,
|
|
170
|
+
*,
|
|
171
|
+
algorithm="norm_eq",
|
|
172
|
+
**kwargs,
|
|
173
|
+
):
|
|
174
|
+
super().__init__(fit_intercept=fit_intercept, copy_X=copy_X, algorithm=algorithm)
|
|
175
|
+
|
|
176
|
+
def fit(self, X, y, queue=None):
|
|
177
|
+
"""
|
|
178
|
+
Fit linear model.
|
|
179
|
+
Parameters
|
|
180
|
+
----------
|
|
181
|
+
X : {array-like, sparse matrix} of shape (n_samples, n_features)
|
|
182
|
+
Training data.
|
|
183
|
+
|
|
184
|
+
y : array-like of shape (n_samples,) or (n_samples, n_targets)
|
|
185
|
+
Target values. Will be cast to X's dtype if necessary.
|
|
186
|
+
|
|
187
|
+
queue : dpctl.SyclQueue
|
|
188
|
+
If not None, use this queue for computations.
|
|
189
|
+
|
|
190
|
+
Returns
|
|
191
|
+
-------
|
|
192
|
+
self : object
|
|
193
|
+
Fitted Estimator.
|
|
194
|
+
"""
|
|
195
|
+
module = self._get_backend("linear_model", "regression")
|
|
196
|
+
|
|
197
|
+
# TODO Fix _check_X_y to make sure this conversion is there
|
|
198
|
+
if not isinstance(X, np.ndarray):
|
|
199
|
+
X = np.asarray(X)
|
|
200
|
+
|
|
201
|
+
dtype = get_dtype(X)
|
|
202
|
+
if dtype not in [np.float32, np.float64]:
|
|
203
|
+
dtype = np.float64
|
|
204
|
+
X = X.astype(dtype, copy=self.copy_X)
|
|
205
|
+
|
|
206
|
+
y = np.asarray(y).astype(dtype=dtype)
|
|
207
|
+
|
|
208
|
+
X, y = _check_X_y(X, y, force_all_finite=False, accept_2d_y=True)
|
|
209
|
+
|
|
210
|
+
policy = self._get_policy(queue, X, y)
|
|
211
|
+
|
|
212
|
+
self.n_features_in_ = _num_features(X, fallback_1d=True)
|
|
213
|
+
|
|
214
|
+
X, y = _convert_to_supported(policy, X, y)
|
|
215
|
+
params = self._get_onedal_params(get_dtype(X))
|
|
216
|
+
X_table, y_table = to_table(X, y)
|
|
217
|
+
|
|
218
|
+
hparams = get_hyperparameters("linear_regression", "train")
|
|
219
|
+
if hparams is not None and not hparams.is_default:
|
|
220
|
+
result = module.train(policy, params, hparams.backend, X_table, y_table)
|
|
221
|
+
else:
|
|
222
|
+
result = module.train(policy, params, X_table, y_table)
|
|
223
|
+
|
|
224
|
+
self._onedal_model = result.model
|
|
225
|
+
|
|
226
|
+
packed_coefficients = from_table(result.model.packed_coefficients)
|
|
227
|
+
self.coef_, self.intercept_ = (
|
|
228
|
+
packed_coefficients[:, 1:],
|
|
229
|
+
packed_coefficients[:, 0],
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
if self.coef_.shape[0] == 1 and y.ndim == 1:
|
|
233
|
+
self.coef_ = self.coef_.ravel()
|
|
234
|
+
self.intercept_ = self.intercept_[0]
|
|
235
|
+
|
|
236
|
+
return self
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
class Ridge(BaseLinearRegression):
|
|
240
|
+
"""
|
|
241
|
+
Ridge Regression oneDAL implementation.
|
|
242
|
+
|
|
243
|
+
Parameters
|
|
244
|
+
----------
|
|
245
|
+
alpha : float, default=1.0
|
|
246
|
+
Regularization strength; must be a positive float. Regularization
|
|
247
|
+
improves the conditioning of the problem and reduces the variance of
|
|
248
|
+
the estimates. Larger values specify stronger regularization.
|
|
249
|
+
|
|
250
|
+
fit_intercept : bool, default=True
|
|
251
|
+
Whether to calculate the intercept for this model. If set
|
|
252
|
+
to False, no intercept will be used in calculations
|
|
253
|
+
(i.e. data is expected to be centered).
|
|
254
|
+
|
|
255
|
+
copy_X : bool, default=True
|
|
256
|
+
If True, X will be copied; else, it may be overwritten.
|
|
257
|
+
|
|
258
|
+
algorithm : string, default="norm_eq"
|
|
259
|
+
Algorithm used for computation on oneDAL side.
|
|
260
|
+
"""
|
|
261
|
+
|
|
262
|
+
def __init__(
|
|
263
|
+
self,
|
|
264
|
+
alpha=1.0,
|
|
265
|
+
fit_intercept=True,
|
|
266
|
+
copy_X=False,
|
|
267
|
+
*,
|
|
268
|
+
algorithm="norm_eq",
|
|
269
|
+
**kwargs,
|
|
270
|
+
):
|
|
271
|
+
super().__init__(
|
|
272
|
+
fit_intercept=fit_intercept, alpha=alpha, copy_X=copy_X, algorithm=algorithm
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
def fit(self, X, y, queue=None):
|
|
276
|
+
"""
|
|
277
|
+
Fit linear model.
|
|
278
|
+
Parameters
|
|
279
|
+
----------
|
|
280
|
+
X : {array-like, sparse matrix} of shape (n_samples, n_features)
|
|
281
|
+
Training data.
|
|
282
|
+
|
|
283
|
+
y : array-like of shape (n_samples,) or (n_samples, n_targets)
|
|
284
|
+
Target values. Will be cast to X's dtype if necessary.
|
|
285
|
+
|
|
286
|
+
queue : dpctl.SyclQueue
|
|
287
|
+
If not None, use this queue for computations.
|
|
288
|
+
|
|
289
|
+
Returns
|
|
290
|
+
-------
|
|
291
|
+
self : object
|
|
292
|
+
Fitted Estimator.
|
|
293
|
+
"""
|
|
294
|
+
module = self._get_backend("linear_model", "regression")
|
|
295
|
+
|
|
296
|
+
X = _check_array(
|
|
297
|
+
X,
|
|
298
|
+
dtype=[np.float64, np.float32],
|
|
299
|
+
force_all_finite=False,
|
|
300
|
+
ensure_2d=False,
|
|
301
|
+
copy=self.copy_X,
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
y = np.asarray(y).astype(dtype=get_dtype(X))
|
|
305
|
+
|
|
306
|
+
X, y = _check_X_y(X, y, force_all_finite=False, accept_2d_y=True)
|
|
307
|
+
|
|
308
|
+
policy = self._get_policy(queue, X, y)
|
|
309
|
+
|
|
310
|
+
self.n_features_in_ = _num_features(X, fallback_1d=True)
|
|
311
|
+
|
|
312
|
+
X, y = _convert_to_supported(policy, X, y)
|
|
313
|
+
params = self._get_onedal_params(get_dtype(X))
|
|
314
|
+
X_table, y_table = to_table(X, y)
|
|
315
|
+
|
|
316
|
+
result = module.train(policy, params, X_table, y_table)
|
|
317
|
+
self._onedal_model = result.model
|
|
318
|
+
|
|
319
|
+
packed_coefficients = from_table(result.model.packed_coefficients)
|
|
320
|
+
self.coef_, self.intercept_ = (
|
|
321
|
+
packed_coefficients[:, 1:],
|
|
322
|
+
packed_coefficients[:, 0],
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
if self.coef_.shape[0] == 1 and y.ndim == 1:
|
|
326
|
+
self.coef_ = self.coef_.ravel()
|
|
327
|
+
self.intercept_ = self.intercept_[0]
|
|
328
|
+
|
|
329
|
+
return self
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# ==============================================================================
|
|
2
|
+
# Copyright 2023 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
|
+
from abc import ABCMeta, abstractmethod
|
|
18
|
+
from numbers import Number
|
|
19
|
+
|
|
20
|
+
import numpy as np
|
|
21
|
+
|
|
22
|
+
from daal4py.sklearn._utils import daal_check_version, get_dtype, make2d
|
|
23
|
+
|
|
24
|
+
from ..common._base import BaseEstimator as onedal_BaseEstimator
|
|
25
|
+
from ..common._estimator_checks import _check_is_fitted
|
|
26
|
+
from ..common._mixin import ClassifierMixin
|
|
27
|
+
from ..datatypes import _convert_to_supported, from_table, to_table
|
|
28
|
+
from ..utils import (
|
|
29
|
+
_check_array,
|
|
30
|
+
_check_n_features,
|
|
31
|
+
_check_X_y,
|
|
32
|
+
_is_csr,
|
|
33
|
+
_num_features,
|
|
34
|
+
_type_of_target,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class BaseLogisticRegression(onedal_BaseEstimator, metaclass=ABCMeta):
|
|
39
|
+
@abstractmethod
|
|
40
|
+
def __init__(self, tol, C, fit_intercept, solver, max_iter, algorithm):
|
|
41
|
+
self.tol = tol
|
|
42
|
+
self.C = C
|
|
43
|
+
self.fit_intercept = fit_intercept
|
|
44
|
+
self.solver = solver
|
|
45
|
+
self.max_iter = max_iter
|
|
46
|
+
self.algorithm = algorithm
|
|
47
|
+
|
|
48
|
+
def _get_onedal_params(self, is_csr, dtype=np.float32):
|
|
49
|
+
intercept = "intercept|" if self.fit_intercept else ""
|
|
50
|
+
return {
|
|
51
|
+
"fptype": "float" if dtype == np.float32 else "double",
|
|
52
|
+
"method": "sparse" if is_csr else self.algorithm,
|
|
53
|
+
"intercept": self.fit_intercept,
|
|
54
|
+
"tol": self.tol,
|
|
55
|
+
"max_iter": self.max_iter,
|
|
56
|
+
"C": self.C,
|
|
57
|
+
"optimizer": self.solver,
|
|
58
|
+
"result_option": (
|
|
59
|
+
intercept
|
|
60
|
+
+ "coefficients|iterations_count"
|
|
61
|
+
+ ("|inner_iterations_count" if self.solver == "newton-cg" else "")
|
|
62
|
+
),
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
def _fit(self, X, y, module, queue):
|
|
66
|
+
sparsity_enabled = daal_check_version((2024, "P", 700))
|
|
67
|
+
X, y = _check_X_y(
|
|
68
|
+
X,
|
|
69
|
+
y,
|
|
70
|
+
accept_sparse=sparsity_enabled,
|
|
71
|
+
force_all_finite=True,
|
|
72
|
+
accept_2d_y=False,
|
|
73
|
+
dtype=[np.float64, np.float32],
|
|
74
|
+
)
|
|
75
|
+
is_csr = _is_csr(X)
|
|
76
|
+
|
|
77
|
+
self.n_features_in_ = _num_features(X, fallback_1d=True)
|
|
78
|
+
|
|
79
|
+
if _type_of_target(y) != "binary":
|
|
80
|
+
raise ValueError("Only binary classification is supported")
|
|
81
|
+
|
|
82
|
+
self.classes_, y = np.unique(y, return_inverse=True)
|
|
83
|
+
y = y.astype(dtype=np.int32)
|
|
84
|
+
|
|
85
|
+
policy = self._get_policy(queue, X, y)
|
|
86
|
+
X, y = _convert_to_supported(policy, X, y)
|
|
87
|
+
params = self._get_onedal_params(is_csr, get_dtype(X))
|
|
88
|
+
X_table, y_table = to_table(X, y)
|
|
89
|
+
|
|
90
|
+
result = module.train(policy, params, X_table, y_table)
|
|
91
|
+
|
|
92
|
+
self._onedal_model = result.model
|
|
93
|
+
self.n_iter_ = np.array([result.iterations_count])
|
|
94
|
+
|
|
95
|
+
# _n_inner_iter is the total number of cg-solver iterations
|
|
96
|
+
if daal_check_version((2024, "P", 300)) and self.solver == "newton-cg":
|
|
97
|
+
self._n_inner_iter = result.inner_iterations_count
|
|
98
|
+
|
|
99
|
+
coeff = from_table(result.model.packed_coefficients)
|
|
100
|
+
self.coef_, self.intercept_ = coeff[:, 1:], coeff[:, 0]
|
|
101
|
+
|
|
102
|
+
return self
|
|
103
|
+
|
|
104
|
+
def _create_model(self, module, policy):
|
|
105
|
+
m = module.model()
|
|
106
|
+
|
|
107
|
+
coefficients = self.coef_
|
|
108
|
+
dtype = get_dtype(coefficients)
|
|
109
|
+
coefficients = np.asarray(coefficients, dtype=dtype)
|
|
110
|
+
|
|
111
|
+
if coefficients.ndim == 2:
|
|
112
|
+
n_features_in = coefficients.shape[1]
|
|
113
|
+
assert coefficients.shape[0] == 1
|
|
114
|
+
else:
|
|
115
|
+
n_features_in = coefficients.size
|
|
116
|
+
|
|
117
|
+
intercept = self.intercept_
|
|
118
|
+
if not isinstance(intercept, Number):
|
|
119
|
+
intercept = np.asarray(intercept, dtype=dtype)
|
|
120
|
+
assert intercept.size == 1
|
|
121
|
+
|
|
122
|
+
intercept = _check_array(
|
|
123
|
+
intercept,
|
|
124
|
+
dtype=[np.float64, np.float32],
|
|
125
|
+
force_all_finite=True,
|
|
126
|
+
ensure_2d=False,
|
|
127
|
+
)
|
|
128
|
+
coefficients = _check_array(
|
|
129
|
+
coefficients,
|
|
130
|
+
dtype=[np.float64, np.float32],
|
|
131
|
+
force_all_finite=True,
|
|
132
|
+
ensure_2d=False,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
coefficients, intercept = make2d(coefficients), make2d(intercept)
|
|
136
|
+
|
|
137
|
+
assert coefficients.shape == (1, n_features_in)
|
|
138
|
+
assert intercept.shape == (1, 1)
|
|
139
|
+
|
|
140
|
+
desired_shape = (1, n_features_in + 1)
|
|
141
|
+
packed_coefficients = np.zeros(desired_shape, dtype=dtype)
|
|
142
|
+
|
|
143
|
+
packed_coefficients[:, 1:] = coefficients
|
|
144
|
+
if self.fit_intercept:
|
|
145
|
+
packed_coefficients[:, 0][:, np.newaxis] = intercept
|
|
146
|
+
|
|
147
|
+
packed_coefficients = _convert_to_supported(policy, packed_coefficients)
|
|
148
|
+
|
|
149
|
+
m.packed_coefficients = to_table(packed_coefficients)
|
|
150
|
+
|
|
151
|
+
self._onedal_model = m
|
|
152
|
+
|
|
153
|
+
return m
|
|
154
|
+
|
|
155
|
+
def _infer(self, X, module, queue):
|
|
156
|
+
_check_is_fitted(self)
|
|
157
|
+
sparsity_enabled = daal_check_version((2024, "P", 700))
|
|
158
|
+
|
|
159
|
+
X = _check_array(
|
|
160
|
+
X,
|
|
161
|
+
dtype=[np.float64, np.float32],
|
|
162
|
+
accept_sparse=sparsity_enabled,
|
|
163
|
+
force_all_finite=True,
|
|
164
|
+
ensure_2d=False,
|
|
165
|
+
accept_large_sparse=sparsity_enabled,
|
|
166
|
+
)
|
|
167
|
+
is_csr = _is_csr(X)
|
|
168
|
+
_check_n_features(self, X, False)
|
|
169
|
+
|
|
170
|
+
X = make2d(X)
|
|
171
|
+
policy = self._get_policy(queue, X)
|
|
172
|
+
|
|
173
|
+
if hasattr(self, "_onedal_model"):
|
|
174
|
+
model = self._onedal_model
|
|
175
|
+
else:
|
|
176
|
+
model = self._create_model(module, policy)
|
|
177
|
+
|
|
178
|
+
X = _convert_to_supported(policy, X)
|
|
179
|
+
params = self._get_onedal_params(is_csr, get_dtype(X))
|
|
180
|
+
|
|
181
|
+
X_table = to_table(X)
|
|
182
|
+
result = module.infer(policy, params, model, X_table)
|
|
183
|
+
return result
|
|
184
|
+
|
|
185
|
+
def _predict(self, X, module, queue):
|
|
186
|
+
result = self._infer(X, module, queue)
|
|
187
|
+
y = from_table(result.responses)
|
|
188
|
+
y = np.take(self.classes_, y.ravel(), axis=0)
|
|
189
|
+
return y
|
|
190
|
+
|
|
191
|
+
def _predict_proba(self, X, module, queue):
|
|
192
|
+
result = self._infer(X, module, queue)
|
|
193
|
+
|
|
194
|
+
y = from_table(result.probabilities)
|
|
195
|
+
y = y.reshape(-1, 1)
|
|
196
|
+
return np.hstack([1 - y, y])
|
|
197
|
+
|
|
198
|
+
def _predict_log_proba(self, X, module, queue):
|
|
199
|
+
y_proba = self._predict_proba(X, module, queue)
|
|
200
|
+
return np.log(y_proba)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class LogisticRegression(ClassifierMixin, BaseLogisticRegression):
|
|
204
|
+
"""
|
|
205
|
+
Logistic Regression oneDAL implementation.
|
|
206
|
+
"""
|
|
207
|
+
|
|
208
|
+
def __init__(
|
|
209
|
+
self,
|
|
210
|
+
tol=1e-4,
|
|
211
|
+
C=1.0,
|
|
212
|
+
fit_intercept=True,
|
|
213
|
+
solver="newton-cg",
|
|
214
|
+
max_iter=100,
|
|
215
|
+
*,
|
|
216
|
+
algorithm="dense_batch",
|
|
217
|
+
**kwargs,
|
|
218
|
+
):
|
|
219
|
+
super().__init__(
|
|
220
|
+
tol=tol,
|
|
221
|
+
C=C,
|
|
222
|
+
fit_intercept=fit_intercept,
|
|
223
|
+
solver=solver,
|
|
224
|
+
max_iter=max_iter,
|
|
225
|
+
algorithm=algorithm,
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
def fit(self, X, y, queue=None):
|
|
229
|
+
return super()._fit(
|
|
230
|
+
X, y, self._get_backend("logistic_regression", "classification", None), queue
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
def predict(self, X, queue=None):
|
|
234
|
+
y = super()._predict(
|
|
235
|
+
X, self._get_backend("logistic_regression", "classification", None), queue
|
|
236
|
+
)
|
|
237
|
+
return y
|
|
238
|
+
|
|
239
|
+
def predict_proba(self, X, queue=None):
|
|
240
|
+
y = super()._predict_proba(
|
|
241
|
+
X, self._get_backend("logistic_regression", "classification", None), queue
|
|
242
|
+
)
|
|
243
|
+
return y
|
|
244
|
+
|
|
245
|
+
def predict_log_proba(self, X, queue=None):
|
|
246
|
+
y = super()._predict_log_proba(
|
|
247
|
+
X, self._get_backend("logistic_regression", "classification", None), queue
|
|
248
|
+
)
|
|
249
|
+
return y
|