Scikit learn 了解onehotencoder的工作原理-为什么我会在ohe专栏中看到多个?

Scikit learn 了解onehotencoder的工作原理-为什么我会在ohe专栏中看到多个?,scikit-learn,pipeline,categorical-data,sklearn-pandas,one-hot-encoding,Scikit Learn,Pipeline,Categorical Data,Sklearn Pandas,One Hot Encoding,我正在使用sklearn管道执行一个热编码: preprocess = make_column_transformer( (MinMaxScaler(),numeric_cols), (OneHotEncoder(),['country']) ) param_grid = { 'xgbclassifier__learning_rate': [0.01,0.005,0.001],

我正在使用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)
为了了解这些国家是如何统一的,我得到了以下信息(我知道有两个)

其结果是:

有几个问题:

  • 现在纠正我,如果我错了,但在一个热编码中,我认为它是所有0的一系列,只有一个数字1。为什么我在一列中得到几个
  • 当我做model.predict(x_测试)时,它应用piepline fom培训中定义的Trasnformation
  • 调用fit_transform时,如何检索要素名称

    • 帮助您更好地理解(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
      


      (1) 每行一个
      1
      (2)是(3)您需要访问
      OHE
      transformer并检查
      词汇表
      属性在文档中我找不到它(词汇表属性?)它是
      类别
      适合
      OHE
      的属性。很抱歉误导了你。这些文档还可以,所以这是在我调用fit_transform之后得到的特性。很抱歉,我不明白你在(1)中的意思,你真的只需要每个向量一个1就可以分类吗?因为在你刚才共享的文档中,如果你滚动到底部,ohe向量有多个1要添加,我是说当我调用fit_transform(X_测试)时我如何获得所有功能的名称?我明白了,谢谢,这样您可以在每列中获得1个以上的功能名称
      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