sklearn.manifold.TSNE fit_transform实际上在空的numpy数组上返回一些东西

时间:2015-01-20 20:54:45

标签: python numpy scikit-learn

为什么tsne.fit_transform([[]])实际上会返回一些内容?

from sklearn.manifold import TSNE
import numpy

tsne = TSNE(n_components=2,
    early_exaggeration=4.0,
    learning_rate=1000.0,
    metric='euclidean',
    init='random',
    random_state=42)

# returns [[  4.96714153e-05  -1.38264301e-05]]
print tsne.fit_transform(numpy.array([[]]))

但将initrandom更改为pca会引发异常:ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (0,)

2 个答案:

答案 0 :(得分:2)

当您使用init='random'嵌入X_embedded gets initialized to None以及随后使用随机权重时,这是相关代码:

<强> scikit-learn/sklearn/manifold/t_sne.py

if X_embedded is None:
    # Initialize embedding randomly
    X_embedded = 1e-4 * random_state.randn(n_samples, self.n_components)

使用init='pca',嵌入通过PCA转换进行初始化:

if self.init == 'pca':
    pca = RandomizedPCA(n_components=self.n_components,
                            random_state=random_state)
    X_embedded = pca.fit_transform(X)

空数组失败。

答案 1 :(得分:0)

这是一个错误。它已在this commit中修复,并应在版本0.16.x之后包含。

您可以使用pip安装当前的sklearn示例版本:

(sudo) pip install scikit-learn

现在,sklearn会引发错误:

In [1]: from sklearn.manifold import TSNE

In [2]: TSNE().fit_transform([[]])
---------------------------------------------------------------------------
ValueError
Traceback (most recent call last)
<ipython-input-2-39cfca09a0bd> in <module>()
----> 1 TSNE().fit_transform([[]])

...

/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.pyc in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features)
    365             raise ValueError("Found array with %d feature(s) (shape=%s) while"
    366                              " a minimum of %d is required."
--> 367                              % (n_features, shape_repr, ensure_min_features))
    368     return array
    369 

ValueError: Found array with 0 feature(s) (shape=(1, 0)) while a minimum of 1 is required.