查看OneHotEncoder
的文档,似乎没有一种方法可以将要素名称包含为OneHot向量的前缀。有谁知道解决这个问题的方法吗?我想念什么吗?
示例数据框:
OneHotEncoder
给编码器一个数据帧,我希望有可能获得类似的东西:
df = pd.DataFrame({'a':['c1', 'c1', 'c2', 'c1', 'c3'], 'b':['c1', 'c4', 'c1', 'c1', 'c1']})
from sklearn.preprocessing import OneHotEncoder
onehot = OneHotEncoder()
onehot.fit(df)
onehot.get_feature_names()
array(['x0_c1', 'x0_c2', 'x0_c3', 'x1_c1', 'x1_c4'], dtype=object)
这里是您需要包含array(['a_c1', 'a_c2', 'a_c3', 'b_c1', 'b_c4'], dtype=object)
中的功能名称的步骤。
get_feature_name
输出:
get_feature_name
每个文档:
get_feature_name(自己,input_features =无)返回输出要素的要素名称。
参数:input_features:字符串列表,长度n_features,输入功能的可选字符串名称(如果有)。默认,使用“ x0”,“ x1”,……“ xn_features”。
返回:output_feature_names:字符串数组,长度n_output_features
使用onehot.get_feature_names(input_features=df.columns)
,您可以设置前缀和分隔符,然后设置原始列名。
array(['a_c1', 'a_c2', 'a_c3', 'b_c1', 'b_c4'], dtype=object)