如何访问 GridSearchCV 中的 ColumnTransformer 元素

How to access ColumnTransformer elements in GridSearchCV

在 grid_search 的 grid_search 中引用包含在 ColumnTransformer(它是管道的一部分)中的单个预处理器时,我想找出正确的命名约定。

环境&示例数据:

import seaborn as sns
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, KBinsDiscretizer, MinMaxScaler
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression

df = sns.load_dataset('titanic')[['survived', 'age', 'embarked']]
X_train, X_test, y_train, y_test = train_test_split(df.drop(columns='survived'), df['survived'], test_size=0.2, 
                                                    random_state=123)

管道:

num = ['age']
cat = ['embarked']

num_transformer = Pipeline(steps=[('imputer', SimpleImputer()), 
                                  ('discritiser', KBinsDiscretizer(encode='ordinal', strategy='uniform')),
                                  ('scaler', MinMaxScaler())])

cat_transformer = Pipeline(steps=[('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
                                  ('onehot', OneHotEncoder(handle_unknown='ignore'))])

preprocessor = ColumnTransformer(transformers=[('num', num_transformer, num),
                                               ('cat', cat_transformer, cat)])

pipe = Pipeline(steps=[('preprocessor', preprocessor),
                       ('classiffier', LogisticRegression(random_state=1, max_iter=10000))])

param_grid = dict([SOMETHING]imputer__strategy = ['mean', 'median'],
                  [SOMETHING]discritiser__nbins = range(5,10),
                  classiffier__C = [0.1, 10, 100],
                  classiffier__solver = ['liblinear', 'saga'])
grid_search = GridSearchCV(pipe, param_grid=param_grid, cv=10)
grid_search.fit(X_train, y_train)

基本上,我应该在代码中写什么来代替 [SOMETHING]?

我查看了 ,它回答了 make_pipeline 的问题 - 所以使用类似的想法,我尝试了 'preprocessor__num__'、'preprocessor__num_'、'pipeline__num__' , 'pipeline__num_' - 目前运气不佳。

谢谢

你很接近,正确的声明方式是这样的:

param_grid = {'preprocessor__num__imputer__strategy' : ['mean', 'median'],
              'preprocessor__num__discritiser__n_bins' : range(5,10),
              'classiffier__C' : [0.1, 10, 100],
              'classiffier__solver' : ['liblinear', 'saga']}

完整代码如下:

import seaborn as sns
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, KBinsDiscretizer, MinMaxScaler
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression

df = sns.load_dataset('titanic')[['survived', 'age', 'embarked']]
X_train, X_test, y_train, y_test = train_test_split(df.drop(columns='survived'), df['survived'], test_size=0.2, 
                                                    random_state=123)
num = ['age']
cat = ['embarked']

num_transformer = Pipeline(steps=[('imputer', SimpleImputer()), 
                                  ('discritiser', KBinsDiscretizer(encode='ordinal', strategy='uniform')),
                                  ('scaler', MinMaxScaler())])

cat_transformer = Pipeline(steps=[('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
                                  ('onehot', OneHotEncoder(handle_unknown='ignore'))])

preprocessor = ColumnTransformer(transformers=[('num', num_transformer, num),
                                               ('cat', cat_transformer, cat)])

pipe = Pipeline(steps=[('preprocessor', preprocessor),
                       ('classiffier', LogisticRegression(random_state=1, max_iter=10000))])

param_grid = {'preprocessor__num__imputer__strategy' : ['mean', 'median'],
              'preprocessor__num__discritiser__n_bins' : range(5,10),
              'classiffier__C' : [0.1, 10, 100],
              'classiffier__solver' : ['liblinear', 'saga']}
grid_search = GridSearchCV(pipe, param_grid=param_grid, cv=10)
grid_search.fit(X_train, y_train)

检查可用参数名称的一种简单方法如下:

print(pipe.get_params().keys())

这将打印出所有可用参数的列表,您可以将其直接复制到 params 字典中。

我写了一个实用函数,你可以用它来检查一个参数是否存在于 pipeline/classifier 中,只需传入一个关键字。

def check_params_exist(esitmator, params_keyword):
    all_params = esitmator.get_params().keys()
    available_params = [x for x in all_params if params_keyword in x]
    if len(available_params)==0:
        return "No matching params found!"
    else:
        return available_params

现在如果您不确定确切的名称,只需将 imputer 作为关键字传递

print(check_params_exist(pipe, 'imputer'))

这将打印以下列表:

['preprocessor__num__imputer',
 'preprocessor__num__imputer__add_indicator',
 'preprocessor__num__imputer__copy',
 'preprocessor__num__imputer__fill_value',
 'preprocessor__num__imputer__missing_values',
 'preprocessor__num__imputer__strategy',
 'preprocessor__num__imputer__verbose',
 'preprocessor__cat__imputer',
 'preprocessor__cat__imputer__add_indicator',
 'preprocessor__cat__imputer__copy',
 'preprocessor__cat__imputer__fill_value',
 'preprocessor__cat__imputer__missing_values',
 'preprocessor__cat__imputer__strategy',
 'preprocessor__cat__imputer__verbose']