0

这就是我所做的。我认为 One hot 编码器出了点问题。

from sklearn.datasets import make_classification
from sklearn.feature_selection import RFE


X, y = make_classification(n_samples=50, n_features=10, random_state=10)
encoder = preprocessing.LabelEncoder()
encoder.fit(X)
X = encoder.transform(X)
print X
print X.shape

encoder = preprocessing.OneHotEncoder()
encoder.fit(X)
X = encoder.transform(X)


print encoder.feature_indices_

estimator = SVR(kernel="linear")
selector = RFE(estimator, 100, step=1)
selector = selector.fit(X, y)

在 XI 上使用标签编码器后,得到了一个形状为 (50, 10) 的数组(这很明显)。但是在做一次热编码之后,我得到的特征指标如下。

[   0  499  987 1487 1968 2459 2957 3401 3886 4379 4868]

据我所知,两个索引之间的最大范围应该小于或等于行数,不是吗?是 50。但是在这里我得到的是 500 而不是 50。我对 One hot 编码是否弄错了,或者 One hot encoding 功能还有其他问题吗?

(此示例仅用于演示我的问题)

4

1 回答 1

1

行数无关紧要,而是任何给定列(即特征)内各行的值范围。当您打印 X 时,您将看到您在任何给定列中的值可能跨越 1:500 的整个范围。

于 2013-07-10T19:53:34.780 回答