Skip to content

Prepare for n_init=auto in KMeans #6142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Feb 25, 2025

Conversation

betatim
Copy link
Member

@betatim betatim commented Nov 22, 2024

This adds a warning for the upcoming switch from n_init=1 to 'auto'. This adds the possibility to use 'auto', which helps being compatible with sickit-learn.

In which version should we switch to the new default?

@betatim betatim requested a review from a team as a code owner November 22, 2024 14:22
@betatim betatim requested review from cjnolet and wphicks November 22, 2024 14:22
@github-actions github-actions bot added the Cython / Python Cython or Python issue label Nov 22, 2024
@wphicks
Copy link
Contributor

wphicks commented Nov 22, 2024

I'd recommend we switch in 25.04 to get compatibility with the scikit-learn change as soon as possible.

@betatim
Copy link
Member Author

betatim commented Nov 22, 2024

Works for me. There is a PR for a forward merge to 25.02, which makes me think that there won't be a 25.04? Is that right? 25.05 in that case?

Copy link
Member

@dantegd dantegd left a comment

Choose a reason for hiding this comment

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

Just one comment about versions, I think being quick with the change is a good idea as @wphicks suggests.

Comment on lines 210 to 212
sklearn_args = inspect.signature(self._cpu_model_class)
sklearn_args = sklearn_args.bind(*args, **kwargs)
sklearn_args.apply_defaults()
Copy link
Member Author

Choose a reason for hiding this comment

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

This means we use the constructor arguments and their default values of the scikit-learn class, combine them with what the user passed in and then feed it to the hyperparameter translator. This makes a difference for cases where the default values in scikit-learn and cuml are different and the user does not explicitly pass that argument.

# The scikit-learn class
class SkTimMeans:
  def __init__(self, foo='bar'):
    ...

# The cuml class
class CuTimMeans:
  def __init__(self, foo='baz'):
    ...

# User code
est = SkTimMeans()

foo should be set to 'bar' in est because that is the default value of the scikit-learn class.

It also fixes the many deprecation warnings in the accelerator tests we were getting for KMeans due to the main change of this PR.

Copy link
Member

Choose a reason for hiding this comment

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

This change is breaking quite a few tests and is orthogonal to the change in n_init, it'd be better to revert it and work on it on a follow up PR, since having KMeans init auto is the objective on this PR

Copy link
Member Author

Choose a reason for hiding this comment

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

It wasn't totally unrelated. It solves the problem that without this the user would get a warning about the deprecation, even though for a scikit-learn user the default is "auto" already (based on the scikit-learn docs).

Switched to using the cuml class'es defaults to see if this creates less breakage.

If we don't use the signature of the constructor then we need to build a way for the hyper-parameter translator to translate arguments that the user didn't pass (I think this is the fundamental issue)

Copy link
Member Author

Choose a reason for hiding this comment

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

With this approach we get failures like this:

sklearn.utils._param_validation.InvalidParameterError: The 'init' parameter of KMeans must be a str among {'random', 'k-means++'}, a callable or an array-like. Got 'scalable-k-means++' instead.

because the cuml default values are different from the scikit-learn ones :-/

@betatim betatim changed the base branch from branch-24.12 to branch-25.02 December 19, 2024 08:31
Copy link
Member

@dantegd dantegd left a comment

Choose a reason for hiding this comment

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

lgtm, just requested small changes about the version

dantegd
dantegd previously requested changes Feb 9, 2025
Comment on lines 210 to 212
sklearn_args = inspect.signature(self._cpu_model_class)
sklearn_args = sklearn_args.bind(*args, **kwargs)
sklearn_args.apply_defaults()
Copy link
Member

Choose a reason for hiding this comment

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

This change is breaking quite a few tests and is orthogonal to the change in n_init, it'd be better to revert it and work on it on a follow up PR, since having KMeans init auto is the objective on this PR

@betatim betatim changed the base branch from branch-25.02 to branch-25.04 February 13, 2025 12:28
betatim and others added 15 commits February 13, 2025 04:28
This adds a warning for the upcoming switch from n_init=1 to 'auto'.
This adds the possibility to use 'auto', which helps being compatible
with sickit-learn.
This way the hyper-parameter translator gets to see all arguments and we
avoid deprecation warnings due to mismatches between the sklearn and
cuml defaults
Co-authored-by: Dante Gama Dessavre <[email protected]>
@betatim betatim added improvement Improvement / enhancement to an existing function non-breaking Non-breaking change labels Feb 19, 2025
@betatim
Copy link
Member Author

betatim commented Feb 19, 2025

Fixed up the last failing test, hopefully.

@csadorf
Copy link
Contributor

csadorf commented Feb 19, 2025

The tests are failing with a FutureWarning:

FutureWarning: The default value of `n_init` will change from 1 to 'auto' in 25.04. Set the value of `n_init` explicitly to suppress this warning.

@betatim
Copy link
Member Author

betatim commented Feb 20, 2025

There are two failures here:

FAILED test_public_methods_attributes.py::test_UniversalBase_estimators[KMeans] - FutureWarning: The default value of `n_init` will change from 1 to 'auto' in 25.04. Set the value of `n_init` explicitly to suppress this warning.
FAILED test_sklearn_import_export.py::test_kmeans - FutureWarning: The default value of `n_init` will change from 1 to 'auto' in 25.04. Set the value of `n_init` explicitly to suppress this warning.

The one in test_public_methods_attributes.py should be gone now with ignoring the deprecation warning. The second one didn't relaly make sense, after a bit of investigating I found #6342.

We can silence the warning with a decorator on test_kmeans but I think all that does is hide a bug :-/

@betatim betatim dismissed dantegd’s stale review February 24, 2025 16:33

I think the review comments are outdated now

@csadorf csadorf assigned csadorf and unassigned betatim Feb 24, 2025
@csadorf
Copy link
Contributor

csadorf commented Feb 24, 2025

/merge

@rapids-bot rapids-bot bot merged commit a49b24a into rapidsai:branch-25.04 Feb 25, 2025
64 of 65 checks passed
dantegd added a commit to dantegd/cuml that referenced this pull request Feb 25, 2025
@betatim betatim deleted the kmeans-auto-2 branch February 25, 2025 12:42
dantegd added a commit to dantegd/cuml that referenced this pull request Feb 26, 2025
raydouglass pushed a commit that referenced this pull request Feb 28, 2025
PRs being backported: 

- [x] #6234
- [x] #6306
- [x] #6320
- [x] #6319
- [x] #6327
- [x] #6333
- [x] #6142 
- [x] #6223
- [x] #6235
- [x] #6317 
- [x] #6331
- [x] #6326
- [x] #6332
- [x] #6347
- [x] #6348
- [x] #6337
- [x] #6355
- [x] #6354
- [x] #6322
- [x] #6353
- [x] #6359
- [x] #6364
- [x] #6363
- [x] [FIL BATCH_TREE_REORG fix for SM90, 100 and
120](a3e419a)

---------

Co-authored-by: William Hicks <[email protected]>
rishic3 added a commit to NVIDIA/spark-rapids-ml that referenced this pull request Mar 6, 2025
Relevant PRs: [cuml 6235](rapidsai/cuml#6235),
[cuml 6142](rapidsai/cuml#6142), for sklearn
compatibility.
- n_init defaults to "warn" as a future warning for the user (still ends
up defaulting to 1), since the default will change to "auto" in 25.04 to
match sklearn.
- LR output shape changed from (n_classes, n_rows) to (n_rows,
n_classes) to match sklearn.

Resolves #860

---------

Signed-off-by: Rishi Chandra <[email protected]>
@csadorf
Copy link
Contributor

csadorf commented Mar 13, 2025

@betatim Can you create a follow-up to remove this now obsolete code segment relating to this PR?

# This failure will be fixed by
# https://github.com/rapidsai/cuml/pull/6142
# otherwise the predict with default n_init like this
# roundtrip will fail later.
pytest.xfail(reason="auto is not supported by cuML n_init yet")

rapids-bot bot pushed a commit that referenced this pull request Mar 18, 2025
Follow up to #6142 (comment)

This cleans up some leftovers from the deprecation cycle for `KMeans`'s `init="auto"`

Authors:
  - Tim Head (https://github.com/betatim)
  - Jim Crist-Harif (https://github.com/jcrist)

Approvers:
  - Jim Crist-Harif (https://github.com/jcrist)

URL: #6445
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cuml-cpu Cython / Python Cython or Python issue improvement Improvement / enhancement to an existing function non-breaking Non-breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants