Skip to content

TSNE with correlation metric: ValueError: Distance matrix 'X' must be symmetric #4475

Closed as not planned

Description

@cel4
from sklearn.manifold import TSNE
import numpy as np
np.random.seed(42)

data = np.random.rand(10, 3)
data[-1, :] = 0

model = TSNE(metric="correlation")
model.fit_transform(data)

TSNE raises an obscure error, when the data set contains rows with a standard deviation 0 and therefore undefined correlations:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-658142c1e315> in <module>()
      1 model = TSNE(metric="correlation")
----> 2 res = model.fit_transform(data)
      3 ran = model.fit_transform(ran_data)

/Users/ch/miniconda/envs/sci34/lib/python3.4/site-packages/sklearn/manifold/t_sne.py in fit_transform(self, X, y)
    522             Embedding of the training data in low-dimensional space.
    523         """
--> 524         self.fit(X)
    525         return self.embedding_

/Users/ch/miniconda/envs/sci34/lib/python3.4/site-packages/sklearn/manifold/t_sne.py in fit(self, X, y)
    447         self.training_data_ = X
    448 
--> 449         P = _joint_probabilities(distances, self.perplexity, self.verbose)
    450         if self.init == 'pca':
    451             pca = RandomizedPCA(n_components=self.n_components,

/Users/ch/miniconda/envs/sci34/lib/python3.4/site-packages/sklearn/manifold/t_sne.py in _joint_probabilities(distances, desired_perplexity, verbose)
     52     P = conditional_P + conditional_P.T
     53     sum_P = np.maximum(np.sum(P), MACHINE_EPSILON)
---> 54     P = np.maximum(squareform(P) / sum_P, MACHINE_EPSILON)
     55     return P
     56 

/Users/ch/miniconda/envs/sci34/lib/python3.4/site-packages/scipy/spatial/distance.py in squareform(X, force, checks)
   1479             raise ValueError('The matrix argument must be square.')
   1480         if checks:
-> 1481             is_valid_dm(X, throw=True, name='X')
   1482 
   1483         # One-side of the dimensions is set here.

/Users/ch/miniconda/envs/sci34/lib/python3.4/site-packages/scipy/spatial/distance.py in is_valid_dm(D, tol, throw, name, warning)
   1562                 if name:
   1563                     raise ValueError(('Distance matrix \'%s\' must be '
-> 1564                                      'symmetric.') % name)
   1565                 else:
   1566                     raise ValueError('Distance matrix must be symmetric.')

ValueError: Distance matrix 'X' must be symmetric

Activity

  1. amueller commented on Mar 31, 2015

    @amueller
    Member

    Thanks for the report. Maybe the best fix would be to not make the metric produce NaNs and instead produce zeros in metrics. That is how we usually deal with zero variance cases.

  2. added
    Easy
    on Mar 31, 2015
  3. added 2 commits that reference this issue on Apr 2, 2015
  4. littmus commented on Aug 25, 2015

    @littmus

    It also happens when using cosine metric.

  5. amueller commented on Aug 25, 2015

    @amueller
    Member

    @littmus can you give a small example?

  6. added this to the 0.17 milestone on Sep 9, 2015
  7. giorgiop commented on Sep 30, 2015

    @giorgiop
    Contributor

    @littmus can you give a small example?

    Same as above, with cosine

    from sklearn.manifold import TSNE
    import numpy as np
    np.random.seed(42)
    
    data = np.random.rand(10, 3)
    data[-1, :] = 0
    
    model = TSNE(metric="cosine")
    model.fit_transform(data)
    

    In the case of cosine the problem is with negative values instead of NaN:

    >>> pairwise_distances(data, metric='cosine')
    array([[  2.22044605e-16,   3.93068804e-01,   3.16458410e-02,
              3.41088501e-01,   4.14053926e-01,   6.83873363e-02,
              1.22318330e-01,   2.66348778e-02,   8.99886820e-02,
              1.00000000e+00],
           [  3.93068804e-01,   1.11022302e-16,   6.08604862e-01,
              2.45187852e-01,   7.51253761e-04,   4.08564041e-01,
              2.20856854e-01,   4.02945244e-01,   2.80685191e-01,
              1.00000000e+00],
           [  3.16458410e-02,   6.08604862e-01,   2.22044605e-16,
              4.93788565e-01,   6.31623359e-01,   1.18832183e-01,
              2.39696816e-01,   6.75443089e-02,   1.57980145e-01,
              1.00000000e+00],
           [  3.41088501e-01,   2.45187852e-01,   4.93788565e-01,
             -2.22044605e-16,   2.69769895e-01,   1.52418674e-01,
              6.20247116e-02,   2.16408658e-01,   5.22888116e-01,
              1.00000000e+00],
           [  4.14053926e-01,   7.51253761e-04,   6.31623359e-01,
              2.69769895e-01,   0.00000000e+00,   4.38078471e-01,
              2.45155524e-01,   4.29871796e-01,   2.86287653e-01,
              1.00000000e+00],
           [  6.83873363e-02,   4.08564041e-01,   1.18832183e-01,
              1.52418674e-01,   4.38078471e-01,   0.00000000e+00,
              3.99266855e-02,   1.00043973e-02,   2.74710246e-01,
              1.00000000e+00],
           [  1.22318330e-01,   2.20856854e-01,   2.39696816e-01,
              6.20247116e-02,   2.45155524e-01,   3.99266855e-02,
              0.00000000e+00,   5.95202718e-02,   2.66727644e-01,
              1.00000000e+00],
           [  2.66348778e-02,   4.02945244e-01,   6.75443089e-02,
              2.16408658e-01,   4.29871796e-01,   1.00043973e-02,
              5.95202718e-02,   0.00000000e+00,   1.94449773e-01,
              1.00000000e+00],
           [  8.99886820e-02,   2.80685191e-01,   1.57980145e-01,
              5.22888116e-01,   2.86287653e-01,   2.74710246e-01,
              2.66727644e-01,   1.94449773e-01,   2.22044605e-16,
              1.00000000e+00],
           [  1.00000000e+00,   1.00000000e+00,   1.00000000e+00,
              1.00000000e+00,   1.00000000e+00,   1.00000000e+00,
              1.00000000e+00,   1.00000000e+00,   1.00000000e+00,
              1.00000000e+00]])
    
  8. ogrisel commented on Sep 30, 2015

    @ogrisel
    Member

    For the cosine metric it should be fixed a posteriori by changings negative numbers with small absolute value (e.g. smaller than 10 * np.finfo(X.dtype).eps) to be set to zero.

    For the correlation distance nans, it looks like a problem in scipy's pdist.

  9. giorgiop commented on Oct 1, 2015

    @giorgiop
    Contributor

    Returning Nan seems a chosen behaviour in Scipy and not a bug. See for example scipy/#3728. I think we should handle those on our side.

    scipy/#3728 also discusses the problem of negative values due to finite arithmetic calculation issues. I will do some experiments.

    By the way, we would get a similar issue with missing value, but I don't think we want to handle that explicitly. See scipy/#3870.

  10. giorgiop commented on Oct 1, 2015

    @giorgiop
    Contributor

    I have tried all the sklearn.metric.pairwise._VALID_METRICS, but wminkowski which is not compatible with t-sne. They include also scipy's ones. The only issues are with correlation,yule, dice and sokalsneath with Nan and cosine with negative values. The latter is the only one built in sklearn.

    Regarding the negative values, we can follow what suggested by @ogrisel and it works fine. Or we could use the trick with np.ptp as proposed in scipy/#3728. However, clipping values to zero was already done for euclidean_distances and we should have a consistent solution.

  11. amueller commented on Oct 13, 2015

    @amueller
    Member

    @giorgiop there is no missing values (in scikit-learn, apart from the preprocessing module)

  12. amueller commented on Oct 13, 2015

    @amueller
    Member

    Clipping to zero sounds good. For correlation, I'd say we should set the distance between constant points to zero.

  13. amueller commented on Oct 13, 2015

    @amueller
    Member

    as post-processing?

  14. 10 remaining items

  15. asanakoy commented on Oct 24, 2016

    @asanakoy
    Contributor

    Pool request fixing negative values: #7732.

  16. modified the milestone: 0.19 on Jun 12, 2017
  17. jnothman commented on Jun 14, 2017

    @jnothman
    Member

    So it seems the cosine bug has been fixed, but correlation is open.

  18. haiatn commented on Sep 21, 2020

    @haiatn
    Contributor

    I think we should close this issue. As I understand this metric does not work if one of the vectors has zero variance, returning Nan seems to be the chosen behaviour for this metric and no solution seems to be better. We could add a different assertion but I am not sure if we should.

  19. dkobak commented on Mar 9, 2021

    @dkobak
    Contributor

    I agree with @haiatn -- I think this is nothing to do here. If there is a row with std=0, then correlation distances are undefined. They are definitely not zero! They are undefined. So one cannot proceed with t-SNE. That's correct behaviour, not a bug. @amueller

  20. added
    Needs Decision
    and removed
    Easy
    on Dec 7, 2021
  21. thomasjpfan commented on Apr 22, 2022

    @thomasjpfan
    Member

    I agree with the comments above and this issue can be closed. On main, the snippet from the opening comment now errors with:

    AssertionError: All probabilities should be finite

    which is more informative about there being non-finite values, such as nans.

    Edit: I am leaving this open for now, to see what other maintainers think.

  22. added
    Needs Decision - Close
    on Apr 22, 2022
  23. haiatn commented on May 5, 2022

    @haiatn
    Contributor

    This looks good imo

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 DecisionNeeds Decision - Closemodule:manifold

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions