scikit-learn-intelex 2024.0.1__py312-none-win_amd64.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.
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/__init__.py +61 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/__main__.py +59 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/_config.py +110 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/_device_offload.py +223 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/_utils.py +95 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/basic_statistics/__init__.py +20 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/basic_statistics/basic_statistics.py +17 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/__init__.py +21 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/dbscan.py +187 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/k_means.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/tests/test_dbscan.py +37 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/tests/test_kmeans.py +31 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/decomposition/__init__.py +20 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/decomposition/pca.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/decomposition/tests/test_pca.py +28 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/dispatcher.py +329 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/doc/third-party-programs.txt +424 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/ensemble/__init__.py +30 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/ensemble/_forest.py +1947 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/ensemble/tests/test_forest.py +118 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/glob/__main__.py +73 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/glob/dispatcher.py +88 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/__init__.py +30 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/coordinate_descent.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/linear.py +373 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/logistic_path.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/ridge.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/tests/test_linear.py +77 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/tests/test_logreg.py +29 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/manifold/__init__.py +20 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/manifold/t_sne.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/manifold/tests/test_tsne.py +27 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/__init__.py +24 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/pairwise.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/ranking.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/tests/test_metrics.py +40 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/model_selection/__init__.py +22 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/model_selection/split.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/model_selection/tests/test_model_selection.py +35 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/__init__.py +28 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/common.py +264 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/knn_classification.py +331 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/knn_regression.py +307 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/knn_unsupervised.py +220 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/lof.py +437 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/tests/test_neighbors.py +85 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/__init__.py +18 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/__init__.py +20 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/_common.py +84 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/k_means.py +370 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/__init__.py +20 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/pca.py +376 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/tests/test_preview_pca.py +38 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/__init__.py +24 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/basic_statistics/__init__.py +19 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/basic_statistics/basic_statistics.py +21 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/cluster/__init__.py +30 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/cluster/dbscan.py +50 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/cluster/kmeans.py +21 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/decomposition/__init__.py +19 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/decomposition/pca.py +21 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/ensemble/__init__.py +19 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/ensemble/forest.py +79 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/linear_model/__init__.py +19 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/linear_model/linear_model.py +21 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/neighbors/__init__.py +19 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/neighbors/neighbors.py +25 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/__init__.py +30 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/_common.py +188 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/nusvc.py +272 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/nusvr.py +163 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/svc.py +301 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/svr.py +164 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/tests/test_svm.py +102 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/_models_info.py +170 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_config.py +39 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_memory_usage.py +225 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_monkeypatch.py +210 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_parallel.py +50 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_patching.py +122 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_run_to_run_stability_tests.py +428 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/utils/_launch_algorithms.py +118 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/utils/__init__.py +19 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/utils/parallel.py +59 -0
- scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/utils/validation.py +18 -0
- scikit_learn_intelex-2024.0.1.dist-info/LICENSE.txt +202 -0
- scikit_learn_intelex-2024.0.1.dist-info/METADATA +230 -0
- scikit_learn_intelex-2024.0.1.dist-info/RECORD +90 -0
- scikit_learn_intelex-2024.0.1.dist-info/WHEEL +5 -0
- scikit_learn_intelex-2024.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# ===============================================================================
|
|
3
|
+
# Copyright 2021 Intel Corporation
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
# ===============================================================================
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
import pytest
|
|
20
|
+
from numpy.testing import assert_allclose
|
|
21
|
+
|
|
22
|
+
from onedal.tests.utils._dataframes_support import (
|
|
23
|
+
_as_numpy,
|
|
24
|
+
_convert_to_dataframe,
|
|
25
|
+
get_dataframes_and_queues,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
30
|
+
def test_sklearnex_import_knn_classifier(dataframe, queue):
|
|
31
|
+
from sklearnex.neighbors import KNeighborsClassifier
|
|
32
|
+
|
|
33
|
+
X = _convert_to_dataframe([[0], [1], [2], [3]], sycl_queue=queue, target_df=dataframe)
|
|
34
|
+
y = _convert_to_dataframe([0, 0, 1, 1], sycl_queue=queue, target_df=dataframe)
|
|
35
|
+
neigh = KNeighborsClassifier(n_neighbors=3).fit(X, y)
|
|
36
|
+
y_test = _convert_to_dataframe([[1.1]], sycl_queue=queue, target_df=dataframe)
|
|
37
|
+
pred = _as_numpy(neigh.predict(y_test))
|
|
38
|
+
assert "sklearnex" in neigh.__module__
|
|
39
|
+
assert_allclose(pred, [0])
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
43
|
+
def test_sklearnex_import_knn_regression(dataframe, queue):
|
|
44
|
+
from sklearnex.neighbors import KNeighborsRegressor
|
|
45
|
+
|
|
46
|
+
X = _convert_to_dataframe([[0], [1], [2], [3]], sycl_queue=queue, target_df=dataframe)
|
|
47
|
+
y = _convert_to_dataframe([0, 0, 1, 1], sycl_queue=queue, target_df=dataframe)
|
|
48
|
+
neigh = KNeighborsRegressor(n_neighbors=2).fit(X, y)
|
|
49
|
+
y_test = _convert_to_dataframe([[1.5]], sycl_queue=queue, target_df=dataframe)
|
|
50
|
+
pred = _as_numpy(neigh.predict(y_test))
|
|
51
|
+
assert "sklearnex" in neigh.__module__
|
|
52
|
+
assert_allclose(pred, [0.5])
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# TODO:
|
|
56
|
+
# investigate failure for `dpnp.ndarrays` and `dpctl.tensors`.
|
|
57
|
+
@pytest.mark.parametrize(
|
|
58
|
+
"dataframe,queue", get_dataframes_and_queues(dataframe_filter_="numpy")
|
|
59
|
+
)
|
|
60
|
+
def test_sklearnex_import_nn(dataframe, queue):
|
|
61
|
+
from sklearnex.neighbors import NearestNeighbors
|
|
62
|
+
|
|
63
|
+
X = [[0, 0, 2], [1, 0, 0], [0, 0, 1]]
|
|
64
|
+
X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
|
|
65
|
+
test = _convert_to_dataframe([[0, 0, 1.3]], sycl_queue=queue, target_df=dataframe)
|
|
66
|
+
neigh = NearestNeighbors(n_neighbors=2).fit(X)
|
|
67
|
+
result = neigh.kneighbors(test, 2, return_distance=False)
|
|
68
|
+
result = _as_numpy(result)
|
|
69
|
+
assert "sklearnex" in neigh.__module__
|
|
70
|
+
assert_allclose(result, [[2, 0]])
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@pytest.mark.parametrize("dataframe,queue", get_dataframes_and_queues())
|
|
74
|
+
def test_sklearnex_import_lof(dataframe, queue):
|
|
75
|
+
from sklearnex.neighbors import LocalOutlierFactor
|
|
76
|
+
|
|
77
|
+
X = [[7, 7, 7], [1, 0, 0], [0, 0, 1], [0, 0, 1]]
|
|
78
|
+
X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
|
|
79
|
+
lof = LocalOutlierFactor(n_neighbors=2)
|
|
80
|
+
result = lof.fit_predict(X)
|
|
81
|
+
result = _as_numpy(result)
|
|
82
|
+
assert hasattr(lof, "_knn")
|
|
83
|
+
assert "sklearnex" in lof.__module__
|
|
84
|
+
assert "sklearnex" in lof._knn.__module__
|
|
85
|
+
assert_allclose(result, [-1, 1, 1, 1])
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# ==============================================================================
|
|
3
|
+
# Copyright 2023 Intel Corporation
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
# ==============================================================================
|
|
17
|
+
|
|
18
|
+
__all__ = ["cluster", "decomposition"]
|
scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/__init__.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# ==============================================================================
|
|
3
|
+
# Copyright 2023 Intel Corporation
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
# ==============================================================================
|
|
17
|
+
|
|
18
|
+
from .k_means import KMeans
|
|
19
|
+
|
|
20
|
+
__all__ = ["KMeans"]
|
scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/_common.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
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 ABC
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def get_cluster_centers(self):
|
|
21
|
+
return self._cluster_centers_
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def set_cluster_centers(self, value):
|
|
25
|
+
self._cluster_centers_ = value
|
|
26
|
+
if hasattr(self, "_onedal_estimator"):
|
|
27
|
+
self._onedal_estimator.cluster_centers_ = value
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def get_labels(self):
|
|
31
|
+
return self._labels_
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def set_labels(self, value):
|
|
35
|
+
self._labels_ = value
|
|
36
|
+
if hasattr(self, "_onedal_estimator"):
|
|
37
|
+
self._onedal_estimator.labels_ = value
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def get_inertia(self):
|
|
41
|
+
return self._inertia_
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def set_inertia(self, value):
|
|
45
|
+
self._inertia_ = value
|
|
46
|
+
if hasattr(self, "_onedal_estimator"):
|
|
47
|
+
self._onedal_estimator.inertia_ = value
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def get_n_iter(self):
|
|
51
|
+
return self._n_iter_
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def set_n_iter(self, value):
|
|
55
|
+
self._n_iter_ = value
|
|
56
|
+
if hasattr(self, "_onedal_estimator"):
|
|
57
|
+
self._onedal_estimator.n_iter_ = value
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class BaseKMeans(ABC):
|
|
61
|
+
def _save_attributes(self):
|
|
62
|
+
assert hasattr(self, "_onedal_estimator")
|
|
63
|
+
self.n_features_in_ = self._onedal_estimator.n_features_in_
|
|
64
|
+
self.fit_status_ = 0
|
|
65
|
+
self._tol = self._onedal_estimator._tol
|
|
66
|
+
self._n_init = self._onedal_estimator._n_init
|
|
67
|
+
self._n_iter_ = self._onedal_estimator.n_iter_
|
|
68
|
+
self._labels_ = self._onedal_estimator.labels_
|
|
69
|
+
self._inertia_ = self._onedal_estimator.inertia_
|
|
70
|
+
self._algorithm = self._onedal_estimator.algorithm
|
|
71
|
+
self._cluster_centers_ = self._onedal_estimator.cluster_centers_
|
|
72
|
+
self._sparse = False
|
|
73
|
+
|
|
74
|
+
self.n_iter_ = property(get_n_iter, set_n_iter)
|
|
75
|
+
self.labels_ = property(get_labels, set_labels)
|
|
76
|
+
self.inertia_ = property(get_labels, set_inertia)
|
|
77
|
+
self.cluster_centers_ = property(get_cluster_centers, set_cluster_centers)
|
|
78
|
+
|
|
79
|
+
self._is_in_fit = True
|
|
80
|
+
self.n_iter_ = self._n_iter_
|
|
81
|
+
self.labels_ = self._labels_
|
|
82
|
+
self.inertia_ = self._inertia_
|
|
83
|
+
self.cluster_centers_ = self._cluster_centers_
|
|
84
|
+
self._is_in_fit = False
|
scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/k_means.py
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# ==============================================================================
|
|
3
|
+
# Copyright 2023 Intel Corporation
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
# ==============================================================================
|
|
17
|
+
|
|
18
|
+
import logging
|
|
19
|
+
|
|
20
|
+
from daal4py.sklearn._utils import daal_check_version
|
|
21
|
+
|
|
22
|
+
if daal_check_version((2023, "P", 200)):
|
|
23
|
+
import numpy as np
|
|
24
|
+
from scipy.sparse import issparse
|
|
25
|
+
from sklearn.cluster import KMeans as sklearn_KMeans
|
|
26
|
+
from sklearn.utils._openmp_helpers import _openmp_effective_n_threads
|
|
27
|
+
from sklearn.utils.validation import (
|
|
28
|
+
_deprecate_positional_args,
|
|
29
|
+
_num_samples,
|
|
30
|
+
check_is_fitted,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
from daal4py.sklearn._utils import sklearn_check_version
|
|
34
|
+
from onedal.cluster import KMeans as onedal_KMeans
|
|
35
|
+
|
|
36
|
+
from ..._device_offload import dispatch, wrap_output_data
|
|
37
|
+
from ..._utils import PatchingConditionsChain
|
|
38
|
+
from ._common import BaseKMeans
|
|
39
|
+
|
|
40
|
+
class KMeans(sklearn_KMeans, BaseKMeans):
|
|
41
|
+
__doc__ = sklearn_KMeans.__doc__
|
|
42
|
+
n_iter_, inertia_ = None, None
|
|
43
|
+
labels_, cluster_centers_ = None, None
|
|
44
|
+
|
|
45
|
+
if sklearn_check_version("1.2"):
|
|
46
|
+
_parameter_constraints: dict = {**sklearn_KMeans._parameter_constraints}
|
|
47
|
+
|
|
48
|
+
@_deprecate_positional_args
|
|
49
|
+
def __init__(
|
|
50
|
+
self,
|
|
51
|
+
n_clusters=8,
|
|
52
|
+
*,
|
|
53
|
+
init="k-means++",
|
|
54
|
+
n_init="auto" if sklearn_check_version("1.4") else "warn",
|
|
55
|
+
max_iter=300,
|
|
56
|
+
tol=1e-4,
|
|
57
|
+
verbose=0,
|
|
58
|
+
random_state=None,
|
|
59
|
+
copy_x=True,
|
|
60
|
+
algorithm="lloyd",
|
|
61
|
+
):
|
|
62
|
+
super().__init__(
|
|
63
|
+
n_clusters=n_clusters,
|
|
64
|
+
init=init,
|
|
65
|
+
max_iter=max_iter,
|
|
66
|
+
tol=tol,
|
|
67
|
+
n_init=n_init,
|
|
68
|
+
verbose=verbose,
|
|
69
|
+
random_state=random_state,
|
|
70
|
+
copy_x=copy_x,
|
|
71
|
+
algorithm=algorithm,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
elif sklearn_check_version("1.0"):
|
|
75
|
+
|
|
76
|
+
@_deprecate_positional_args
|
|
77
|
+
def __init__(
|
|
78
|
+
self,
|
|
79
|
+
n_clusters=8,
|
|
80
|
+
*,
|
|
81
|
+
init="k-means++",
|
|
82
|
+
n_init=10,
|
|
83
|
+
max_iter=300,
|
|
84
|
+
tol=1e-4,
|
|
85
|
+
verbose=0,
|
|
86
|
+
random_state=None,
|
|
87
|
+
copy_x=True,
|
|
88
|
+
algorithm="auto",
|
|
89
|
+
):
|
|
90
|
+
super().__init__(
|
|
91
|
+
n_clusters=n_clusters,
|
|
92
|
+
init=init,
|
|
93
|
+
max_iter=max_iter,
|
|
94
|
+
tol=tol,
|
|
95
|
+
n_init=n_init,
|
|
96
|
+
verbose=verbose,
|
|
97
|
+
random_state=random_state,
|
|
98
|
+
copy_x=copy_x,
|
|
99
|
+
algorithm=algorithm,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
else:
|
|
103
|
+
|
|
104
|
+
@_deprecate_positional_args
|
|
105
|
+
def __init__(
|
|
106
|
+
self,
|
|
107
|
+
n_clusters=8,
|
|
108
|
+
*,
|
|
109
|
+
init="k-means++",
|
|
110
|
+
n_init=10,
|
|
111
|
+
max_iter=300,
|
|
112
|
+
tol=1e-4,
|
|
113
|
+
precompute_distances="deprecated",
|
|
114
|
+
verbose=0,
|
|
115
|
+
random_state=None,
|
|
116
|
+
copy_x=True,
|
|
117
|
+
n_jobs="deprecated",
|
|
118
|
+
algorithm="auto",
|
|
119
|
+
):
|
|
120
|
+
super().__init__(
|
|
121
|
+
n_clusters=n_clusters,
|
|
122
|
+
init=init,
|
|
123
|
+
max_iter=max_iter,
|
|
124
|
+
tol=tol,
|
|
125
|
+
precompute_distances=precompute_distances,
|
|
126
|
+
n_init=n_init,
|
|
127
|
+
verbose=verbose,
|
|
128
|
+
random_state=random_state,
|
|
129
|
+
copy_x=copy_x,
|
|
130
|
+
n_jobs=n_jobs,
|
|
131
|
+
algorithm=algorithm,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
def _initialize_onedal_estimator(self):
|
|
135
|
+
onedal_params = {
|
|
136
|
+
"n_clusters": self.n_clusters,
|
|
137
|
+
"init": self.init,
|
|
138
|
+
"max_iter": self.max_iter,
|
|
139
|
+
"tol": self.tol,
|
|
140
|
+
"n_init": self.n_init,
|
|
141
|
+
"verbose": self.verbose,
|
|
142
|
+
"random_state": self.random_state,
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
self._onedal_estimator = onedal_KMeans(**onedal_params)
|
|
146
|
+
|
|
147
|
+
def _onedal_fit_supported(self, method_name, X, y=None, sample_weight=None):
|
|
148
|
+
assert method_name == "fit"
|
|
149
|
+
|
|
150
|
+
class_name = self.__class__.__name__
|
|
151
|
+
patching_status = PatchingConditionsChain(f"sklearn.cluster.{class_name}.fit")
|
|
152
|
+
|
|
153
|
+
sample_count = _num_samples(X)
|
|
154
|
+
self._algorithm = self.algorithm
|
|
155
|
+
supported_algs = ["auto", "full", "lloyd"]
|
|
156
|
+
correct_count = self.n_clusters < sample_count
|
|
157
|
+
|
|
158
|
+
patching_status.and_conditions(
|
|
159
|
+
[
|
|
160
|
+
(
|
|
161
|
+
self.algorithm in supported_algs,
|
|
162
|
+
"Only lloyd algorithm is supported.",
|
|
163
|
+
),
|
|
164
|
+
(not issparse(self.init), "Sparse init values are not supported"),
|
|
165
|
+
(correct_count, "n_clusters is smaller than number of samples"),
|
|
166
|
+
(sample_weight is None, "Sample weight is not None."),
|
|
167
|
+
(not issparse(X), "Sparse input is not supported."),
|
|
168
|
+
]
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
return patching_status
|
|
172
|
+
|
|
173
|
+
def fit(self, X, y=None, sample_weight=None):
|
|
174
|
+
"""Compute k-means clustering.
|
|
175
|
+
|
|
176
|
+
Parameters
|
|
177
|
+
----------
|
|
178
|
+
X : array-like or sparse matrix, shape=(n_samples, n_features)
|
|
179
|
+
Training instances to cluster. It must be noted that the data
|
|
180
|
+
will be converted to C ordering, which will cause a memory
|
|
181
|
+
copy if the given data is not C-contiguous.
|
|
182
|
+
|
|
183
|
+
y : Ignored
|
|
184
|
+
not used, present here for API consistency by convention.
|
|
185
|
+
|
|
186
|
+
sample_weight : array-like, shape (n_samples,), optional
|
|
187
|
+
The weights for each observation in X. If None, all observations
|
|
188
|
+
are assigned equal weight (default: None)
|
|
189
|
+
|
|
190
|
+
"""
|
|
191
|
+
|
|
192
|
+
if sklearn_check_version("1.0"):
|
|
193
|
+
self._check_feature_names(X, reset=True)
|
|
194
|
+
if sklearn_check_version("1.2"):
|
|
195
|
+
self._validate_params()
|
|
196
|
+
|
|
197
|
+
dispatch(
|
|
198
|
+
self,
|
|
199
|
+
"fit",
|
|
200
|
+
{
|
|
201
|
+
"onedal": self.__class__._onedal_fit,
|
|
202
|
+
"sklearn": sklearn_KMeans.fit,
|
|
203
|
+
},
|
|
204
|
+
X,
|
|
205
|
+
y,
|
|
206
|
+
sample_weight,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
return self
|
|
210
|
+
|
|
211
|
+
def _onedal_fit(self, X, _, sample_weight, queue=None):
|
|
212
|
+
assert sample_weight is None
|
|
213
|
+
|
|
214
|
+
X = self._validate_data(
|
|
215
|
+
X,
|
|
216
|
+
accept_sparse=False,
|
|
217
|
+
dtype=[np.float64, np.float32],
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
if sklearn_check_version("1.2"):
|
|
221
|
+
self._check_params_vs_input(X)
|
|
222
|
+
else:
|
|
223
|
+
self._check_params(X)
|
|
224
|
+
|
|
225
|
+
self._n_features_out = self.n_clusters
|
|
226
|
+
self._n_threads = _openmp_effective_n_threads()
|
|
227
|
+
|
|
228
|
+
self._initialize_onedal_estimator()
|
|
229
|
+
self._onedal_estimator.fit(X, queue=queue)
|
|
230
|
+
|
|
231
|
+
self._save_attributes()
|
|
232
|
+
|
|
233
|
+
def _onedal_predict_supported(self, method_name, X):
|
|
234
|
+
assert method_name == "predict"
|
|
235
|
+
|
|
236
|
+
class_name = self.__class__.__name__
|
|
237
|
+
patching_status = PatchingConditionsChain(
|
|
238
|
+
f"sklearn.cluster.{class_name}.predict"
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
supported_algs = ["auto", "full", "lloyd"]
|
|
242
|
+
dense_centers = not issparse(self.cluster_centers_)
|
|
243
|
+
|
|
244
|
+
patching_status.and_conditions(
|
|
245
|
+
[
|
|
246
|
+
(
|
|
247
|
+
self.algorithm in supported_algs,
|
|
248
|
+
"Only lloyd algorithm is supported.",
|
|
249
|
+
),
|
|
250
|
+
(dense_centers, "Sparse clusters is not supported."),
|
|
251
|
+
(not issparse(X), "Sparse input is not supported."),
|
|
252
|
+
]
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
return patching_status
|
|
256
|
+
|
|
257
|
+
@wrap_output_data
|
|
258
|
+
def predict(self, X):
|
|
259
|
+
"""Compute k-means clustering.
|
|
260
|
+
|
|
261
|
+
Parameters
|
|
262
|
+
----------
|
|
263
|
+
X : array-like or sparse matrix, shape=(n_samples, n_features)
|
|
264
|
+
Training instances to cluster. It must be noted that the data
|
|
265
|
+
will be converted to C ordering, which will cause a memory
|
|
266
|
+
copy if the given data is not C-contiguous.
|
|
267
|
+
|
|
268
|
+
y : Ignored
|
|
269
|
+
not used, present here for API consistency by convention.
|
|
270
|
+
|
|
271
|
+
sample_weight : array-like, shape (n_samples,), optional
|
|
272
|
+
The weights for each observation in X. If None, all observations
|
|
273
|
+
are assigned equal weight (default: None)
|
|
274
|
+
|
|
275
|
+
"""
|
|
276
|
+
|
|
277
|
+
if sklearn_check_version("1.0"):
|
|
278
|
+
self._check_feature_names(X, reset=True)
|
|
279
|
+
if sklearn_check_version("1.2"):
|
|
280
|
+
self._validate_params()
|
|
281
|
+
|
|
282
|
+
return dispatch(
|
|
283
|
+
self,
|
|
284
|
+
"predict",
|
|
285
|
+
{
|
|
286
|
+
"onedal": self.__class__._onedal_predict,
|
|
287
|
+
"sklearn": sklearn_KMeans.predict,
|
|
288
|
+
},
|
|
289
|
+
X,
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
def _onedal_predict(self, X, queue=None):
|
|
293
|
+
X = self._validate_data(
|
|
294
|
+
X, accept_sparse=False, reset=False, dtype=[np.float64, np.float32]
|
|
295
|
+
)
|
|
296
|
+
if not hasattr(self, "_onedal_estimator"):
|
|
297
|
+
self._initialize_onedal_estimator()
|
|
298
|
+
self._onedal_estimator.cluster_centers_ = self.cluster_centers_
|
|
299
|
+
|
|
300
|
+
return self._onedal_estimator.predict(X, queue=queue)
|
|
301
|
+
|
|
302
|
+
def _onedal_supported(self, method_name, *data):
|
|
303
|
+
if method_name == "fit":
|
|
304
|
+
return self._onedal_fit_supported(method_name, *data)
|
|
305
|
+
if method_name == "predict":
|
|
306
|
+
return self._onedal_predict_supported(method_name, *data)
|
|
307
|
+
raise RuntimeError(
|
|
308
|
+
f"Unknown method {method_name} in {self.__class__.__name__}"
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
def _onedal_gpu_supported(self, method_name, *data):
|
|
312
|
+
return self._onedal_supported(method_name, *data)
|
|
313
|
+
|
|
314
|
+
def _onedal_cpu_supported(self, method_name, *data):
|
|
315
|
+
return self._onedal_supported(method_name, *data)
|
|
316
|
+
|
|
317
|
+
@wrap_output_data
|
|
318
|
+
def fit_transform(self, X, y=None, sample_weight=None):
|
|
319
|
+
"""Compute clustering and transform X to cluster-distance space.
|
|
320
|
+
|
|
321
|
+
Equivalent to fit(X).transform(X), but more efficiently implemented.
|
|
322
|
+
|
|
323
|
+
Parameters
|
|
324
|
+
----------
|
|
325
|
+
X : {array-like, sparse matrix} of shape (n_samples, n_features)
|
|
326
|
+
New data to transform.
|
|
327
|
+
|
|
328
|
+
y : Ignored
|
|
329
|
+
Not used, present here for API consistency by convention.
|
|
330
|
+
|
|
331
|
+
sample_weight : array-like of shape (n_samples,), default=None
|
|
332
|
+
The weights for each observation in X. If None, all observations
|
|
333
|
+
are assigned equal weight.
|
|
334
|
+
|
|
335
|
+
Returns
|
|
336
|
+
-------
|
|
337
|
+
X_new : ndarray of shape (n_samples, n_clusters)
|
|
338
|
+
X transformed in the new space.
|
|
339
|
+
"""
|
|
340
|
+
return self.fit(X, sample_weight=sample_weight)._transform(X)
|
|
341
|
+
|
|
342
|
+
@wrap_output_data
|
|
343
|
+
def transform(self, X):
|
|
344
|
+
"""Transform X to a cluster-distance space.
|
|
345
|
+
|
|
346
|
+
In the new space, each dimension is the distance to the cluster
|
|
347
|
+
centers. Note that even if X is sparse, the array returned by
|
|
348
|
+
`transform` will typically be dense.
|
|
349
|
+
|
|
350
|
+
Parameters
|
|
351
|
+
----------
|
|
352
|
+
X : {array-like, sparse matrix} of shape (n_samples, n_features)
|
|
353
|
+
New data to transform.
|
|
354
|
+
|
|
355
|
+
Returns
|
|
356
|
+
-------
|
|
357
|
+
X_new : ndarray of shape (n_samples, n_clusters)
|
|
358
|
+
X transformed in the new space.
|
|
359
|
+
"""
|
|
360
|
+
check_is_fitted(self)
|
|
361
|
+
|
|
362
|
+
X = self._check_test_data(X)
|
|
363
|
+
return self._transform(X)
|
|
364
|
+
|
|
365
|
+
else:
|
|
366
|
+
from daal4py.sklearn.cluster import KMeans
|
|
367
|
+
|
|
368
|
+
logging.warning(
|
|
369
|
+
"Preview KMeans requires oneDAL version >= 2023.2 " "but it was not found"
|
|
370
|
+
)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# ===============================================================================
|
|
3
|
+
# Copyright 2023 Intel Corporation
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
# ===============================================================================
|
|
17
|
+
|
|
18
|
+
from .pca import PCA
|
|
19
|
+
|
|
20
|
+
__all__ = ["PCA"]
|