Skip to content

Conversation

KateBlueSky
Copy link

@KateBlueSky KateBlueSky commented Aug 6, 2025

Refactor KMeans estimator to follow oneDAL estimator design pattern

Depends on #2641

This PR refactors the KMeans estimator to align with the standardized design pattern used across oneDAL estimators, such as DummyEstimator outlined in #2534 . The main goal is to make the estimator consistent, maintainable, and compatible with future extensions (e.g., other algorithms or backends).

Changes Made

  • Reorganized KMeans and _BaseKMeans classes to follow the oneDAL estimator model pattern.

  • Added backend bindings using @bind_default_backend decorators:

  • train() → kmeans.clustering

  • infer() → kmeans.clustering

  • _is_same_clustering() → kmeans_common (no policy)

  • Centralized creation of the params dictionary in _get_onedal_params(...).

  • Ensured type dispatch and method dispatch use fptype and method respectively (e.g., by_default, lloyd_csr).

  • Wrapped all input/output in to_table / from_table.

  • Separated backend logic from estimator logic using _fit_backend() and _predict_backend().

  • Deferred model creation and attribute assignment to follow a minimalistic and clean init/finalization process.

  • Applied Sycl queue support via @supports_queue decorators for fit/predict/score.

  • Removed redundant or sklearn-only attributes that aren’t required by oneDAL estimators.

  • Preserved full feature parity (e.g., init modes, scoring, CSR support, random_state handling, etc.).


PR should start as a draft, then move to ready for review state after CI is passed and all applicable checkboxes are closed.
This approach ensures that reviewers don't spend extra time asking for regular requirements.

You can remove a checkbox as not applicable only if it doesn't relate to this PR in any way.
For example, PR with docs update doesn't require checkboxes for performance while PR with any change in actual code should have checkboxes and justify how this code change is expected to affect performance (or justification should be self-evident).

Checklist to comply with before moving PR from draft:

PR completeness and readability

  • I have reviewed my changes thoroughly before submitting this pull request.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation to reflect the changes or created a separate PR with update and provided its number in the description, if necessary.
  • Git commit message contains an appropriate signed-off-by string (see CONTRIBUTING.md for details).
  • I have added a respective label(s) to PR if I have a permission for that.
  • I have resolved any merge conflicts that might occur with the base branch.

Testing

  • I have run it locally and tested the changes extensively.
  • All CI jobs are green or I have provided justification why they aren't.
  • I have extended testing suite if new functionality was introduced in this PR.

Performance

  • I have measured performance for affected algorithms using scikit-learn_bench and provided at least summary table with measured data, if performance change is expected.
  • I have provided justification why performance has changed or why changes are not expected.
  • I have provided justification why quality metrics have changed or why changes are not expected.
  • I have extended benchmarking suite and provided corresponding scikit-learn_bench PR if new measurable functionality was introduced in this PR.

Copy link

codecov bot commented Aug 6, 2025

Codecov Report

❌ Patch coverage is 83.78378% with 12 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
onedal/cluster/kmeans.py 83.09% 8 Missing and 4 partials ⚠️
Flag Coverage Δ
azure 80.97% <82.43%> (+0.01%) ⬆️
github 73.24% <83.78%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sklearnex/cluster/k_means.py 89.93% <100.00%> (+0.20%) ⬆️
onedal/cluster/kmeans.py 79.62% <83.09%> (+0.65%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

"The default value of `n_init` will change from "
f"{default_n_init} to 'auto' in 1.4. Set the value of `n_init`"
" explicitly to suppress the warning"
f"{default_n_init} to 'auto' in 1.4. Set `n_init` explicitly "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have a utility to check the sklearn version, this could be placed under an if-else, or removed altogether considering we appear to not support versions 1.1 through 1.3.

elif callable(init):
cc_arr = init(X, self.n_clusters, random_state)
cc_arr = np.ascontiguousarray(cc_arr, dtype=dtype)
if hasattr(cc_arr, "__array_namespace__"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the function get_namespace supposed to be doing these checks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants