【问题标题】:Retrieve Model Results for Shapley values from GridSearch CV从 GridSearch CV 中检索 Shapley 值的模型结果
【发布时间】:2021-10-10 21:54:44
【问题描述】:

我已经使用 GridSearchCV 调整了一个模型。现在我想计算 Shapley 值并将它们可视化。困难在于shap 包不包括模型,而不是 GridSearch 结果。同样,当我将 best_estimator_ 属性传递给它时,它也不喜欢。它说不支持该模型。如何从 GridSearchCV 或其他东西中获取 Shapley 值来计算 Shapley 值。我的专栏之一是分类的,因此需要进行预处理。由于我有来自网格搜索的 best_params,我可以将模型作为 xgboost_regressor 模型运行,但在没有预处理的情况下这样做已经有一段时间了。

from xgboost import XGBRegressor as xgr    
model=xgr(booster ='gbtree', random_state = 13)
cv_inner = KFold(n_splits=5, shuffle=True)
params = {
        'model__n_estimators' : [1500,2000]
         ,'model__learning_rate' : [0.1,0.2,0.3]
         ,'model__gamma' : [0, 0.005,0.01]
         ,'model__lambda' : [0.1, 0.2,0.3]
         ,'model__alpha' : [0, 0.001, 0.05]
         ,'model__max_depth' : [6]
         ,'model__min_child_weight' : [1]
         ,'model__subsample' : [0.8]
    }
preprocessor = ColumnTransformer(
                    transformers=[
                        ('cat', OneHotEncoder(), [0])
                    ]
                    ,remainder = 'passthrough')
mymodel = Pipeline(steps = [
                        ('preprocessor',preprocessor),
                        ('model', model)
                        ])
optimize_hparams = GridSearchCV(
    estimator = mymodel, param_grid=params, n_jobs = -1,
    cv=cv_inner, scoring='neg_mean_absolute_error')
optimize_hparams.fit(X, y)
import shap
shap_values = shap.TreeExplainer(optimize_hparams.best_estimator_['model']).shap_values(X)
shap.summary_plot(shap_values, X, plot_type="bar")

【问题讨论】:

    标签: python xgboost grid-search shap


    【解决方案1】:

    在计算 Shap 值之前,您需要将网格搜索中的预处理器和最佳模型拟合到数据中,示例请参见下面的代码。

    import shap
    import numpy as np
    from sklearn.datasets import make_regression
    from sklearn.model_selection import KFold, GridSearchCV
    from sklearn.compose import ColumnTransformer
    from sklearn.preprocessing import OneHotEncoder
    from sklearn.pipeline import Pipeline
    from xgboost import XGBRegressor as xgr 
    
    # generate the features and target
    X, y = make_regression(n_samples=100, n_features=5, random_state=100)
    
    # add a categorical feature in the first column
    X = np.hstack([np.random.choice(['a', 'b', 'c'], size=(X.shape[0], 1)), X])
    
    # set up the grid search
    model = xgr(booster='gbtree', random_state=13)
    
    cv_inner = KFold(n_splits=5, shuffle=True)
    
    params = {
        'model__n_estimators' : [1500, 2000],
        'model__learning_rate' : [0.1, 0.2, 0.3],
        'model__gamma' : [0, 0.005, 0.01],
        'model__lambda' : [0.1, 0.2, 0.3],
        'model__alpha' : [0, 0.001, 0.05],
        'model__max_depth' : [6],
        'model__min_child_weight' : [1],
        'model__subsample' : [0.8],
    }
    
    preprocessor = ColumnTransformer(transformers=[('cat', OneHotEncoder(), [0])], remainder='passthrough')
    
    mymodel = Pipeline(steps=[('preprocessor', preprocessor), ('model', model)])
    
    optimize_hparams = GridSearchCV(
        estimator=mymodel, 
        param_grid=params, 
        cv=cv_inner, 
        scoring='neg_mean_absolute_error',
        n_jobs=-1,
    )
    
    # run the grid search
    optimize_hparams.fit(X, y)
    
    # fit the preprocessor 
    X_encoded = optimize_hparams.best_estimator_['preprocessor'].fit_transform(X)
    
    # fit the model 
    best_model = optimize_hparams.best_estimator_['model'].fit(X_encoded, y)
    
    # calculate the Shap values
    shap_values = shap.TreeExplainer(best_model).shap_values(X_encoded)
    
    # plot the Shap values
    shap.summary_plot(shap_values, X_encoded, plot_type='bar')
    

    【讨论】:

      猜你喜欢
      • 2019-08-26
      • 2016-03-15
      • 2012-11-25
      • 2019-12-26
      • 2012-07-01
      • 2016-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多