从GridSearch CV中检索模型结果以计算Shapley值

我已经使用GridSearchCV调整了一个模型。现在我想计算Shapley值并可视化它们。困难在于shap包需要一个模型,而不是GridSearch的结果。同样,当我传递best_estimator_属性时,它也不喜欢。它说模型不被支持。我如何从GridSearchCV或其他东西中获取Shapley值来计算Shapley值。我的一个列是分类列,因此需要预处理。由于我从网格搜索中获得了最佳参数,我可以将模型作为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 shapshap_values = shap.TreeExplainer(optimize_hparams.best_estimator_['model']).shap_values(X)shap.summary_plot(shap_values, X, plot_type="bar")

回答:

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注