|
| 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 daal4py.sklearn._utils import get_dtype |
| 18 | + |
| 19 | +from ...datatypes import _convert_to_supported, from_table, to_table |
| 20 | +from ...decomposition import IncrementalPCA as base_IncrementalPCA |
| 21 | +from ...utils import _check_array |
| 22 | +from .._base import BaseEstimatorSPMD |
| 23 | + |
| 24 | + |
| 25 | +class IncrementalPCA(BaseEstimatorSPMD, base_IncrementalPCA): |
| 26 | + """ |
| 27 | + Distributed incremental estimator for PCA based on oneDAL implementation. |
| 28 | + Allows for distributed PCA computation if data is split into batches. |
| 29 | +
|
| 30 | + API is the same as for `onedal.decomposition.IncrementalPCA` |
| 31 | + """ |
| 32 | + |
| 33 | + def _reset(self): |
| 34 | + self._partial_result = super(base_IncrementalPCA, self)._get_backend( |
| 35 | + "decomposition", "dim_reduction", "partial_train_result" |
| 36 | + ) |
| 37 | + if hasattr(self, "components_"): |
| 38 | + del self.components_ |
| 39 | + |
| 40 | + def partial_fit(self, X, y=None, queue=None): |
| 41 | + """Incremental fit with X. All of X is processed as a single batch. |
| 42 | +
|
| 43 | + Parameters |
| 44 | + ---------- |
| 45 | + X : array-like of shape (n_samples, n_features) |
| 46 | + Training data, where `n_samples` is the number of samples and |
| 47 | + `n_features` is the number of features. |
| 48 | +
|
| 49 | + y : Ignored |
| 50 | + Not used, present for API consistency by convention. |
| 51 | +
|
| 52 | + Returns |
| 53 | + ------- |
| 54 | + self : object |
| 55 | + Returns the instance itself. |
| 56 | + """ |
| 57 | + X = _check_array(X) |
| 58 | + n_samples, n_features = X.shape |
| 59 | + |
| 60 | + first_pass = not hasattr(self, "components_") |
| 61 | + if first_pass: |
| 62 | + self.components_ = None |
| 63 | + self.n_samples_seen_ = n_samples |
| 64 | + self.n_features_in_ = n_features |
| 65 | + else: |
| 66 | + self.n_samples_seen_ += n_samples |
| 67 | + |
| 68 | + if self.n_components is None: |
| 69 | + if self.components_ is None: |
| 70 | + self.n_components_ = min(n_samples, n_features) |
| 71 | + else: |
| 72 | + self.n_components_ = self.components_.shape[0] |
| 73 | + else: |
| 74 | + self.n_components_ = self.n_components |
| 75 | + |
| 76 | + self._queue = queue |
| 77 | + |
| 78 | + policy = super(base_IncrementalPCA, self)._get_policy(queue, X) |
| 79 | + X = _convert_to_supported(policy, X) |
| 80 | + |
| 81 | + if not hasattr(self, "_dtype"): |
| 82 | + self._dtype = get_dtype(X) |
| 83 | + self._params = self._get_onedal_params(X) |
| 84 | + |
| 85 | + X_table = to_table(X) |
| 86 | + self._partial_result = super(base_IncrementalPCA, self)._get_backend( |
| 87 | + "decomposition", |
| 88 | + "dim_reduction", |
| 89 | + "partial_train", |
| 90 | + policy, |
| 91 | + self._params, |
| 92 | + self._partial_result, |
| 93 | + X_table, |
| 94 | + ) |
| 95 | + return self |
| 96 | + |
| 97 | + def _create_model(self): |
| 98 | + m = super(base_IncrementalPCA, self)._get_backend( |
| 99 | + "decomposition", "dim_reduction", "model" |
| 100 | + ) |
| 101 | + m.eigenvectors = to_table(self.components_) |
| 102 | + m.means = to_table(self.mean_) |
| 103 | + if self.whiten: |
| 104 | + m.eigenvalues = to_table(self.explained_variance_) |
| 105 | + self._onedal_model = m |
| 106 | + return m |
| 107 | + |
| 108 | + def predict(self, X, queue=None): |
| 109 | + policy = super(base_IncrementalPCA, self)._get_policy(queue, X) |
| 110 | + model = self._create_model() |
| 111 | + X = _convert_to_supported(policy, X) |
| 112 | + params = self._get_onedal_params(X, stage="predict") |
| 113 | + |
| 114 | + result = super(base_IncrementalPCA, self)._get_backend( |
| 115 | + "decomposition", "dim_reduction", "infer", policy, params, model, to_table(X) |
| 116 | + ) |
| 117 | + return from_table(result.transformed_data) |
0 commit comments