python - 形状绘图出现错误
问题描述
X = df.copy()
# Save and drop labels
y = df['class']
X = X.drop('class', axis=1)
cat_features = list(range(0, X.shape[1]))
model = CatBoostClassifier(iterations=2000, learning_rate=0.1, random_seed=12)
model.fit(X, y, verbose=False, plot=False)
explainer = shap.Explainer(model)
shap_values = explainer(X)
shap.force_plot(explainer.expected_value, shap_values[0:5,:],X.iloc[0:5,:], plot_cmap="DrDb")
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-170-ba1eca12b9ed> in <module>
----> 1 shap.force_plot(10, shap_values[0:5,:],X.iloc[0:5,:], plot_cmap="DrDb")
~\anaconda3\lib\site-packages\shap\plots\_force.py in force(base_value, shap_values, features, feature_names, out_names, link, plot_cmap, matplotlib, show, figsize, ordering_keys, ordering_keys_time_format, text_rotation, contribution_threshold)
101
102 if type(shap_values) != np.ndarray:
--> 103 return visualize(shap_values)
104
105 # convert from a DataFrame or other types
~\anaconda3\lib\site-packages\shap\plots\_force.py in visualize(e, plot_cmap, matplotlib, figsize, show, ordering_keys, ordering_keys_time_format, text_rotation, min_perc)
343 return AdditiveForceArrayVisualizer(e, plot_cmap=plot_cmap, ordering_keys=ordering_keys, ordering_keys_time_format=ordering_keys_time_format)
344 else:
--> 345 assert False, "visualize() can only display Explanation objects (or arrays of them)!"
346
347 class BaseVisualizer:
AssertionError: visualize() can only display Explanation objects (or arrays of them)!
试图用 shap 和我的数据进行绘图,但出错了,我实际上不明白为什么。没有发现任何关于这个的东西。请解释如何避免这个错误?
explainer.expected_value
-5.842052267820879
解决方案
您应该将最后一行更改为:shap.force_plot(explainer.expected_value, shap_values.values[0:5,:],X.iloc[0:5,:], plot_cmap="DrDb")
通过调用shap_values.values
而不是 just shap_values
,因为shap_values
拥有 shapley 值, thebase_values
和data
. 在检查变量之前,我遇到了同样的问题。
推荐阅读
- swift - self.presentationMode.wrappedValue.dismiss() 在 SwiftUI 中不起作用
- text-extraction - 使用PyPDF2抓取PDF,数据不按顺序抓取!这是这个库的限制吗?
- azure - 使用自定义 ui 使用 Azure 托管应用程序创建 azure 管理组时出错
- java - spring端点SOAP请求有效负载子元素在嵌套命名空间时在XSD中解析为pojo时获取空值
- split - Payumey 拆分支付集成
- r - 查找变量的最大值
- java - 如何在 maven ~/.m2/settings.xml 中设置 jdk 11
- django - Django Signals 如何允许未经身份验证的用户发表评论?
- windows - Powershell将域用户添加到文件的ntfs权限
- reactjs - 为什么我在 react-native 中使用 forwardRef 时会出错?