fit_transform 后数组大小不同

Different size of array after fit_transform

我对 fit_transform 功能有疑问。有人可以解释为什么数组的大小不同吗?

In [5]: X.shape, test.shape

Out[5]: ((1000, 1932), (1000, 1932))

In [6]: from sklearn.feature_selection import VarianceThreshold
        sel = VarianceThreshold(threshold=(.8 * (1 - .8)))
        features = sel.fit_transform(X)
        features_test = sel.fit_transform(test)

In [7]: features.shape, features_test.shape

Out[7]:((1000, 1663), (1000, 1665))

UPD:哪个转换可以帮助我得到相同大小的数组?

因为fit_transform 对数组应用降维。这就是结果数组维度与输入不同的原因。

看到这个what is the difference between 'transform' and 'fit_transform' in sklearn and this http://scikit-learn.org/stable/modules/feature_extraction.html

这是因为您两次拟合您的选择器

首先,请注意 fit_transform 只是对 fit 的调用,然后是对 transform.

的调用

fit 方法允许您的 VarianceThreshold 选择器根据您提供的参数找到它想要保留在数据集中的特征。

transform 方法执行实际的特征选择和 returns 仅包含所选特征的 n 数组。