sklearn.manifold.TSNE fit_transform 实际上 return 空 numpy 数组上的东西

sklearn.manifold.TSNE fit_transform actually return something on empty numpy array

为什么 tsne.fit_transform([[]]) 实际上 returns 有什么?

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,)

当你使用 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)

空数组失败。

这是一个错误。它已在 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.