我目前在处理一个分类问题,并想创建特征重要性的可视化。我使用Python的XGBoost软件包,它已经提供了特征重要性图。但是,我发现shap (https://github.com/slundberg/shap)是一个基于树分类器的Python库,可以创建非常好的特征重要性图形。所有的东西都运行良好,我也可以将创建的图形保存为PNG,但是,如果我尝试将其保存为PDF或SVG,则会发生异常。这是我的操作:
首先,我训练XGBoost模型,并得到一个名为bst
的模型。
train = remove_labels_for_binary_df(dataset_fc_baseline_1[0].train)
test = remove_labels_for_binary_df(dataset_fc_baseline_1[0].test)
results, bst = xgboost_with_bst(*transform_feat_to_num(train, test))
然后我创建SHAP值,使用它们创建一个摘要图并保存可视化结果。如果我将图像另存为plt.savefig('shap.png')
,一切都正常运作。
import shap
import matplotlib.pyplot as plt
shap.initjs()
explainer = shap.TreeExplainer(bst)
shap_values = explainer.shap_values(train)
fig = shap.summary_plot(shap_values, train, show=False)
plt.savefig('shap.png')
不过,我需要PDF或SVG图形而非PNG格式,因此尝试使用plt.savefig('shap.pdf')
进行保存。通常情况下这样做是有效的,但对于shap图却会产生以下异常。
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-39-49d17973f438> in <module>()
1 fig = shap.summary_plot(shap_values, train, show=False)
----> 2 plt.savefig('shap.pdf')
C:\Users\Studio\Anaconda3\lib\site-packages\matplotlib\pyplot.py in
savefig(*args, **kwargs)
708 def savefig(*args, **kwargs):
709 fig = gcf()
--> 710 res = fig.savefig(*args, **kwargs)
711 fig.canvas.draw_idle() # need this if 'transparent=True' to reset
colors
712 return res
C:\Users\Studio\Anaconda3\lib\site-packages\matplotlib\figure.py in
savefig(self, fname, **kwargs)
2033 self.set_frameon(frameon)
2034
-> 2035 self.canvas.print_figure(fname, **kwargs)
2036
2037 if frameon:
C:\Users\Studio\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in
print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format,
**kwargs)
2261 orientation=orientation,
2262 bbox_inches_restore=_bbox_inches_restore,
-> 2263 **kwargs)
2264 finally:
2265 if bbox_inches and restore_bbox:
C:\Users\Studio\Anaconda3\lib\site-
packages\matplotlib\backends\backend_pdf.py in print_pdf(self, filename,
**kwargs)
2584 RendererPdf(file, image_dpi, height, width),
2585 bbox_inches_restore=_bbox_inches_restore)
-> 2586 self.figure.draw(renderer)
2587 renderer.finalize()
2588 if not isinstance(filename, PdfPages):
C:\Users\Studio\Anaconda3\lib\site-packages\matplotlib\artist.py in
draw_wrapper(artist, renderer, *args, **kwargs)
53 renderer.start_filter()
54
---> 55 return draw(artist, renderer, *args, **kwargs)
56 finally:
57 if artist.get_agg_filter() is not None:
C:\Users\Studio\Anaconda3\lib\site-packages\matplotlib\figure.py in
draw(self, renderer)
1473
1474 mimage._draw_list_compositing_images(
-> 1475 renderer, self, artists, self.suppressComposite)
1476
1477 renderer.close_group('figure')
C:\Users\Studio\Anaconda3\lib\site-packages\matplotlib\image.py in
_draw_list_compositing_images(renderer, parent, artists, suppress_composite)
139 if not_composite or not has_images:
140 for a in artists:
--> 141 a.draw(renderer)
142 else:
143 # Composite any adjacent images together
C:\Users\Studio\Anaconda3\lib\site-packages\matplotlib\artist.py in
draw_wrapper(artist, renderer, *args, **kwargs)
53 renderer.start_filter()
54
---> 55 return draw(artist, renderer, *args, **kwargs)
56 finally:
57 if artist.get_agg_filter() is not None:
C:\Users\Studio\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in
draw(self, renderer, inframe)
2605 renderer.stop_rasterizing()
2606
-> 2607 mimage._draw_list_compositing_images(renderer, self,
artists)
2608
2609 renderer.close_group('axes')
C:\Users\Studio\Anaconda3\lib\site-packages\matplotlib\image.py in
_draw_list_compositing_images(renderer, parent, artists, suppress_composite)
139 if not_composite or not has_images:
140 for a in artists:
--> 141 a.draw(renderer)
142 else:
143 # Composite any adjacent images together
C:\Users\Studio\Anaconda3\lib\site-packages\matplotlib\artist.py in
draw_wrapper(artist, renderer, *args, **kwargs)
58 renderer.stop_filter(artist.get_agg_filter())
59 if artist.get_rasterized():
---> 60 renderer.stop_rasterizing()
61
62 draw_wrapper._supports_rasterization = True
C:\Users\Studio\Anaconda3\lib\site-
packages\matplotlib\backends\backend_mixed.py in stop_rasterizing(self)
128
129 height = self._height * self.dpi
--> 130 buffer, bounds =
self._raster_renderer.tostring_rgba_minimized()
131 l, b, w, h = bounds
132 if w > 0 and h > 0:
C:\Users\Studio\Anaconda3\lib\site-
packages\matplotlib\backends\backend_agg.py in tostring_rgba_minimized(self)
138 [extents[0] + extents[2], self.height - extents[1]]]
139 region = self.copy_from_bbox(bbox)
--> 140 return np.array(region), extents
141
142 def draw_path(self, gc, path, transform, rgbFace=None):
ValueError: negative dimensions are not allowed
你有没有什么办法来解决这个问题?