Skip to content

Commit

Permalink
[enhancement] Add IncrementalEmpiricalCovariance to patch_map (#1674)
Browse files Browse the repository at this point in the history
* initial hack

* formatting

* formatting

* isorted

* remove unnecessary lines

* add another patch option

* first corrections

* Update covariance.py

* Update covariance.py

* Update dispatcher.py

* Update incremental_covariance.py

* Update covariance.py

* Update covariance.py

* Update covariance.py

* Update incremental_covariance.py

* Update test_patching.py

* expand tests temporarily

* reorient n_features_in_

* updates

* added formatting

* remove TODO

* Update covariance.py

* Update incremental_covariance.py

* Update covariance.py

* Update covariance.py

* Update covariance.py

* Update incremental_covariance.py

* Update covariance.py

* Update incremental_covariance.py

* Update test_incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update test_incremental_covariance.py

* Update test_incremental_covariance.py

* Update incremental_covariance.py

* Update test_n_jobs_support.py

* Update dispatcher.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update test_incremental_covariance.py

* Update covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update test_incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* formatting

* fix test_monkeypatch.py

* switch order

* formatting

* generalize sklearn test patching in sklearnex

* add wraps

* add proper import

* check_array -> _check_array

* switch underscore

* change text

* add _check_array

* linting

* add warning

* remove other PRs from this PR

* forgotten None

* final changes

* and to or

* add tests on recommendation

* readd column_count

* readd to patchmap

* Update test_incremental_covariance.py

* Update test_memory_usage.py

* formatting

* Update sklearnex/tests/test_memory_usage.py

Co-authored-by: Alexander Andreev <alexander.andreev@intel.com>

* change order to prevent fp64 issues

* add assume_centered to daal calls

* mistake

* fix linting, change tests

* allow for method=None

* pre-protect for dpnp/dpctl

* change daal_check_versions

* change daal_check_versions

* switch to zeros_like

* formatting

* fix c++ mistake

* add testing for assume_centered

* Update incremental_covariance.py

* Update dispatcher.py

* Update test_patching.py

* add daal_check_version

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update covariance.py

* Update test_incremental_covariance.py

* Update incremental_covariance.py

* Update test_patching.py

* Update test_incremental_covariance.py

* Update test_incremental_covariance.py

* Update incremental_covariance.py

* Update deselected_tests.yaml

* formatting

* fix bad merge

* fix test failure

* Update dispatcher.py

* Update test_incremental_covariance.py

* Update incremental_covariance.py

* This may timeout CI

* Update incremental_covariance.py

* Update incremental_covariance.py

* Update test_incremental_covariance.py

* Update test_incremental_covariance.py

* formatting

* Update test_incremental_covariance.py

* Update test_incremental_covariance.py

* Update deselected_tests.yaml

---------

Co-authored-by: Alexander Andreev <alexander.andreev@intel.com>
  • Loading branch information
icfaust and Alexsandruss authored Apr 26, 2024
1 parent 57c3c73 commit 8c7a928
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 55 deletions.
3 changes: 2 additions & 1 deletion deselected_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,13 @@ deselected_tests:
# Need to rework getting policy to correctly obtain it for method without data (finalize_fit)
# and avoid keeping it in class attribute, also need to investigate how to implement
# partial result serialization
- tests/test_common.py::test_estimators[IncrementalEmpiricalCovariance()-check_estimators_pickle]
- tests/test_common.py::test_estimators[IncrementalEmpiricalCovariance()-check_estimators_pickle(readonly_memmap=True)]
- tests/test_common.py::test_estimators[IncrementalLinearRegression()-check_estimators_pickle]
- tests/test_common.py::test_estimators[IncrementalLinearRegression()-check_estimators_pickle(readonly_memmap=True)]
# There are not enough data to run onedal backend
- tests/test_common.py::test_estimators[IncrementalLinearRegression()-check_fit2d_1sample]


# --------------------------------------------------------
# No need to test daal4py patching
reduced_tests:
Expand Down
25 changes: 18 additions & 7 deletions onedal/covariance/incremental_covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from onedal import _backend

from ..datatypes import _convert_to_supported, from_table, to_table
from ..utils import _check_array
from .covariance import BaseEmpiricalCovariance


Expand All @@ -37,6 +38,12 @@ class IncrementalEmpiricalCovariance(BaseEmpiricalCovariance):
If True biased estimation of covariance is computed which equals to
the unbiased one multiplied by (n_samples - 1) / n_samples.
assume_centered : bool, default=False
If True, data are not centered before computation.
Useful when working with data whose mean is almost, but not exactly
zero.
If False (default), data are centered before computation.
Attributes
----------
location_ : ndarray of shape (n_features,)
Expand All @@ -46,8 +53,11 @@ class IncrementalEmpiricalCovariance(BaseEmpiricalCovariance):
Estimated covariance matrix
"""

def __init__(self, method="dense", bias=False):
super().__init__(method, bias)
def __init__(self, method="dense", bias=False, assume_centered=False):
super().__init__(method, bias, assume_centered)
self._reset()

def _reset(self):
self._partial_result = self._get_backend(
"covariance", None, "partial_compute_result"
)
Expand All @@ -74,15 +84,16 @@ def partial_fit(self, X, y=None, queue=None):
self : object
Returns the instance itself.
"""
X = _check_array(X, dtype=[np.float64, np.float32], ensure_2d=True)

if not hasattr(self, "_policy"):
self._policy = self._get_policy(queue, X)

X = _convert_to_supported(self._policy, X)

if not hasattr(self, "_dtype"):
self._dtype = get_dtype(X)
X = make2d(X)
types = [np.float32, np.float64]
if get_dtype(X) not in types:
X = X.astype(np.float64)
X = _convert_to_supported(self._policy, X)

params = self._get_onedal_params(self._dtype)
table_X = to_table(X)
self._partial_result = self._get_backend(
Expand Down
Loading

0 comments on commit 8c7a928

Please sign in to comment.