Skip to content

FEA Implement classical MDS - #31322

Merged
OmarManzoor merged 36 commits into
scikit-learn:mainfrom
dkobak:classical-mds
Sep 8, 2025
Merged

OmarManzoor merged 36 commits into
scikit-learn:mainfrom
dkobak:classical-mds

Conversation

@dkobak

@dkobak dkobak commented May 6, 2025 •

Copy link
Copy Markdown
Contributor

Fixes #15272. Supersedes #22330.

This PR implements classical MDS, also known as principal coordinates analysis (PCoA) or Torgerson's scaling, see https://en.wikipedia.org/wiki/Multidimensional_scaling#Classical_multidimensional_scaling. As discussed in #22330, it is implemented as a new class ClassicalMDS.

Simple demonstration:

import pylab as plt
import numpy as np

from sklearn.datasets import load_iris
from sklearn.manifold import ClassicalMDS
from sklearn.decomposition import PCA

X, y = load_iris(return_X_y=True)

Z1 = PCA(n_components=2).fit_transform(X)
Z2 = ClassicalMDS(n_components=2, metric="euclidean").fit_transform(X)
Z3 = ClassicalMDS(n_components=2, metric="cosine"   ).fit_transform(X)
Z4 = ClassicalMDS(n_components=2, metric="manhattan").fit_transform(X)

fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(6, 6), layout="constrained")

axs.flat[0].scatter(Z1[:,0], Z1[:,1], c=y)
axs.flat[0].set_title("PCA")

axs.flat[1].scatter(Z2[:,0], Z2[:,1], c=y)
axs.flat[1].set_title("Classical MDS, Euclidean dist.")

axs.flat[2].scatter(-Z3[:,0], Z3[:,1], c=y)
axs.flat[2].set_title("Classical MDS, cosine dist.")

axs.flat[3].scatter(Z4[:,0], Z4[:,1], c=y)
axs.flat[3].set_title("Classical MDS, Manhattan dist.")

cmds

Classical MDS is also set as default initialization for metric/non-metric MDS in the MDS() class.

For consistency, this PR also adds support for non-Euclidean metrics to the MDS class.

@github-actions

github-actions Bot commented May 6, 2025 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 1d53e85. Link to the linter CI: here

@dkobak

dkobak commented May 8, 2025

Copy link
Copy Markdown
Contributor Author

Pinging @antoinebaker for a review :-) I have been doing some updates to this PR but am now finished with it.

@antoinebaker

Copy link
Copy Markdown
Member

Thanks for the PR @dkobak !

Could you maybe use this PR to implement ClassicalMDS only, and do the enhancements of MDS in a separate / follow up PR ? It will ease the reviewing / merging process I think.

@dkobak

dkobak commented May 12, 2025 •

Copy link
Copy Markdown
Contributor Author

Could you maybe use this PR to implement ClassicalMDS only, and do the enhancements of MDS in a separate / follow up PR ? It will ease the reviewing / merging process I think.

All right. I took all changes to the MDS class out of this PR now. I will wait until this PR is merged and then do a follow-up PR to change the MDS class.

@dkobak dkobak changed the title Implement classical MDS FEA Implement classical MDS May 14, 2025
@antoinebaker

Copy link
Copy Markdown
Member

Could you please add ClassicalMDS to the common test suite for estimators ? It can be done by adding an entry to the INIT_PARAMS dict:

INIT_PARAMS = {
AdaBoostClassifier: dict(n_estimators=5),
AdaBoostRegressor: dict(n_estimators=5),

Then it will be tested for common checks on estimators in sklearn/tests/test_common.py::test_estimators

@dkobak

dkobak commented May 28, 2025

Copy link
Copy Markdown
Contributor Author

Hi @antoinebaker. I think the INIT_PARAMS dictionary is only needed to provide non-default params (like a small number of iterations), which for ClassicalMDS is not needed. An estimator does not have to be in INIT_PARAMS in order to be tested, it will simply be tested with default params in that case. Let me know if I am wrong.

@antoinebaker antoinebaker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here a first round of reviews

Comment thread sklearn/manifold/_classical_mds.py Resolved
Comment thread sklearn/manifold/_classical_mds.py Resolved
Comment thread sklearn/manifold/_classical_mds.py Resolved
Comment thread sklearn/manifold/_classical_mds.py Resolved
@antoinebaker

Copy link
Copy Markdown
Member

Hi @antoinebaker. I think the INIT_PARAMS dictionary is only needed to provide non-default params (like a small number of iterations), which for ClassicalMDS is not needed. An estimator does not have to be in INIT_PARAMS in order to be tested, it will simply be tested with default params in that case. Let me know if I am wrong.

Ah my bad, I think you're right. If ClassicalMDS appears in the sklearn/tests/test_common.py::test_estimators suite that's all good.

@adrinjalali

Copy link
Copy Markdown
Member

I'm not sure if we discussed this, but I favor this comment (#22330 (comment)) (except the default value) as an overall API, instead of introducing a new class. Any blockers for doing so?

@dkobak

dkobak commented Jun 3, 2025

Copy link
Copy Markdown
Contributor Author

@adrinjalali: Yes, we did discuss it. Just below the comment you linked to, @antoinebaker gave detailed reasons for why he prefers a separate class, please see here: #22330 (comment). He convinced me, and you wrote "I'm happy with the suggestions here" (#22330 (comment)), which is why I implemented a separate class...

@adrinjalali

Copy link
Copy Markdown
Member

Hmm. Yeah fair enough. Just to avoid future surprises, maybe @lorentzenchr @GaelVaroquaux could also give their opinion?

@antoinebaker

antoinebaker commented Jun 4, 2025 •

Copy link
Copy Markdown
Member

The TLDR of #22330 (comment) is that ClassicalMDS (Principal Coordinates Analysis) implemented in this PR and the current MDS (SMACOF) implemented in sklearn don't have much in common except their names (different algorithms, objectives, arguments, attributes).

EDIT: removed outdated comment.

@dkobak

dkobak commented Jul 16, 2025

Copy link
Copy Markdown
Contributor Author

@adrinjalali See above -- I have added examples. Let me know if there is anything else I can do on my side.

@antoinebaker antoinebaker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I read the examples, and overall this LGTM !

The nonmetric MDS seems to perform badly on S-curve and sphere (plot_compare_methods and plot_manifold_sphere). Is this an expected limitation of this method on such dataset ? Or is there something wrong in term of hyperparameters (eg convergence controlled by eps) ? Note that it's not directly related to this PR (which is on ClassicalMDS which seems to perform okay), so I still approve the changes. But it could be nice to fix this nonmetric mds example if possible.

@dkobak

dkobak commented Jul 30, 2025

Copy link
Copy Markdown
Contributor Author

The nonmetric MDS seems to perform badly on S-curve and sphere (plot_compare_methods and plot_manifold_sphere). Is this an expected limitation of this method on such dataset ? Or is there something wrong in term of hyperparameters (eg convergence controlled by eps) ? Note that it's not directly related to this PR (which is on ClassicalMDS which seems to perform okay), so I still approve the changes. But it could be nice to fix this nonmetric mds example if possible.

Yes, I also noticed that. It is not expected and should be fixed. Optimization of non-metric MDS is finicky and I am not sure what exactly is going wrong there. But I would like to switch both metric and non-metric MDS to use classical MDS as default initialization (rather than random initialization), and I think this should take care of this issue.

Once this PR is merged, I will prepare a follow-up PR that

  1. adds other metrics to the MDS class (metric="cosine" etc.)
  2. changes the current metric=True argument to the MDS class to metric_mds=True
  3. sets classical MDS as default initialization for MDS.

@dkobak

dkobak commented Aug 4, 2025

Copy link
Copy Markdown
Contributor Author

Hi @adrinjalali, just a gentle ping to take another look here. But if you are on holidays then enjoy your holidays :-)

@adrinjalali

Copy link
Copy Markdown
Member

Thanks for the work so far. I'm happy with what we have, except the docstring / userguide for this class. In none of the two places the formulation is explained well enough I feel. Could you please improve the small section in the user guide and also the docstring please?

@dkobak

dkobak commented Aug 4, 2025

Copy link
Copy Markdown
Contributor Author

@adrinjalali Fair enough. I added some details, take a look if this is sufficient now.

Comment thread doc/modules/manifold.rst Resolved
Comment thread sklearn/manifold/_classical_mds.py Resolved

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thanks. Since it's a new estimator, I'd like another pair of eyes on this before merging. @adam2392 or @OmarManzoor could have a look maybe?

@adam2392
adam2392 self-requested a review August 5, 2025 14:51
@dkobak

dkobak commented Aug 25, 2025

Copy link
Copy Markdown
Contributor Author

Dear @adam2392, hope you are having a nice summer! Just a gentle reminder that it would be amazing if you could look at this PR :-) Cheers.

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Thanks @dkobak

@OmarManzoor
OmarManzoor merged commit 730d651 into scikit-learn:main Sep 8, 2025
@dkobak
dkobak deleted the classical-mds branch September 19, 2025 17:03
@dkobak

dkobak commented Sep 20, 2025

Copy link
Copy Markdown
Contributor Author

Dear all, as discussed above, now that this PR is merged, I prepared a follow-up PR that modifies the MDS class accordingly. I'll be grateful for reviews there! See #32229. Would be great to merge it into the same sklearn version (1.8).

@lesteve lesteve mentioned this pull request Dec 2, 2025
7 tasks done
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.

sklearn MDS vs skbio PCoA

4 participants