Skip to content

Non-metric MDS gives nonsense results on the Digits dataset #27028

Closed

Description

@dkobak

Describe the bug

I am running metric and nonmetric MDS on the Digits dataset, and obtain nonsense results with metric=False. At the same time, my understanding is that non-metric MDS is more flexible and should not yield worse results compared to metric MDS. Is my understanding wrong, or does it suggest some problems with the non-metric MDS implementation?

Steps/Code to Reproduce

%matplotlib notebook

import pylab as plt
import numpy as np
from sklearn.datasets import load_digits
from sklearn.manifold import MDS

digits = load_digits()
X, y = digits.data, digits.target

Z1 = MDS(n_components=2, n_init=1, random_state=42).fit_transform(X)
Z2 = MDS(n_components=2, n_init=1, random_state=42, metric=False).fit_transform(X)

titles = ['Metric MDS', 'Non-metric MDS']

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

for i,Z in enumerate([Z1, Z2]):
    ax = axs.ravel()[i]
    
    ax.set_aspect('equal', 'datalim')
    
    ind = np.random.permutation(X.shape[0])
    ax.scatter(*Z[ind].T, s=3, color=plt.cm.Dark2(y)[ind])

    ax.set_xticks([])
    ax.set_yticks([])
    for sp in ax.spines:
        ax.spines[sp].set_visible(False)
        
    ax.set_title(titles[i])
    
fig.savefig('digits-embed-mds-nonmetric.png', dpi=300)

Expected Results

Non-metric MDS giving something sensible.

Actual Results

digits-embed-mds-nonmetric

Versions

System:
    python: 3.7.9 (default, Aug 31 2020, 12:42:55)  [GCC 7.3.0]
executable: /home/dmitry/anaconda3/bin/python
   machine: Linux-5.15.0-76-generic-x86_64-with-debian-bullseye-sid

Python dependencies:
          pip: 22.3.1
   setuptools: 65.6.3
      sklearn: 0.24.1
        numpy: 1.21.5
        scipy: 1.7.3
       Cython: 0.29.32
       pandas: 1.3.5
   matplotlib: 3.5.2
       joblib: 1.1.1
threadpoolctl: 2.2.0

Built with OpenMP: True

Activity

  1. added
    Needs Triage
    on Aug 7, 2023
  2. dkobak commented on Aug 9, 2023

    @dkobak
    ContributorAuthor

    Maybe @NelleV would know what's going on here? She is the main author of the MDS class, it seems.

  3. Micky774 commented on Aug 11, 2023

    @Micky774
    Contributor

    At the same time, my understanding is that non-metric MDS is more flexible

    Yes, I would expect its solution space is a superset of metric MDS

    and should not yield worse results compared to metric MDS

    This I'm less confident about. It's flexibility and optimization strategy honestly may make it more liable to find "suboptimal" or "poor" embeddings since it "only" cares about ordinality of data.

    I really am not sure whether this is just an example of how constrained systems can outperform under-constrained systems, or if there is something operatively incorrect about the non-metric MDS implementation, but I lean towards the prior.

  4. added
    Needs Investigation
    and removed
    Needs Triage
    on Aug 11, 2023
  5. dkobak commented on Aug 11, 2023

    @dkobak
    ContributorAuthor

    I really am not sure whether this is just an example of how constrained systems can outperform under-constrained systems, or if there is something operatively incorrect about the non-metric MDS implementation, but I lean towards the prior.

    No, I'm actually pretty sure it's the latter.

    First, metric MDS takes 756 iterations to converge. Default is max_iter=300, but I checked with

    Z1 = MDS(n_components=2, n_init=1, random_state=42, metric=True, verbose=2, max_iter=1000).fit_transform(X)
    

    But non-metric MDS stops after two iterations

    Z2 = MDS(n_components=2, n_init=1, random_state=42, metric=False, verbose=2).fit_transform(X)
    

    Here is the full output:

    it: 0, stress 88230.49297090062
    it: 1, stress 73509.74850291299
    it: 2, stress 73810.46739972188
    breaking at iteration 2 with stress 73810.46739972188
    

    This is very fishy. The loss goes up, so it stops the iterations.

    Second, let's initialize non-metric optimiazation with the result of metric optimization:

    Z3 = MDS(n_components=2, n_init=1, random_state=42, metric=False, verbose=2).fit_transform(X, init=Z1)
    

    Now it also converges in two iterations, but achieves much lower loss:

    it: 0, stress 3355940054.205797
    it: 1, stress 33534.27419788723
    it: 2, stress 33605.33211344484
    breaking at iteration 2 with stress 33605.33211344484
    

    This is also very fishy -- that's 2x lower loss than the default optimization run.

    digits-embed-mds-nonmetric-2

  6. dkobak commented on Sep 6, 2023

    @dkobak
    ContributorAuthor

    What do you think @Micky774? To me the above is evidence that the optimisation is not working correctly.

  7. dkobak commented on Oct 8, 2023

    @dkobak
    ContributorAuthor

    May be related to #18933 but I'm not sure. CC @henrymartin1.

  8. Micky774 commented on Oct 11, 2023

    @Micky774
    Contributor

    What do you think @Micky774? To me the above is evidence that the optimisation is not working correctly.

    Yeah at this point it definitely seems like something is wrong. I unfortunately don't have much extra bandwidth for this at the moment, but I will offer a review if anyone else would like to take a stab at this.

    May be related to #18933 but I'm not sure

    I would like to see this issue worked on first, then revisit this instance to determine if the bug persists.

    Thank you for taking the time to advocate for this issue :)

  9. dkobak commented on Dec 20, 2024

    @dkobak
    ContributorAuthor

    Should be fixed in my #30514.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds Investigation

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions