Repository navigation
Closed as not planned
Closed as not planned
Copy link
Labels
BugNeeds DecisionRequires decisionNeeds Decision - CloseRequires decision for closingmodule:manifold
Description
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
Metadata
Metadata
Assignees
Labels
BugNeeds DecisionRequires decisionNeeds Decision - CloseRequires decision for closingmodule:manifold
Activity
amueller commentedon Mar 31, 2015
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.
scikit-learn#4475 : Add a safe_pairwise_distances function, dealing w…
scikit-learn#4475: Fix directly in pairwise_distances, as suggested
littmus commentedon Aug 25, 2015
It also happens when using cosine metric.
amueller commentedon Aug 25, 2015
@littmus can you give a small example?
giorgiop commentedon Sep 30, 2015
Same as above, with cosine
In the case of
cosinethe problem is with negative values instead ofNaN:ogrisel commentedon Sep 30, 2015
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.giorgiop commentedon Oct 1, 2015
Returning
Nanseems 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.
giorgiop commentedon Oct 1, 2015
I have tried all the
sklearn.metric.pairwise._VALID_METRICS, butwminkowskiwhich is not compatible with t-sne. They include also scipy's ones. The only issues are withcorrelation,yule,diceandsokalsneathwithNanandcosinewith 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.ptpas proposed in scipy/#3728. However, clipping values to zero was already done foreuclidean_distancesand we should have a consistent solution.amueller commentedon Oct 13, 2015
@giorgiop there is no missing values (in scikit-learn, apart from the preprocessing module)
amueller commentedon Oct 13, 2015
Clipping to zero sounds good. For correlation, I'd say we should set the distance between constant points to zero.
amueller commentedon Oct 13, 2015
as post-processing?
10 remaining items
asanakoy commentedon Oct 24, 2016
Pool request fixing negative values: #7732.
jnothman commentedon Jun 14, 2017
So it seems the cosine bug has been fixed, but correlation is open.
haiatn commentedon Sep 21, 2020
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.
dkobak commentedon Mar 9, 2021
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
thomasjpfan commentedon Apr 22, 2022
I agree with the comments above and this issue can be closed. On
main, the snippet from the opening comment now errors with: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.
haiatn commentedon May 5, 2022
This looks good imo