我在使用OneHotEncoder仅编码分类列而忽略连续列时遇到了问题。无论我在categorical_features中指定什么,编码器都会对所有列进行编码。例如:
enc = preprocessing.OneHotEncoder()
enc.fit([[0, 40, 3], [1, 50, 0], [0, 45, 1], [1, 30, 2]])
OneHotEncoder(categorical_features=[0,2],
handle_unknown='error', n_values='auto', sparse=True)
print enc.n_values_
print enc.feature_indices_
enc.transform([[0, 45, 3]]).toarray()我只想对第1列和第3列进行编码,保留中间一列(值40、50、45、30)为连续值。所以我指定了categorical_features=0,2,但是不管我做什么,这段代码的输出仍然是:
[ 2 51 4]
[ 0 2 53 57]
Out[129]:
array([[ 1., 0., 0., 0., 1., 0., 0., 0., 0., 1.]])发布于 2016-04-17 20:40:05
为什么你称OneHotEncoder构造函数为twise?enc是由默认构造函数创建的,所以对于enc,你可以使用categorical_features='all' (所有特性都是分类的)。据我所知,你需要这样的东西:
enc = OneHotEncoder(categorical_features=[0,2],
handle_unknown='error', n_values='auto', sparse=True)
enc.fit([[0, 40, 3], [1, 50, 0], [0, 45, 1], [1, 30, 2]])
print(enc.n_values_)
print(enc.feature_indices_)
enc.transform([[0, 45, 3]]).toarray()你将会有
[2 4]
[0 2 6]
Out[23]: array([[ 1., 0., 0., 0., 0., 1., 45.]])https://stackoverflow.com/questions/36667539
复制相似问题