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.

Files changed (90) hide show
  1. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/__init__.py +61 -0
  2. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/__main__.py +59 -0
  3. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/_config.py +110 -0
  4. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/_device_offload.py +223 -0
  5. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/_utils.py +95 -0
  6. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/basic_statistics/__init__.py +20 -0
  7. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/basic_statistics/basic_statistics.py +17 -0
  8. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/__init__.py +21 -0
  9. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/dbscan.py +187 -0
  10. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/k_means.py +18 -0
  11. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/tests/test_dbscan.py +37 -0
  12. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/cluster/tests/test_kmeans.py +31 -0
  13. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/decomposition/__init__.py +20 -0
  14. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/decomposition/pca.py +18 -0
  15. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/decomposition/tests/test_pca.py +28 -0
  16. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/dispatcher.py +329 -0
  17. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/doc/third-party-programs.txt +424 -0
  18. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/ensemble/__init__.py +30 -0
  19. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/ensemble/_forest.py +1947 -0
  20. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/ensemble/tests/test_forest.py +118 -0
  21. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/glob/__main__.py +73 -0
  22. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/glob/dispatcher.py +88 -0
  23. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/__init__.py +30 -0
  24. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/coordinate_descent.py +18 -0
  25. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/linear.py +373 -0
  26. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/logistic_path.py +18 -0
  27. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/ridge.py +18 -0
  28. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/tests/test_linear.py +77 -0
  29. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/linear_model/tests/test_logreg.py +29 -0
  30. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/manifold/__init__.py +20 -0
  31. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/manifold/t_sne.py +18 -0
  32. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/manifold/tests/test_tsne.py +27 -0
  33. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/__init__.py +24 -0
  34. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/pairwise.py +18 -0
  35. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/ranking.py +18 -0
  36. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/metrics/tests/test_metrics.py +40 -0
  37. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/model_selection/__init__.py +22 -0
  38. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/model_selection/split.py +18 -0
  39. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/model_selection/tests/test_model_selection.py +35 -0
  40. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/__init__.py +28 -0
  41. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/common.py +264 -0
  42. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/knn_classification.py +331 -0
  43. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/knn_regression.py +307 -0
  44. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/knn_unsupervised.py +220 -0
  45. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/lof.py +437 -0
  46. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/neighbors/tests/test_neighbors.py +85 -0
  47. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/__init__.py +18 -0
  48. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/__init__.py +20 -0
  49. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/_common.py +84 -0
  50. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/cluster/k_means.py +370 -0
  51. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/__init__.py +20 -0
  52. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/pca.py +376 -0
  53. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/preview/decomposition/tests/test_preview_pca.py +38 -0
  54. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/__init__.py +24 -0
  55. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/basic_statistics/__init__.py +19 -0
  56. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/basic_statistics/basic_statistics.py +21 -0
  57. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/cluster/__init__.py +30 -0
  58. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/cluster/dbscan.py +50 -0
  59. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/cluster/kmeans.py +21 -0
  60. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/decomposition/__init__.py +19 -0
  61. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/decomposition/pca.py +21 -0
  62. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/ensemble/__init__.py +19 -0
  63. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/ensemble/forest.py +79 -0
  64. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/linear_model/__init__.py +19 -0
  65. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/linear_model/linear_model.py +21 -0
  66. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/neighbors/__init__.py +19 -0
  67. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/spmd/neighbors/neighbors.py +25 -0
  68. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/__init__.py +30 -0
  69. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/_common.py +188 -0
  70. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/nusvc.py +272 -0
  71. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/nusvr.py +163 -0
  72. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/svc.py +301 -0
  73. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/svr.py +164 -0
  74. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/svm/tests/test_svm.py +102 -0
  75. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/_models_info.py +170 -0
  76. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_config.py +39 -0
  77. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_memory_usage.py +225 -0
  78. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_monkeypatch.py +210 -0
  79. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_parallel.py +50 -0
  80. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_patching.py +122 -0
  81. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/test_run_to_run_stability_tests.py +428 -0
  82. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/tests/utils/_launch_algorithms.py +118 -0
  83. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/utils/__init__.py +19 -0
  84. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/utils/parallel.py +59 -0
  85. scikit_learn_intelex-2024.0.1.data/data/Lib/site-packages/sklearnex/utils/validation.py +18 -0
  86. scikit_learn_intelex-2024.0.1.dist-info/LICENSE.txt +202 -0
  87. scikit_learn_intelex-2024.0.1.dist-info/METADATA +230 -0
  88. scikit_learn_intelex-2024.0.1.dist-info/RECORD +90 -0
  89. scikit_learn_intelex-2024.0.1.dist-info/WHEEL +5 -0
  90. scikit_learn_intelex-2024.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,79 @@
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
+ from onedal.spmd.ensemble import RandomForestClassifier as onedal_RandomForestClassifier
20
+ from onedal.spmd.ensemble import RandomForestRegressor as onedal_RandomForestRegressor
21
+
22
+ from ...ensemble import RandomForestClassifier as RandomForestClassifier_Batch
23
+ from ...ensemble import RandomForestRegressor as RandomForestRegressor_Batch
24
+
25
+
26
+ class BaseForestSPMD(ABC):
27
+ def _onedal_classifier(self, **onedal_params):
28
+ return onedal_RandomForestClassifier(**onedal_params)
29
+
30
+ def _onedal_regressor(self, **onedal_params):
31
+ return onedal_RandomForestRegressor(**onedal_params)
32
+
33
+
34
+ class RandomForestClassifier(BaseForestSPMD, RandomForestClassifier_Batch):
35
+ __doc__ = RandomForestClassifier_Batch.__doc__
36
+
37
+ def _onedal_cpu_supported(self, method_name, *data):
38
+ # TODO:
39
+ # check which methods supported SPMD interface on CPU.
40
+ ready = super()._onedal_cpu_supported(method_name, *data)
41
+ if not ready:
42
+ raise RuntimeError(
43
+ f"Method {method_name} in {self.__class__.__name__} "
44
+ "is not supported with given inputs."
45
+ )
46
+ return ready
47
+
48
+ def _onedal_gpu_supported(self, method_name, *data):
49
+ ready = super()._onedal_gpu_supported(method_name, *data)
50
+ if not ready:
51
+ raise RuntimeError(
52
+ f"Method {method_name} in {self.__class__.__name__} "
53
+ "is not supported with given inputs."
54
+ )
55
+ return ready
56
+
57
+
58
+ class RandomForestRegressor(BaseForestSPMD, RandomForestRegressor_Batch):
59
+ __doc__ = RandomForestRegressor_Batch.__doc__
60
+
61
+ def _onedal_cpu_supported(self, method_name, *data):
62
+ # TODO:
63
+ # check which methods supported SPMD interface on CPU.
64
+ ready = super()._onedal_cpu_supported(method_name, *data)
65
+ if not ready:
66
+ raise RuntimeError(
67
+ f"Method {method_name} in {self.__class__.__name__} "
68
+ "is not supported with given inputs."
69
+ )
70
+ return ready
71
+
72
+ def _onedal_gpu_supported(self, method_name, *data):
73
+ ready = super()._onedal_gpu_supported(method_name, *data)
74
+ if not ready:
75
+ raise RuntimeError(
76
+ f"Method {method_name} in {self.__class__.__name__} "
77
+ "is not supported with given inputs."
78
+ )
79
+ return ready
@@ -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 .linear_model import LinearRegression
18
+
19
+ __all__ = ["LinearRegression"]
@@ -0,0 +1,21 @@
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 onedal.spmd.linear_model import LinearRegression
18
+
19
+ # TODO:
20
+ # Currently it uses `onedal` module interface.
21
+ # Add sklearnex dispatching.
@@ -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 .neighbors import KNeighborsClassifier, KNeighborsRegressor, NearestNeighbors
18
+
19
+ __all__ = ["KNeighborsClassifier", "KNeighborsRegressor", "NearestNeighbors"]
@@ -0,0 +1,25 @@
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 onedal.spmd.neighbors import (
18
+ KNeighborsClassifier,
19
+ KNeighborsRegressor,
20
+ NearestNeighbors,
21
+ )
22
+
23
+ # TODO:
24
+ # Currently it uses `onedal` module interface.
25
+ # Add sklearnex dispatching.
@@ -0,0 +1,30 @@
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
+ from .._utils import get_sklearnex_version
19
+
20
+ if get_sklearnex_version((2021, "P", 300)):
21
+ from .nusvc import NuSVC
22
+ from .nusvr import NuSVR
23
+ from .svc import SVC
24
+ from .svr import SVR
25
+
26
+ __all__ = ["SVR", "SVC", "NuSVC", "NuSVR"]
27
+ else:
28
+ from daal4py.sklearn.svm import SVC
29
+
30
+ __all__ = ["SVC"]
@@ -0,0 +1,188 @@
1
+ # ==============================================================================
2
+ # Copyright 2021 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
+ import numpy as np
20
+ from sklearn.calibration import CalibratedClassifierCV
21
+ from sklearn.model_selection import StratifiedKFold
22
+ from sklearn.preprocessing import LabelEncoder
23
+
24
+ from daal4py.sklearn._utils import sklearn_check_version
25
+ from onedal.utils import _column_or_1d
26
+
27
+ from .._utils import PatchingConditionsChain
28
+
29
+
30
+ def get_dual_coef(self):
31
+ return self.dual_coef_
32
+
33
+
34
+ def set_dual_coef(self, value):
35
+ self.dual_coef_ = value
36
+ if hasattr(self, "_onedal_estimator"):
37
+ self._onedal_estimator.dual_coef_ = value
38
+ if not self._is_in_fit:
39
+ del self._onedal_estimator._onedal_model
40
+
41
+
42
+ def get_intercept(self):
43
+ return self._intercept_
44
+
45
+
46
+ def set_intercept(self, value):
47
+ self._intercept_ = value
48
+ if hasattr(self, "_onedal_estimator"):
49
+ self._onedal_estimator.intercept_ = value
50
+ if not self._is_in_fit:
51
+ del self._onedal_estimator._onedal_model
52
+
53
+
54
+ class BaseSVM(ABC):
55
+ def _onedal_gpu_supported(self, method_name, *data):
56
+ patching_status = PatchingConditionsChain(f"sklearn.{method_name}")
57
+ patching_status.and_conditions([(False, "GPU offloading is not supported.")])
58
+ return patching_status
59
+
60
+ def _onedal_cpu_supported(self, method_name, *data):
61
+ class_name = self.__class__.__name__
62
+ patching_status = PatchingConditionsChain(
63
+ f"sklearn.svm.{class_name}.{method_name}"
64
+ )
65
+ if method_name == "fit":
66
+ patching_status.and_conditions(
67
+ [
68
+ (
69
+ self.kernel in ["linear", "rbf", "poly", "sigmoid"],
70
+ f'Kernel is "{self.kernel}" while '
71
+ '"linear", "rbf", "poly" and "sigmoid" are only supported.',
72
+ )
73
+ ]
74
+ )
75
+ return patching_status
76
+ inference_methods = (
77
+ ["predict"]
78
+ if class_name.endswith("R")
79
+ else ["predict", "predict_proba", "decision_function"]
80
+ )
81
+ if method_name in inference_methods:
82
+ patching_status.and_conditions(
83
+ [(hasattr(self, "_onedal_estimator"), "oneDAL model was not trained.")]
84
+ )
85
+ return patching_status
86
+ raise RuntimeError(f"Unknown method {method_name} in {class_name}")
87
+
88
+
89
+ class BaseSVC(BaseSVM):
90
+ def _compute_balanced_class_weight(self, y):
91
+ y_ = _column_or_1d(y)
92
+ classes, _ = np.unique(y_, return_inverse=True)
93
+
94
+ le = LabelEncoder()
95
+ y_ind = le.fit_transform(y_)
96
+ if not all(np.in1d(classes, le.classes_)):
97
+ raise ValueError("classes should have valid labels that are in y")
98
+
99
+ recip_freq = len(y_) / (len(le.classes_) * np.bincount(y_ind).astype(np.float64))
100
+ return recip_freq[le.transform(classes)]
101
+
102
+ def _fit_proba(self, X, y, sample_weight=None, queue=None):
103
+ params = self.get_params()
104
+ params["probability"] = False
105
+ params["decision_function_shape"] = "ovr"
106
+ clf_base = self.__class__(**params)
107
+
108
+ try:
109
+ n_splits = 5
110
+ n_jobs = n_splits if queue is None or queue.sycl_device.is_cpu else 1
111
+ cv = StratifiedKFold(
112
+ n_splits=n_splits, shuffle=True, random_state=self.random_state
113
+ )
114
+ if sklearn_check_version("0.24"):
115
+ self.clf_prob = CalibratedClassifierCV(
116
+ clf_base, ensemble=False, cv=cv, method="sigmoid", n_jobs=n_jobs
117
+ )
118
+ else:
119
+ self.clf_prob = CalibratedClassifierCV(clf_base, cv=cv, method="sigmoid")
120
+ self.clf_prob.fit(X, y, sample_weight)
121
+ except ValueError:
122
+ clf_base = clf_base.fit(X, y, sample_weight)
123
+ self.clf_prob = CalibratedClassifierCV(
124
+ clf_base, cv="prefit", method="sigmoid"
125
+ )
126
+ self.clf_prob.fit(X, y, sample_weight)
127
+
128
+ def _save_attributes(self):
129
+ self.support_vectors_ = self._onedal_estimator.support_vectors_
130
+ self.n_features_in_ = self._onedal_estimator.n_features_in_
131
+ self.fit_status_ = 0
132
+ self.dual_coef_ = self._onedal_estimator.dual_coef_
133
+ self.shape_fit_ = self._onedal_estimator.class_weight_
134
+ self.classes_ = self._onedal_estimator.classes_
135
+ self.class_weight_ = self._onedal_estimator.class_weight_
136
+ self.support_ = self._onedal_estimator.support_
137
+
138
+ self._intercept_ = self._onedal_estimator.intercept_
139
+ self._n_support = self._onedal_estimator._n_support
140
+ self._sparse = False
141
+ self._gamma = self._onedal_estimator._gamma
142
+ if self.probability:
143
+ length = int(len(self.classes_) * (len(self.classes_) - 1) / 2)
144
+ self._probA = np.zeros(length)
145
+ self._probB = np.zeros(length)
146
+ else:
147
+ self._probA = np.empty(0)
148
+ self._probB = np.empty(0)
149
+
150
+ self._dual_coef_ = property(get_dual_coef, set_dual_coef)
151
+ self.intercept_ = property(get_intercept, set_intercept)
152
+
153
+ self._is_in_fit = True
154
+ self._dual_coef_ = self.dual_coef_
155
+ self.intercept_ = self._intercept_
156
+ self._is_in_fit = False
157
+
158
+ if sklearn_check_version("1.1"):
159
+ length = int(len(self.classes_) * (len(self.classes_) - 1) / 2)
160
+ self.n_iter_ = np.full((length,), self._onedal_estimator.n_iter_)
161
+
162
+
163
+ class BaseSVR(BaseSVM):
164
+ def _save_attributes(self):
165
+ self.support_vectors_ = self._onedal_estimator.support_vectors_
166
+ self.n_features_in_ = self._onedal_estimator.n_features_in_
167
+ self.fit_status_ = 0
168
+ self.dual_coef_ = self._onedal_estimator.dual_coef_
169
+ self.shape_fit_ = self._onedal_estimator.shape_fit_
170
+ self.support_ = self._onedal_estimator.support_
171
+
172
+ self._intercept_ = self._onedal_estimator.intercept_
173
+ self._n_support = [self.support_vectors_.shape[0]]
174
+ self._sparse = False
175
+ self._gamma = self._onedal_estimator._gamma
176
+ self._probA = None
177
+ self._probB = None
178
+
179
+ self._dual_coef_ = property(get_dual_coef, set_dual_coef)
180
+ self.intercept_ = property(get_intercept, set_intercept)
181
+
182
+ self._is_in_fit = True
183
+ self._dual_coef_ = self.dual_coef_
184
+ self.intercept_ = self._intercept_
185
+ self._is_in_fit = False
186
+
187
+ if sklearn_check_version("1.1"):
188
+ self.n_iter_ = self._onedal_estimator.n_iter_
@@ -0,0 +1,272 @@
1
+ # ==============================================================================
2
+ # Copyright 2021 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 sklearn.exceptions import NotFittedError
18
+ from sklearn.svm import NuSVC as sklearn_NuSVC
19
+ from sklearn.utils.validation import _deprecate_positional_args
20
+
21
+ from daal4py.sklearn._utils import sklearn_check_version
22
+
23
+ from .._device_offload import dispatch, wrap_output_data
24
+ from ._common import BaseSVC
25
+
26
+ if sklearn_check_version("1.0"):
27
+ from sklearn.utils.metaestimators import available_if
28
+
29
+ from onedal.svm import NuSVC as onedal_NuSVC
30
+
31
+
32
+ class NuSVC(sklearn_NuSVC, BaseSVC):
33
+ __doc__ = sklearn_NuSVC.__doc__
34
+
35
+ if sklearn_check_version("1.2"):
36
+ _parameter_constraints: dict = {**sklearn_NuSVC._parameter_constraints}
37
+
38
+ @_deprecate_positional_args
39
+ def __init__(
40
+ self,
41
+ *,
42
+ nu=0.5,
43
+ kernel="rbf",
44
+ degree=3,
45
+ gamma="scale",
46
+ coef0=0.0,
47
+ shrinking=True,
48
+ probability=False,
49
+ tol=1e-3,
50
+ cache_size=200,
51
+ class_weight=None,
52
+ verbose=False,
53
+ max_iter=-1,
54
+ decision_function_shape="ovr",
55
+ break_ties=False,
56
+ random_state=None,
57
+ ):
58
+ super().__init__(
59
+ nu=nu,
60
+ kernel=kernel,
61
+ degree=degree,
62
+ gamma=gamma,
63
+ coef0=coef0,
64
+ shrinking=shrinking,
65
+ probability=probability,
66
+ tol=tol,
67
+ cache_size=cache_size,
68
+ class_weight=class_weight,
69
+ verbose=verbose,
70
+ max_iter=max_iter,
71
+ decision_function_shape=decision_function_shape,
72
+ break_ties=break_ties,
73
+ random_state=random_state,
74
+ )
75
+
76
+ def fit(self, X, y, sample_weight=None):
77
+ """
78
+ Fit the SVM model according to the given training data.
79
+
80
+ Parameters
81
+ ----------
82
+ X : {array-like, sparse matrix} of shape (n_samples, n_features) \
83
+ or (n_samples, n_samples)
84
+ Training vectors, where `n_samples` is the number of samples
85
+ and `n_features` is the number of features.
86
+ For kernel="precomputed", the expected shape of X is
87
+ (n_samples, n_samples).
88
+
89
+ y : array-like of shape (n_samples,)
90
+ Target values (class labels in classification, real numbers in
91
+ regression).
92
+
93
+ sample_weight : array-like of shape (n_samples,), default=None
94
+ Per-sample weights. Rescale C per sample. Higher weights
95
+ force the classifier to put more emphasis on these points.
96
+
97
+ Returns
98
+ -------
99
+ self : object
100
+ Fitted estimator.
101
+
102
+ Notes
103
+ -----
104
+ If X and y are not C-ordered and contiguous arrays of np.float64 and
105
+ X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
106
+
107
+ If X is a dense array, then the other methods will not support sparse
108
+ matrices as input.
109
+ """
110
+ if sklearn_check_version("1.2"):
111
+ self._validate_params()
112
+ if sklearn_check_version("1.0"):
113
+ self._check_feature_names(X, reset=True)
114
+ dispatch(
115
+ self,
116
+ "fit",
117
+ {
118
+ "onedal": self.__class__._onedal_fit,
119
+ "sklearn": sklearn_NuSVC.fit,
120
+ },
121
+ X,
122
+ y,
123
+ sample_weight,
124
+ )
125
+
126
+ return self
127
+
128
+ @wrap_output_data
129
+ def predict(self, X):
130
+ """
131
+ Perform regression on samples in X.
132
+
133
+ For an one-class model, +1 (inlier) or -1 (outlier) is returned.
134
+
135
+ Parameters
136
+ ----------
137
+ X : {array-like, sparse matrix} of shape (n_samples, n_features)
138
+ For kernel="precomputed", the expected shape of X is
139
+ (n_samples_test, n_samples_train).
140
+
141
+ Returns
142
+ -------
143
+ y_pred : ndarray of shape (n_samples,)
144
+ The predicted values.
145
+ """
146
+ if sklearn_check_version("1.0"):
147
+ self._check_feature_names(X, reset=False)
148
+ return dispatch(
149
+ self,
150
+ "predict",
151
+ {
152
+ "onedal": self.__class__._onedal_predict,
153
+ "sklearn": sklearn_NuSVC.predict,
154
+ },
155
+ X,
156
+ )
157
+
158
+ if sklearn_check_version("1.0"):
159
+
160
+ @available_if(sklearn_NuSVC._check_proba)
161
+ def predict_proba(self, X):
162
+ """
163
+ Compute probabilities of possible outcomes for samples in X.
164
+
165
+ The model need to have probability information computed at training
166
+ time: fit with attribute `probability` set to True.
167
+
168
+ Parameters
169
+ ----------
170
+ X : array-like of shape (n_samples, n_features)
171
+ For kernel="precomputed", the expected shape of X is
172
+ (n_samples_test, n_samples_train).
173
+
174
+ Returns
175
+ -------
176
+ T : ndarray of shape (n_samples, n_classes)
177
+ Returns the probability of the sample for each class in
178
+ the model. The columns correspond to the classes in sorted
179
+ order, as they appear in the attribute :term:`classes_`.
180
+
181
+ Notes
182
+ -----
183
+ The probability model is created using cross validation, so
184
+ the results can be slightly different than those obtained by
185
+ predict. Also, it will produce meaningless results on very small
186
+ datasets.
187
+ """
188
+ return self._predict_proba(X)
189
+
190
+ else:
191
+
192
+ @property
193
+ def predict_proba(self):
194
+ self._check_proba()
195
+ return self._predict_proba
196
+
197
+ @wrap_output_data
198
+ def _predict_proba(self, X):
199
+ if sklearn_check_version("1.0"):
200
+ self._check_feature_names(X, reset=False)
201
+ sklearn_pred_proba = (
202
+ sklearn_NuSVC.predict_proba
203
+ if sklearn_check_version("1.0")
204
+ else sklearn_NuSVC._predict_proba
205
+ )
206
+
207
+ return dispatch(
208
+ self,
209
+ "predict_proba",
210
+ {
211
+ "onedal": self.__class__._onedal_predict_proba,
212
+ "sklearn": sklearn_pred_proba,
213
+ },
214
+ X,
215
+ )
216
+
217
+ @wrap_output_data
218
+ def decision_function(self, X):
219
+ if sklearn_check_version("1.0"):
220
+ self._check_feature_names(X, reset=False)
221
+ return dispatch(
222
+ self,
223
+ "decision_function",
224
+ {
225
+ "onedal": self.__class__._onedal_decision_function,
226
+ "sklearn": sklearn_NuSVC.decision_function,
227
+ },
228
+ X,
229
+ )
230
+
231
+ def _onedal_fit(self, X, y, sample_weight=None, queue=None):
232
+ onedal_params = {
233
+ "nu": self.nu,
234
+ "kernel": self.kernel,
235
+ "degree": self.degree,
236
+ "gamma": self.gamma,
237
+ "coef0": self.coef0,
238
+ "tol": self.tol,
239
+ "shrinking": self.shrinking,
240
+ "cache_size": self.cache_size,
241
+ "max_iter": self.max_iter,
242
+ "class_weight": self.class_weight,
243
+ "break_ties": self.break_ties,
244
+ "decision_function_shape": self.decision_function_shape,
245
+ }
246
+
247
+ self._onedal_estimator = onedal_NuSVC(**onedal_params)
248
+ self._onedal_estimator.fit(X, y, sample_weight, queue=queue)
249
+
250
+ if self.probability:
251
+ self._fit_proba(X, y, sample_weight, queue=queue)
252
+ self._save_attributes()
253
+
254
+ def _onedal_predict(self, X, queue=None):
255
+ return self._onedal_estimator.predict(X, queue=queue)
256
+
257
+ def _onedal_predict_proba(self, X, queue=None):
258
+ if getattr(self, "clf_prob", None) is None:
259
+ raise NotFittedError(
260
+ "predict_proba is not available when fitted with probability=False"
261
+ )
262
+ from .._config import config_context, get_config
263
+
264
+ # We use stock metaestimators below, so the only way
265
+ # to pass a queue is using config_context.
266
+ cfg = get_config()
267
+ cfg["target_offload"] = queue
268
+ with config_context(**cfg):
269
+ return self.clf_prob.predict_proba(X)
270
+
271
+ def _onedal_decision_function(self, X, queue=None):
272
+ return self._onedal_estimator.decision_function(X, queue=queue)