[BUG] Fix TimeSeriesKernelKMeans mutating kernel when kernel="kdtw" - #3424
Conversation
`_fit` resolved the "kdtw" string to a local callable and assigned it back to `self.kernel`, mutating the public parameter. This broke parameter consistency (`get_params()` returned a function instead of "kdtw") and made repeated `fit` calls take a different code path, since `self.kernel == "kdtw"` no longer held. Store the resolved callable in a fitted `self.kernel_` attribute instead, leaving the `kernel` parameter untouched. Document `kernel_` in the class docstring and add a regression test. Fixes #3422
Thank you for contributing to
|
|
wow that was quick :) failure not related, thanks for this |
|
The only failing job ( It is a pre-existing flaky test: Evidence that it is unrelated/flaky: every full I'll push an empty commit to re-trigger CI. Happy to open a separate PR hardening that test (passing a fixed |
|
its fine, I can retrigger the failing test only rather than rerun the whole CI, just let it run through |
|
Thanks, sounds good — I'll leave it with you. Happy to open a separate PR adding a fixed |
|
flaky test a separate issue, thanks |
|
@all-contributors please add @maxime2476 for bug |
|
I've put up a pull request to add @maxime2476! 🎉 |
Reference Issues/PRs
Fixes #3422
What does this implement/fix? Explain your changes.
When
kernel="kdtw",TimeSeriesKernelKMeans._fitresolved the"kdtw"string into a local callable and assigned it back to
self.kernel, mutatingthe public parameter. This had two consequences:
get_params()returned a function object instead of the original"kdtw"string, breaking scikit-learn parameter consistency (clone, repr, etc.).
fitno longer entered the KDTW branch, because theself.kernel == "kdtw"check no longer held.This PR stores the resolved callable in a fitted
self.kernel_attribute andpasses that to the underlying
tslearnestimator, leaving thekernelparameter untouched. This follows the scikit-learn convention that
__init__parameters are never modified during
fit, and that derived state lives in atrailing-underscore attribute.
Does your contribution introduce a new dependency? If yes, which one?
No.
Any other comments?
kernel_is documented in the classAttributessection.test_kernel_k_means_kdtw_does_not_mutate_kernel, which asserts thatkernel/get_params()["kernel"]stay"kdtw", thatkernel_is theresolved callable, and that a repeated
fitproduces identical labels.PR checklist
[BUG].