我正在使用OneHotEncoder对一些分类变量进行编码(例如 - 性别和年龄组)。编码器生成的特征名称类似于 - 'x0_female'、'x0_male'、'x1_0.0'、'x1_15.0' 等。
>>> train_X = pd.DataFrame({'Sex':['male', 'female']*3, 'AgeGroup':[0,15,30,45,60,75]})
>>> from sklearn.preprocessing import OneHotEncoder
>>> encoder = OneHotEncoder()
>>> train_X_encoded = encoder.fit_transform(train_X[['Sex', 'AgeGroup']])
>>> encoder.get_feature_names()
>>> array(['x0_female', 'x0_male', 'x1_0.0', 'x1_15.0', 'x1_30.0', 'x1_45.0',
'x1_60.0', 'x1_75.0'], dtype=object)
有没有办法告诉OneHotEncoder
以某种方式创建特征名称,使列名添加在开头,例如 - Sex_female,AgeGroup_15.0等,类似于Pandas的get_dummies()
所做的那样。