|
| 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 | + |
| 19 | +from daal4py.sklearn._utils import get_dtype |
| 20 | + |
| 21 | +from ...covariance import ( |
| 22 | + IncrementalEmpiricalCovariance as base_IncrementalEmpiricalCovariance, |
| 23 | +) |
| 24 | +from ...datatypes import _convert_to_supported, to_table |
| 25 | +from ...utils import _check_array |
| 26 | +from .._base import BaseEstimatorSPMD |
| 27 | + |
| 28 | + |
| 29 | +class IncrementalEmpiricalCovariance( |
| 30 | + BaseEstimatorSPMD, base_IncrementalEmpiricalCovariance |
| 31 | +): |
| 32 | + def _reset(self): |
| 33 | + self._partial_result = super( |
| 34 | + base_IncrementalEmpiricalCovariance, self |
| 35 | + )._get_backend("covariance", None, "partial_compute_result") |
| 36 | + |
| 37 | + def partial_fit(self, X, y=None, queue=None): |
| 38 | + """ |
| 39 | + Computes partial data for the covariance matrix |
| 40 | + from data batch X and saves it to `_partial_result`. |
| 41 | +
|
| 42 | + Parameters |
| 43 | + ---------- |
| 44 | + X : array-like of shape (n_samples, n_features) |
| 45 | + Training data batch, where `n_samples` is the number of samples |
| 46 | + in the batch, and `n_features` is the number of features. |
| 47 | +
|
| 48 | + y : Ignored |
| 49 | + Not used, present for API consistency by convention. |
| 50 | +
|
| 51 | + queue : dpctl.SyclQueue |
| 52 | + If not None, use this queue for computations. |
| 53 | +
|
| 54 | + Returns |
| 55 | + ------- |
| 56 | + self : object |
| 57 | + Returns the instance itself. |
| 58 | + """ |
| 59 | + X = _check_array(X, dtype=[np.float64, np.float32], ensure_2d=True) |
| 60 | + |
| 61 | + self._queue = queue |
| 62 | + |
| 63 | + policy = super(base_IncrementalEmpiricalCovariance, self)._get_policy(queue, X) |
| 64 | + |
| 65 | + X = _convert_to_supported(policy, X) |
| 66 | + |
| 67 | + if not hasattr(self, "_dtype"): |
| 68 | + self._dtype = get_dtype(X) |
| 69 | + |
| 70 | + params = self._get_onedal_params(self._dtype) |
| 71 | + table_X = to_table(X) |
| 72 | + self._partial_result = super( |
| 73 | + base_IncrementalEmpiricalCovariance, self |
| 74 | + )._get_backend( |
| 75 | + "covariance", |
| 76 | + None, |
| 77 | + "partial_compute", |
| 78 | + policy, |
| 79 | + params, |
| 80 | + self._partial_result, |
| 81 | + table_X, |
| 82 | + ) |
0 commit comments