本文介绍了如何访问 GridSearchCV 中的 ColumnTransformer 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to find out the correct naming convention when referring to individual preprocessor included in ColumnTransformer (which is part of a pipeline) in param_grid for grid_search.

环境&示例数据:

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]?

Basically, what should I write instead of [SOMETHING] in my code?

我看过这个答案,它回答了make_pipeline 的问题 - 所以使用类似的想法,我尝试了 'preprocessor__num__', 'preprocessor__num_', 'pipeline__num__', 'pipeline__num_' - 到目前为止没有运气.

I have looked at this answer which answered the question for make_pipeline - so using the similar idea, I tried 'preprocessor__num__', 'preprocessor__num_', 'pipeline__num__', 'pipeline__num_' - no luck so far.

谢谢

推荐答案

你已经接近了,正确的声明方式是这样的:

You were close, the correct way to declare it is like this:

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)

检查可用参数名称的一种简单方法是这样的:

One simply way to check the available parameter names is like this:

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

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

This will print out the list of all the available parameters which you can copy directly into your params dictionary.

我编写了一个实用函数,您可以使用该函数通过简单地传入关键字来检查管道/分类器中是否存在参数.

I have written a utility function which you can use to check if a parameter exist in a pipeline/classifier by simply passing in a keyword.

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 作为关键字传递

Now if you are unsure of the exact name, just pass imputer as the keyword

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']

这篇关于如何访问 GridSearchCV 中的 ColumnTransformer 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 20:38