OneHotEncoder的特征名称

58

我正在使用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()所做的那样。
3个回答

75

可以将带有原始列名的列表传递给get_feature_names

>>> encoder.get_feature_names(['Sex', 'AgeGroup'])

array(['Sex_female', 'Sex_male', 'AgeGroup_0', 'AgeGroup_15',
       'AgeGroup_30', 'AgeGroup_45', 'AgeGroup_60', 'AgeGroup_75'],
      dtype=object)
>>> encoder.get_feature_names_out(['Sex', 'AgeGroup'])

array(['Sex_female', 'Sex_male', 'AgeGroup_0', 'AgeGroup_15',
       'AgeGroup_30', 'AgeGroup_45', 'AgeGroup_60', 'AgeGroup_75'],
      dtype=object)

22
  • type(train_X_encoded)scipy.sparse.csr.csr_matrix
# pandas.DataFrame.sparse.from_spmatrix will load this sparse matrix
>>> print(train_X_encoded)

  (0, 1)    1.0
  (0, 2)    1.0
  (1, 0)    1.0
  (1, 3)    1.0
  (2, 1)    1.0
  (2, 4)    1.0
  (3, 0)    1.0
  (3, 5)    1.0
  (4, 1)    1.0
  (4, 6)    1.0
  (5, 0)    1.0
  (5, 7)    1.0

# pandas.DataFrame will load this dense matrix
>>> print(train_X_encoded.todense())

[[0. 1. 1. 0. 0. 0. 0. 0.]
 [1. 0. 0. 1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 1. 0. 0. 0.]
 [1. 0. 0. 0. 0. 1. 0. 0.]
 [0. 1. 0. 0. 0. 0. 1. 0.]
 [1. 0. 0. 0. 0. 0. 0. 1.]]
import pandas as pd

column_name = encoder.get_feature_names_out(['Sex', 'AgeGroup'])
one_hot_encoded_frame = pd.DataFrame.sparse.from_spmatrix(train_X_encoded, columns=column_name)

# display(one_hot_encoded_frame)
   Sex_female  Sex_male  AgeGroup_0  AgeGroup_15  AgeGroup_30  AgeGroup_45  AgeGroup_60  AgeGroup_75
0         0.0       1.0         1.0          0.0          0.0          0.0          0.0          0.0
1         1.0       0.0         0.0          1.0          0.0          0.0          0.0          0.0
2         0.0       1.0         0.0          0.0          1.0          0.0          0.0          0.0
3         1.0       0.0         0.0          0.0          0.0          1.0          0.0          0.0
4         0.0       1.0         0.0          0.0          0.0          0.0          1.0          0.0
5         1.0       0.0         0.0          0.0          0.0          0.0          0.0          1.0
scikit-learn v1.0 开始使用 get_feature_names_out 代替 get_feature_names

3
感谢提供一个好的解决方案。@Nursnaaz 需要将稀疏矩阵转换为密集矩阵。
column_name = encoder.get_feature_names(['Sex', 'AgeGroup'])
one_hot_encoded_frame =  pd.DataFrame(train_X_encoded.todense(), columns= column_name)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接