我正在尝试在 Python 中绘制一些SHAP 图,以更深入地了解我的机器学习模型的输出。这是我在 for 循环中调用的方法:
def plotAndSaveSHAPSummary(model,train_data,x_train,pathToSHAPPlots):
shap_values = model.get_feature_importance(train_data, type='ShapValues')
expected_value = shap_values[0,-1]
shap_values = shap_values[:,:-1]
shap.summary_plot(shap_values,x_train,max_display=20,show=False)
plt.savefig(pathToSHAPPlots+'/SHAP Plots/SHAP_Plot'+str(counter)+'.png',dpi=300,bbox_inches='tight')
plt.clf()
绘图按预期保存到磁盘,但在每次调用 savefig 方法后,我收到以下错误消息:
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\PathTo\Anaconda\Lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "D:\PathTo\Anaconda\Lib\tkinter\__init__.py", line 749, in callit
func(*args)
File "D:\PathTo\Anaconda\lib\site-packages\matplotlib\backends\_backend_tk.py", line 270, in idle_draw
self.draw()
File "D:\PathTo\Anaconda\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
super(FigureCanvasTkAgg, self).draw()
File "D:\PathTo\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 393, in draw
self.figure.draw(self.renderer)
File "D:\PathTo\Anaconda\lib\site-packages\matplotlib\backend_bases.py", line 1535, in _draw
def _draw(renderer): raise Done(renderer)
matplotlib.backend_bases._get_renderer.<locals>.Done: <matplotlib.backends.backend_agg.RendererAgg object at 0x000002066B288288>
任何想法如何摆脱这个异常?