Repository navigation
MNT carry out 1.10 deprecations in MDS - #35261
Conversation
cakedev0
left a comment
There was a problem hiding this comment.
Thanks for the PR! LGTM overall, only nits below.
Also, some additional possible clean-ups you could do in this PR:
- remove
init="random"in the class docstring - remove
init="classical_mds"inplot_mds.py,plot_compare_methods.py,plot_lle_digits.pyandplot_manifold_sphere.py.
Those 2 were only there to silence the FutureWarning.
| init : {'random', 'classical_mds'}, default='classical_mds' | ||
| The initialization approach. If `random`, random initialization is used. | ||
| If `classical_mds`, then classical MDS is run and used as initialization | ||
| for MDS (in this case, the value of `n_init` is ignored). |
There was a problem hiding this comment.
With classical_mds as the default, n_init, n_jobs and random_state have no effect unless init="random". Could you say so in their docstrings? (The random_state one also says "initialize the centers", which comes from KMeans.)
There was a problem hiding this comment.
I also removed n_init, random_state and n_jobs where MDS is initialised with init=classical_mds since they are ignored.
| .. versionchanged:: 1.8 | ||
| Prior to 1.8, `metric=True/False` was used to select metric/non-metric | ||
| MDS, which is now the role of `metric_mds`. The support for ``True`` | ||
| and ``False`` will be dropped in version 1.10, use `metric_mds` instead. | ||
| and ``False`` was dropped in version 1.10, use `metric_mds` instead. |
There was a problem hiding this comment.
I'd keep the 1.8 note as is and add a separate entry for 1.10:
| .. versionchanged:: 1.8 | |
| Prior to 1.8, `metric=True/False` was used to select metric/non-metric | |
| MDS, which is now the role of `metric_mds`. The support for ``True`` | |
| and ``False`` will be dropped in version 1.10, use `metric_mds` instead. | |
| and ``False`` was dropped in version 1.10, use `metric_mds` instead. | |
| .. versionchanged:: 1.8 | |
| Prior to 1.8, `metric=True/False` was used to select metric/non-metric | |
| MDS, which is now the role of `metric_mds`. | |
| .. versionchanged:: 1.10 | |
| Boolean values for `metric` are no longer supported, use `metric_mds` | |
| instead. |
| "'classical_mds' in 1.10. To suppress this warning, provide " | ||
| "some value of `init`.", | ||
| FutureWarning, | ||
| if isinstance(self.metric, (bool, np.bool_)): |
There was a problem hiding this comment.
When carrying out deprecations we usually just remove the option from the constraints and let param validation raise (e.g. n_init="warn" in #33595). Here it would give:
InvalidParameterError: The 'metric' parameter of MDS must be an instance of 'str' or a callable. Got True instead.
I see the point of a more helpful message since metric changed meaning, but it means keeping a hidden constraint and a manual check with no plan to remove them. I'd remove Hidden("boolean") from the metric constraint and delete this isinstance block, so that param validation rejects booleans like any other invalid value. WDYT?
There was a problem hiding this comment.
I guess I was a bit overzealous in trying to keep the error message clear, but the deprecation cycle should be enough to warn users. Thinking about it it's true that not having a plan to remove the specific message feels weird, but putting one would be adding an extra deprecation cycle, which should not be necessary. I'll remove the custom error message.
Co-authored-by: Arthur Lacote <[email protected]>
todo: move [sklearn#35295](scikit-learn/scikit-learn#35295) from Waiting to Now: address lorentzenchr's review on removing the `_safe_indexing` alias add: Maintenance: re-review [sklearn#35261](scikit-learn/scikit-learn#35261) (1.10 deprecations in MDS; FrancoisPgm addressed my comments) todo: betatim's fixes on [sklearn#35053](scikit-learn/scikit-learn#35053), ogrisel asking jjerphan to review [sklearn#34935](scikit-learn/scikit-learn#34935) Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Also cc'ing @dkobak for visibility. |
|
Thanks for implementing this, looks good to me together with @cakedev0 helpful suggestions above. |
| best_pos, best_stress = None, None | ||
|
|
||
| if effective_n_jobs(n_jobs) == 1: | ||
| if effective_n_jobs(n_jobs) == 1 or n_init == 1: |
There was a problem hiding this comment.
n_jobs was actually not ignored when using init='classical_mds', as having n_jobs > 1 would here force to use joblib to parallelize, but with n_init forced to one, so the parallelized for loop would only iterate over one iteration. I believe it can only be slower than not using parallelization at all, so I suggest not to go through the parallelized path when n_init == 1. WDYT @cakedev0 ?
There was a problem hiding this comment.
This shouldn't change the result right? Have you checked that it doesn't?
If I understand this right then we are "just" avoiding an unnecessary use of joblib, but the computation that happens is the same in either case. Is that also what you think happens?
If yes, then I think it is fine to make this change. If no, it is more complicated. We can make this change if the result when using joblib is wrong (this is a bugfix). If the joblib result isn't wrong, just different I am not sure what to do.
There was a problem hiding this comment.
Yes it should not change the result, in both paths of the if statement, the same _smacof_single method will be used with the same arguments[1]. It should already be the case since changing the value of n_jobs should only change the optimization and not numerical results, so I admit I initially just read the code to convince myself that it was equivalent, but with the help of an LLM I made a quick code snippet to check that both are equivalent, and they are.
The code snippet in question (ran with the `or n_init == 1` commented out, otherwise you always pass through the first branch of the if).
import numpy as np
from numpy.testing import assert_allclose, assert_equal
from sklearn.manifold import MDS
rng = np.random.RandomState(0)
X = rng.randn(8, 3)
mds_1 = MDS(n_jobs=1)
mds_2 = MDS(n_jobs=2)
embedding_1 = mds_1.fit_transform(X)
embedding_2 = mds_2.fit_transform(X)
assert_allclose(embedding_1, embedding_2)
assert_allclose(mds_1.stress_, mds_2.stress_)
assert_equal(mds_1.n_iter_, mds_2.n_iter_)[1] : Except for random_state but with init="classical_mds" it is ignored, and with init="random", adapting the test snippet above with mds_1 = MDS(n_jobs=1, init="random", random_state=0) and mds_2 = MDS(n_jobs=2, init="random", random_state=0) keeps the assertions true.
EDIT : actually, I messed up with the test snippet execution the first time, and doing it properly shows that with init="random", using n_jobs=1 or n_jobs>1 does change the results as it changes the seeding. It was already the case before this PR, now what is different is that with init="random" and n_init=1, you'll always get the seeding corresponding to the first if branch, so people with MDS(init="random", n_init=1, n_jobs=n) with n>1 will have a different result. I think that is acceptable.
There was a problem hiding this comment.
I agree. The results only change with a fixed random_state for init="random", n_init=1, n_jobs>1. They then match n_jobs=1, which is unchanged. So completely fine and this change is what makes "n_jobs is ignored" true in the docstring.
done: HGB: get [sklearn#35216](scikit-learn/scikit-learn#35216) merged (binning crash on all-missing features, fixes [sklearn#35164](scikit-learn/scikit-learn#35164)), merged before I removed it done: Trees: get [sklearn#34554](scikit-learn/scikit-learn#34554) merged (recursion PDP on categorical features) drop: Threads: answer lesteve on [sklearn#35105](scikit-learn/scikit-learn#35105) (RFC deprecate `pre_dispatch`) drop: Maintenance: follow up on [sklearn#35209](scikit-learn/scikit-learn#35209) (newton-cg test failing on the macOS x86_64 wheels) drop: Benchmarks: review [benchmarks#142](probabl-ai/scikit-learn-benchmarks#142) (cuML benchmark config) drop: Threads: review [sklearn#34729](scikit-learn/scikit-learn#34729) (inference methods don't modify the estimator) drop: Linear: [sklearn#34793](scikit-learn/scikit-learn#34793) (Ridge centering trick for cholesky), benchmarks in [benchmarks#115](probabl-ai/scikit-learn-benchmarks#115) and [benchmarks#31](probabl-ai/scikit-learn-benchmarks#31) drop: Upstream: sklearnex reports [sklearnex#3402](uxlfoundation/scikit-learn-intelex#3402), [sklearnex#3377](uxlfoundation/scikit-learn-intelex#3377), [sklearnex#3220](uxlfoundation/scikit-learn-intelex#3220) drop: Upstream: [data-apis/array-api#976](data-apis/array-api#976) (why `stable=True` is the default for `xp.sort`) drop: Upstream: dpnp reports [IntelPython/dpnp#3149](IntelPython/dpnp#3149) and [IntelPython/dpnp#3148](IntelPython/dpnp#3148) drop: Threads: [sklearn#34736](scikit-learn/scikit-learn#34736) (oversubscription in `pairwise_distances`), out of the reported findings item drop: KMeans: benchmarks [benchmarks#154](probabl-ai/scikit-learn-benchmarks#154), [benchmarks#160](probabl-ai/scikit-learn-benchmarks#160), [OpenBLAS#6105](OpenMathLib/OpenBLAS#6105) and [sklearn#17334](scikit-learn/scikit-learn#17334), out of the [sklearn#35193](scikit-learn/scikit-learn#35193) item todo: move [joblib#1899](joblib/joblib#1899), [sklearn#35261](scikit-learn/scikit-learn#35261) and [sklearn#35053](scikit-learn/scikit-learn#35053) from Reviews to Now, [sklearn#35181](scikit-learn/scikit-learn#35181) to the top of Waiting (by hand in 03835dd) Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
AI found that
Should we have a test that checks something like |
Good catch, thanks, your AI has a better eye than mine ^^
I see the point of removing the |
Let's do |
cakedev0
left a comment
There was a problem hiding this comment.
LGTM, some tiny nits below,:
_smacof_single(L76) andsmacof(L302) still say "initialize the centers" in theirrandom_statedocstrings, the same KMeans wording you fixed inMDS.
Co-authored-by: Arthur Lacote <[email protected]>
|
I accepted the last comment and I enabled auto-merge, thanks a lot @FrancoisPgm for the PR and @betatim + @cakedev0 + @virchan + @dkobak for the reviews! |
Reference Issues/PRs
Towards #35252
What does this implement/fix? Explain your changes.
Carry out the deprecation of
dissimilarityin favor ofmetricsinsklearn.manifold.MDS, also deprecate the use ofmetrics=Trueormetrics=Falsein favor ofmetrics_mds. Change the default orinitfrom"random"to"classical_mds". These deprecations originate from #32229.AI usage disclosure
I used AI assistance for:
Any other comments?