Python OneHotEncoder类别参数
使用sklearn 0.22,Category_features参数将被删除,因此以下代码不再可执行:Python OneHotEncoder类别参数,python,machine-learning,scikit-learn,preprocessor,one-hot-encoding,Python,Machine Learning,Scikit Learn,Preprocessor,One Hot Encoding,使用sklearn 0.22,Category_features参数将被删除,因此以下代码不再可执行: import numpy as np from sklearn.preprocessing import OneHotEncoder X = np.array([[1, 1], [2, 2], [1, 3]]) encoder = OneHotEncoder(categorical_features=[1], sparse=False) print(encoder.fit_transfor
import numpy as np
from sklearn.preprocessing import OneHotEncoder
X = np.array([[1, 1], [2, 2], [1, 3]])
encoder = OneHotEncoder(categorical_features=[1], sparse=False)
print(encoder.fit_transform(X))
问题是,我如何使用categories参数实现与上面代码中相同的行为,因为
OneHotEncoder(categories=[[1,2],[1,2,3]],sparse=False)
也会对第一列进行编码,OneHotEncoder(categories=[[1,2,3]],sparse=False)
抛出一个错误OK,因此,基本上您希望对第二列[1,2,3]进行热编码,并将第一列[1,2,1]保持为pass-through。在较新的sklearn版本中,您可以使用ColumnTransformer组合不同的预处理过程,如下所示:
import numpy as np
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
X = np.array([[1, 1], [2, 2], [1, 3]])
encoder = ColumnTransformer(
[('number1', OneHotEncoder(dtype='int'), [1])],
remainder="passthrough"
)
print(encoder.fit_transform(X))
然后,不必使用类别指定值范围。有关更多详细信息,请参阅文档