question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

T-SNE fails for CSR matrix

See original GitHub issue

T-SNE fails for CSR matrix with:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

Code to reproduce:

from sklearn.neighbors import BallTree, kneighbors_graph
from sklearn.manifold import TSNE
X = np.random.randn(100, 10)

bt = BallTree(X, leaf_size=300)
distances = kneighbors_graph(bt, n_neighbors=40, mode="distance", metric="cosine")
X_embedded = TSNE(n_components=2, metric="precomputed").fit_transform(distances)

Reason: When distance is square Compressed Sparse Row matrix then np.any(X > 0) is also sparse matrix.

<ipython-input-55-71728b7132f2> in <module>() ----> 1 X_embedded = TSNE(n_components=2, metric=“precomputed”).fit_transform(distances) 2 3 ax = plt.scatter(X_embedded[:,0], X_embedded[:,1], c=clusters[0:len(X_embedded)]).axes

/Users/roman/.virtualenvs/wordmap/lib/python2.7/site-packages/sklearn/manifold/t_sne.pyc in fit_transform(self, X, y) 857 Embedding of the training data in low-dimensional space. 858 “”" –> 859 embedding = self.fit(X) 860 self.embedding = embedding 861 return self.embedding_

/Users/roman/.virtualenvs/wordmap/lib/python2.7/site-packages/sklearn/manifold/t_sne.pyc in _fit(self, X, skip_num_points) 645 if X.shape[0] != X.shape[1]: 646 raise ValueError(“X should be a square distance matrix”) –> 647 if np.any(X < 0): 648 raise ValueError("All distances should be positive, the " 649 "precomputed distances given as X is not "

/Users/roman/.virtualenvs/wordmap/lib/python2.7/site-packages/scipy/sparse/base.pyc in bool(self) 236 return self.nnz != 0 237 else: –> 238 raise ValueError("The truth value of an array with more than one " 239 “element is ambiguous. Use a.any() or a.all().”) 240 nonzero = bool

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:20 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
rthcommented, Sep 5, 2017

Aww makes sense. I can reproduce this, thanks for the code snippet! As you say the root cause is that np.any(distance > 0) evaluates to a sparse array instead of a boolean. Apparently, it’s a known issue with sparse CSR, not that I was aware it could do that…

This line was recently introduced in https://github.com/scikit-learn/scikit-learn/pull/9032 but question is why the test sklearn/manifold/tests/test_t_sne.py::test_fit_csr_matrix doesn’t fail.

ping @tomMoral @jnothman sounds like a regression in 0.19…

0reactions
TomDLTcommented, Sep 18, 2019

Fixed in #10482

Read more comments on GitHub >

github_iconTop Results From Across the Web

sklearn tsne with sparse matrix - python - Stack Overflow
I'm trying to display tsne on a very sparse matrix with precomputed distances values but I'm having trouble with it. It boils down...
Read more >
t-SNE of a 99% sparse data set - Cross Validated
I have a sparse matrix of representing 12 cancer types. It's a very sparse with about 99% of elements are zeros. I have...
Read more >
sklearn.manifold.TSNE — scikit-learn 1.2.0 documentation
TSNE : Comparison of Manifold Learning methods Comparison of Manifold ... If the method is 'exact', X may be a sparse matrix of...
Read more >
Optimizing Data Locality and Termination Criterion for t-SNE
(b) Another problem is the lack of an effective stopping criterion in the literature. In this paper, we report an improved GPU implementation...
Read more >
Using T-SNE in Python to Visualize High-Dimensional Data Sets
The problem today is that most data sets have a large number of variables. In other words, they have a high number of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found