scikit-learn-intelex 2025.0.0__py39-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-39-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-39-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-39-x86_64-linux-gnu.so +0 -0
- onedal/_onedal_py_host.cpython-39-x86_64-linux-gnu.so +0 -0
- onedal/_onedal_py_spmd_dpc.cpython-39-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,19 @@
|
|
|
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 .covariance import EmpiricalCovariance
|
|
18
|
+
|
|
19
|
+
__all__ = ["EmpiricalCovariance"]
|
|
@@ -0,0 +1,133 @@
|
|
|
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
|
+
import warnings
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
from scipy import sparse as sp
|
|
21
|
+
from sklearn.covariance import EmpiricalCovariance as sklearn_EmpiricalCovariance
|
|
22
|
+
from sklearn.utils import check_array
|
|
23
|
+
|
|
24
|
+
from daal4py.sklearn._n_jobs_support import control_n_jobs
|
|
25
|
+
from daal4py.sklearn._utils import daal_check_version, sklearn_check_version
|
|
26
|
+
from onedal.common.hyperparameters import get_hyperparameters
|
|
27
|
+
from onedal.covariance import EmpiricalCovariance as onedal_EmpiricalCovariance
|
|
28
|
+
from sklearnex import config_context
|
|
29
|
+
from sklearnex.metrics import pairwise_distances
|
|
30
|
+
|
|
31
|
+
from ..._device_offload import dispatch, wrap_output_data
|
|
32
|
+
from ..._utils import PatchingConditionsChain, register_hyperparameters
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@register_hyperparameters({"fit": get_hyperparameters("covariance", "compute")})
|
|
36
|
+
@control_n_jobs(decorated_methods=["fit", "mahalanobis"])
|
|
37
|
+
class EmpiricalCovariance(sklearn_EmpiricalCovariance):
|
|
38
|
+
__doc__ = sklearn_EmpiricalCovariance.__doc__
|
|
39
|
+
|
|
40
|
+
if sklearn_check_version("1.2"):
|
|
41
|
+
_parameter_constraints: dict = {
|
|
42
|
+
**sklearn_EmpiricalCovariance._parameter_constraints,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
def _save_attributes(self):
|
|
46
|
+
assert hasattr(self, "_onedal_estimator")
|
|
47
|
+
if not daal_check_version((2024, "P", 400)) and self.assume_centered:
|
|
48
|
+
location = self._onedal_estimator.location_[None, :]
|
|
49
|
+
self._onedal_estimator.covariance_ += np.dot(location.T, location)
|
|
50
|
+
self._onedal_estimator.location_ = np.zeros_like(np.squeeze(location))
|
|
51
|
+
self._set_covariance(self._onedal_estimator.covariance_)
|
|
52
|
+
self.location_ = self._onedal_estimator.location_
|
|
53
|
+
|
|
54
|
+
_onedal_covariance = staticmethod(onedal_EmpiricalCovariance)
|
|
55
|
+
|
|
56
|
+
def _onedal_fit(self, X, queue=None):
|
|
57
|
+
if X.shape[0] == 1:
|
|
58
|
+
warnings.warn(
|
|
59
|
+
"Only one sample available. You may want to reshape your data array"
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
onedal_params = {
|
|
63
|
+
"method": "dense",
|
|
64
|
+
"bias": True,
|
|
65
|
+
"assume_centered": self.assume_centered,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
self._onedal_estimator = self._onedal_covariance(**onedal_params)
|
|
69
|
+
self._onedal_estimator.fit(X, queue=queue)
|
|
70
|
+
self._save_attributes()
|
|
71
|
+
|
|
72
|
+
def _onedal_supported(self, method_name, *data):
|
|
73
|
+
class_name = self.__class__.__name__
|
|
74
|
+
patching_status = PatchingConditionsChain(
|
|
75
|
+
f"sklearn.covariance.{class_name}.{method_name}"
|
|
76
|
+
)
|
|
77
|
+
if method_name in ["fit", "mahalanobis"]:
|
|
78
|
+
(X,) = data
|
|
79
|
+
patching_status.and_conditions(
|
|
80
|
+
[
|
|
81
|
+
(not sp.issparse(X), "X is sparse. Sparse input is not supported."),
|
|
82
|
+
]
|
|
83
|
+
)
|
|
84
|
+
return patching_status
|
|
85
|
+
raise RuntimeError(f"Unknown method {method_name} in {self.__class__.__name__}")
|
|
86
|
+
|
|
87
|
+
_onedal_cpu_supported = _onedal_supported
|
|
88
|
+
_onedal_gpu_supported = _onedal_supported
|
|
89
|
+
|
|
90
|
+
def fit(self, X, y=None):
|
|
91
|
+
if sklearn_check_version("1.2"):
|
|
92
|
+
self._validate_params()
|
|
93
|
+
if sklearn_check_version("0.23"):
|
|
94
|
+
X = self._validate_data(X, force_all_finite=False)
|
|
95
|
+
else:
|
|
96
|
+
X = check_array(X, force_all_finite=False)
|
|
97
|
+
|
|
98
|
+
dispatch(
|
|
99
|
+
self,
|
|
100
|
+
"fit",
|
|
101
|
+
{
|
|
102
|
+
"onedal": self.__class__._onedal_fit,
|
|
103
|
+
"sklearn": sklearn_EmpiricalCovariance.fit,
|
|
104
|
+
},
|
|
105
|
+
X,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
return self
|
|
109
|
+
|
|
110
|
+
# expose sklearnex pairwise_distances if mahalanobis distance eventually supported
|
|
111
|
+
@wrap_output_data
|
|
112
|
+
def mahalanobis(self, X):
|
|
113
|
+
if sklearn_check_version("1.0"):
|
|
114
|
+
X = self._validate_data(X, reset=False)
|
|
115
|
+
else:
|
|
116
|
+
X = check_array(X)
|
|
117
|
+
|
|
118
|
+
precision = self.get_precision()
|
|
119
|
+
with config_context(assume_finite=True):
|
|
120
|
+
# compute mahalanobis distances
|
|
121
|
+
dist = pairwise_distances(
|
|
122
|
+
X, self.location_[np.newaxis, :], metric="mahalanobis", VI=precision
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
return np.reshape(dist, (len(X),)) ** 2
|
|
126
|
+
|
|
127
|
+
error_norm = wrap_output_data(sklearn_EmpiricalCovariance.error_norm)
|
|
128
|
+
score = wrap_output_data(sklearn_EmpiricalCovariance.score)
|
|
129
|
+
|
|
130
|
+
fit.__doc__ = sklearn_EmpiricalCovariance.fit.__doc__
|
|
131
|
+
mahalanobis.__doc__ = sklearn_EmpiricalCovariance.mahalanobis
|
|
132
|
+
error_norm.__doc__ = sklearn_EmpiricalCovariance.error_norm.__doc__
|
|
133
|
+
score.__doc__ = sklearn_EmpiricalCovariance.score.__doc__
|
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
import numpy as np
|
|
18
|
+
import pytest
|
|
19
|
+
from numpy.testing import assert_allclose
|
|
20
|
+
|
|
21
|
+
from daal4py.sklearn._utils import daal_check_version
|
|
22
|
+
from onedal.tests.utils._dataframes_support import (
|
|
23
|
+
_convert_to_dataframe,
|
|
24
|
+
get_dataframes_and_queues,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
29
|
+
@pytest.mark.parametrize("macro_block", [None, 1024])
|
|
30
|
+
@pytest.mark.parametrize("assume_centered", [True, False])
|
|
31
|
+
def test_sklearnex_import_covariance(dataframe, queue, macro_block, assume_centered):
|
|
32
|
+
from sklearnex.preview.covariance import EmpiricalCovariance
|
|
33
|
+
|
|
34
|
+
X = np.array([[0, 1], [0, 1]])
|
|
35
|
+
|
|
36
|
+
X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
|
|
37
|
+
empcov = EmpiricalCovariance(assume_centered=assume_centered)
|
|
38
|
+
if daal_check_version((2024, "P", 0)) and macro_block is not None:
|
|
39
|
+
hparams = empcov.get_hyperparameters("fit")
|
|
40
|
+
hparams.cpu_macro_block = macro_block
|
|
41
|
+
result = empcov.fit(X)
|
|
42
|
+
|
|
43
|
+
expected_covariance = np.array([[0, 0], [0, 0]])
|
|
44
|
+
expected_means = np.array([0, 0])
|
|
45
|
+
|
|
46
|
+
if assume_centered:
|
|
47
|
+
expected_covariance = np.array([[0, 0], [0, 1]])
|
|
48
|
+
else:
|
|
49
|
+
expected_means = np.array([0, 1])
|
|
50
|
+
|
|
51
|
+
assert_allclose(expected_covariance, result.covariance_)
|
|
52
|
+
assert_allclose(expected_means, result.location_)
|
|
53
|
+
|
|
54
|
+
X = np.array([[1, 2], [3, 6]])
|
|
55
|
+
|
|
56
|
+
X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
|
|
57
|
+
result = empcov.fit(X)
|
|
58
|
+
|
|
59
|
+
if assume_centered:
|
|
60
|
+
expected_covariance = np.array([[5, 10], [10, 20]])
|
|
61
|
+
else:
|
|
62
|
+
expected_covariance = np.array([[1, 2], [2, 4]])
|
|
63
|
+
expected_means = np.array([2, 4])
|
|
64
|
+
|
|
65
|
+
assert_allclose(expected_covariance, result.covariance_)
|
|
66
|
+
assert_allclose(expected_means, result.location_)
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
from .incremental_pca import IncrementalPCA
|
|
18
|
+
|
|
19
|
+
__all__ = ["IncrementalPCA"]
|
|
@@ -0,0 +1,228 @@
|
|
|
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
|
+
from sklearn.decomposition import IncrementalPCA as sklearn_IncrementalPCA
|
|
19
|
+
from sklearn.utils import check_array, gen_batches
|
|
20
|
+
|
|
21
|
+
from daal4py.sklearn._n_jobs_support import control_n_jobs
|
|
22
|
+
from daal4py.sklearn._utils import sklearn_check_version
|
|
23
|
+
from onedal.decomposition import IncrementalPCA as onedal_IncrementalPCA
|
|
24
|
+
|
|
25
|
+
from ..._device_offload import dispatch, wrap_output_data
|
|
26
|
+
from ..._utils import PatchingConditionsChain
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@control_n_jobs(
|
|
30
|
+
decorated_methods=["fit", "partial_fit", "transform", "_onedal_finalize_fit"]
|
|
31
|
+
)
|
|
32
|
+
class IncrementalPCA(sklearn_IncrementalPCA):
|
|
33
|
+
|
|
34
|
+
def __init__(self, n_components=None, *, whiten=False, copy=True, batch_size=None):
|
|
35
|
+
super().__init__(
|
|
36
|
+
n_components=n_components, whiten=whiten, copy=copy, batch_size=batch_size
|
|
37
|
+
)
|
|
38
|
+
self._need_to_finalize = False
|
|
39
|
+
self._need_to_finalize_attrs = {
|
|
40
|
+
"mean_",
|
|
41
|
+
"explained_variance_",
|
|
42
|
+
"explained_variance_ratio_",
|
|
43
|
+
"n_components_",
|
|
44
|
+
"components_",
|
|
45
|
+
"noise_variance_",
|
|
46
|
+
"singular_values_",
|
|
47
|
+
"var_",
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_onedal_incremental_pca = staticmethod(onedal_IncrementalPCA)
|
|
51
|
+
|
|
52
|
+
def _onedal_transform(self, X, queue=None):
|
|
53
|
+
assert hasattr(self, "_onedal_estimator")
|
|
54
|
+
if self._need_to_finalize:
|
|
55
|
+
self._onedal_finalize_fit()
|
|
56
|
+
X = check_array(X, dtype=[np.float64, np.float32])
|
|
57
|
+
return self._onedal_estimator.predict(X, queue)
|
|
58
|
+
|
|
59
|
+
def _onedal_fit_transform(self, X, queue=None):
|
|
60
|
+
self._onedal_fit(X, queue)
|
|
61
|
+
return self._onedal_transform(X, queue)
|
|
62
|
+
|
|
63
|
+
def _onedal_partial_fit(self, X, check_input=True, queue=None):
|
|
64
|
+
first_pass = not hasattr(self, "_onedal_estimator")
|
|
65
|
+
|
|
66
|
+
if check_input:
|
|
67
|
+
if sklearn_check_version("1.0"):
|
|
68
|
+
X = self._validate_data(
|
|
69
|
+
X, dtype=[np.float64, np.float32], reset=first_pass
|
|
70
|
+
)
|
|
71
|
+
else:
|
|
72
|
+
X = check_array(
|
|
73
|
+
X,
|
|
74
|
+
dtype=[np.float64, np.float32],
|
|
75
|
+
copy=self.copy,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
n_samples, n_features = X.shape
|
|
79
|
+
|
|
80
|
+
if self.n_components is None:
|
|
81
|
+
if not hasattr(self, "_components_shape"):
|
|
82
|
+
self.n_components_ = min(n_samples, n_features)
|
|
83
|
+
self._components_shape = self.n_components_
|
|
84
|
+
|
|
85
|
+
elif not self.n_components <= n_features:
|
|
86
|
+
raise ValueError(
|
|
87
|
+
"n_components=%r invalid for n_features=%d, need "
|
|
88
|
+
"more rows than columns for IncrementalPCA "
|
|
89
|
+
"processing" % (self.n_components, n_features)
|
|
90
|
+
)
|
|
91
|
+
elif not self.n_components <= n_samples:
|
|
92
|
+
raise ValueError(
|
|
93
|
+
"n_components=%r must be less or equal to "
|
|
94
|
+
"the batch number of samples "
|
|
95
|
+
"%d." % (self.n_components, n_samples)
|
|
96
|
+
)
|
|
97
|
+
else:
|
|
98
|
+
self.n_components_ = self.n_components
|
|
99
|
+
|
|
100
|
+
if not hasattr(self, "n_samples_seen_"):
|
|
101
|
+
self.n_samples_seen_ = n_samples
|
|
102
|
+
else:
|
|
103
|
+
self.n_samples_seen_ += n_samples
|
|
104
|
+
|
|
105
|
+
onedal_params = {"n_components": self.n_components_, "whiten": self.whiten}
|
|
106
|
+
|
|
107
|
+
if not hasattr(self, "_onedal_estimator"):
|
|
108
|
+
self._onedal_estimator = self._onedal_incremental_pca(**onedal_params)
|
|
109
|
+
self._onedal_estimator.partial_fit(X, queue=queue)
|
|
110
|
+
self._need_to_finalize = True
|
|
111
|
+
|
|
112
|
+
def _onedal_finalize_fit(self, queue=None):
|
|
113
|
+
assert hasattr(self, "_onedal_estimator")
|
|
114
|
+
self._onedal_estimator.finalize_fit(queue=queue)
|
|
115
|
+
self._need_to_finalize = False
|
|
116
|
+
|
|
117
|
+
def _onedal_fit(self, X, queue=None):
|
|
118
|
+
if sklearn_check_version("1.2"):
|
|
119
|
+
self._validate_params()
|
|
120
|
+
|
|
121
|
+
if sklearn_check_version("1.0"):
|
|
122
|
+
X = self._validate_data(X, dtype=[np.float64, np.float32], copy=self.copy)
|
|
123
|
+
else:
|
|
124
|
+
X = check_array(
|
|
125
|
+
X,
|
|
126
|
+
dtype=[np.float64, np.float32],
|
|
127
|
+
copy=self.copy,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
n_samples, n_features = X.shape
|
|
131
|
+
|
|
132
|
+
if self.batch_size is None:
|
|
133
|
+
self.batch_size_ = 5 * n_features
|
|
134
|
+
else:
|
|
135
|
+
self.batch_size_ = self.batch_size
|
|
136
|
+
|
|
137
|
+
self.n_samples_seen_ = 0
|
|
138
|
+
if hasattr(self, "_onedal_estimator"):
|
|
139
|
+
self._onedal_estimator._reset()
|
|
140
|
+
|
|
141
|
+
for batch in gen_batches(n_samples, self.batch_size_):
|
|
142
|
+
X_batch = X[batch]
|
|
143
|
+
self._onedal_partial_fit(X_batch, queue=queue)
|
|
144
|
+
|
|
145
|
+
self._onedal_finalize_fit(queue=queue)
|
|
146
|
+
|
|
147
|
+
return self
|
|
148
|
+
|
|
149
|
+
def _onedal_supported(self, method_name, *data):
|
|
150
|
+
patching_status = PatchingConditionsChain(
|
|
151
|
+
f"sklearn.decomposition.{self.__class__.__name__}.{method_name}"
|
|
152
|
+
)
|
|
153
|
+
return patching_status
|
|
154
|
+
|
|
155
|
+
_onedal_cpu_supported = _onedal_supported
|
|
156
|
+
_onedal_gpu_supported = _onedal_supported
|
|
157
|
+
|
|
158
|
+
def __getattr__(self, attr):
|
|
159
|
+
if attr in self._need_to_finalize_attrs:
|
|
160
|
+
if hasattr(self, "_onedal_estimator"):
|
|
161
|
+
if self._need_to_finalize:
|
|
162
|
+
self._onedal_finalize_fit()
|
|
163
|
+
return getattr(self._onedal_estimator, attr)
|
|
164
|
+
else:
|
|
165
|
+
raise AttributeError(
|
|
166
|
+
f"'{self.__class__.__name__}' object has no attribute '{attr}'"
|
|
167
|
+
)
|
|
168
|
+
if attr in self.__dict__:
|
|
169
|
+
return self.__dict__[attr]
|
|
170
|
+
|
|
171
|
+
raise AttributeError(
|
|
172
|
+
f"'{self.__class__.__name__}' object has no attribute '{attr}'"
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
def partial_fit(self, X, y=None, check_input=True):
|
|
176
|
+
dispatch(
|
|
177
|
+
self,
|
|
178
|
+
"partial_fit",
|
|
179
|
+
{
|
|
180
|
+
"onedal": self.__class__._onedal_partial_fit,
|
|
181
|
+
"sklearn": sklearn_IncrementalPCA.partial_fit,
|
|
182
|
+
},
|
|
183
|
+
X,
|
|
184
|
+
check_input=check_input,
|
|
185
|
+
)
|
|
186
|
+
return self
|
|
187
|
+
|
|
188
|
+
def fit(self, X, y=None):
|
|
189
|
+
dispatch(
|
|
190
|
+
self,
|
|
191
|
+
"fit",
|
|
192
|
+
{
|
|
193
|
+
"onedal": self.__class__._onedal_fit,
|
|
194
|
+
"sklearn": sklearn_IncrementalPCA.fit,
|
|
195
|
+
},
|
|
196
|
+
X,
|
|
197
|
+
)
|
|
198
|
+
return self
|
|
199
|
+
|
|
200
|
+
@wrap_output_data
|
|
201
|
+
def transform(self, X):
|
|
202
|
+
return dispatch(
|
|
203
|
+
self,
|
|
204
|
+
"transform",
|
|
205
|
+
{
|
|
206
|
+
"onedal": self.__class__._onedal_transform,
|
|
207
|
+
"sklearn": sklearn_IncrementalPCA.transform,
|
|
208
|
+
},
|
|
209
|
+
X,
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
@wrap_output_data
|
|
213
|
+
def fit_transform(self, X, y=None, **fit_params):
|
|
214
|
+
return dispatch(
|
|
215
|
+
self,
|
|
216
|
+
"fit_transform",
|
|
217
|
+
{
|
|
218
|
+
"onedal": self.__class__._onedal_fit_transform,
|
|
219
|
+
"sklearn": sklearn_IncrementalPCA.fit_transform,
|
|
220
|
+
},
|
|
221
|
+
X,
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
__doc__ = sklearn_IncrementalPCA.__doc__
|
|
225
|
+
fit.__doc__ = sklearn_IncrementalPCA.fit.__doc__
|
|
226
|
+
fit_transform.__doc__ = sklearn_IncrementalPCA.fit_transform.__doc__
|
|
227
|
+
transform.__doc__ = sklearn_IncrementalPCA.transform.__doc__
|
|
228
|
+
partial_fit.__doc__ = sklearn_IncrementalPCA.partial_fit.__doc__
|