了解 onehotencoder 的工作原理 - 为什么我在 ohe 列中得到多个?

understanding how onehotencoder works - why do i get mutliple ones in ohe column?

我正在使用 sklearn 管道执行单热编码:

preprocess = make_column_transformer(
    (MinMaxScaler(),numeric_cols),
    (OneHotEncoder(),['country'])
    )

param_grid =    { 
                  'xgbclassifier__learning_rate': [0.01,0.005,0.001],
                 
                  }

model = make_pipeline(preprocess,XGBClassifier())

# Initialize Grid Search Modelg
model = GridSearchCV(model,param_grid = param_grid,scoring = 'roc_auc',
                                 verbose= 1,iid= True,
                                     refit = True,cv  = 3)
model.fit(X_train,y_train)

为了了解这些国家是如何进行热编码的,我得到以下信息(我知道有两个)

pd.DataFrame(preprocess.fit_transform(X_test))

结果是:

几个问题:

为了帮助您更好地理解 (1),即 OHE 的工作原理。

假设您有 1 列包含分类数据:

df = pd.DataFrame({"categorical": ["a","b","a"]})
print(df)
  categorical
0           a
1           b
2           a

然后你会得到一个 1 每行(这对于一列分类数据总是正确的),但不一定是在每列的基础上:

from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder()
ohe.fit(df)
ohe_out = ohe.transform(df).todense()
# ohe_df = pd.DataFrame(ohe_out, columns=ohe.get_feature_names(df.columns))
ohe_df = pd.DataFrame(ohe_out, columns=ohe.get_feature_names(["categorical"]))
print(ohe_df)
   categorical_a  categorical_b
0            1.0            0.0
1            0.0            1.0
2            1.0            0.0

您是否应该添加更多数据列,例如一个数字列,这将适用于每列,但不再适用于整行:

df = pd.DataFrame({"categorical":["a","b","a"],"nums":[0,1,0]})
print(df)
  categorical  nums
0           a     0
1           b     1
2           a     0

ohe.fit(df)
ohe_out = ohe.transform(df).todense()
# ohe_df = pd.DataFrame(ohe_out, columns=ohe.get_feature_names(df.columns))
ohe_df = pd.DataFrame(ohe_out, columns=ohe.get_feature_names(["categorical","nums"]))
print(ohe_df)
   categorical_a  categorical_b  nums_0  nums_1
0            1.0            0.0     1.0     0.0
1            0.0            1.0     0.0     1.0
2            1.0            0.0     1.0     0.0