sklearn tsne with sparse matrix

2019-09-16 14:42发布

问题:

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 to this:

row = np.array([0, 2, 2, 0, 1, 2])
col = np.array([0, 0, 1, 2, 2, 2])
distances = np.array([.1, .2, .3, .4, .5, .6])
X = csc_matrix((distances, (row, col)), shape=(3, 3))
Y = TSNE(metric='precomputed').fit_transform(X)

However, I get this error:

TypeError: A sparse matrix was passed, but dense data is required for method="barnes_hut". Use X.toarray() to convert to a dense numpy array if the array is small enough for it to fit in memory. Otherwise consider dimensionality reduction techniques (e.g. TruncatedSVD)

I don't want to perform TruncatedSVD since I already computed distances.

If I change the method='exact', I get another error (which is somewhat questionable):

NotImplementedError: >= and <= don't work with 0.

NOTE: my distance matrix is about 100k x 100k with approximately 1M non zero values.

Any ideas?

回答1:

I think this should solve your problem:

X = csr_matrix((distances, (row, col)), shape=(3, 3)).todense()

If you really ment csr_matrix instead of csc_matrix