sklearn tsne with sparse matrix

谁说我不能喝 提交于 2020-01-17 07:06:06

问题


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



来源:https://stackoverflow.com/questions/42541788/sklearn-tsne-with-sparse-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!