为什么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([[]]))
但是将init
从random
更改为pca
会引发例外:ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (0,)
。
init='random'
嵌入X_embedded
初始化为 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)
对于空数组,此操作将失败。
错误。它已在此提交中修复,应从版本 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.