Comments (27)
@ivan-marroquin That's right. You can also pass show=False if you want to keep manipulating the matplotlib plot. I also just added a shap.save_html(file, output_of_force_plot)
function since it does seem useful.
from shap.
@coulls any luck with outputting to a file? This is an excellent package and I am running into the same issue as you i.e. how to use it in a context where ipython cannot be used
from shap.
@michael1albq I had the same problem, had to make the following change to force.py
(just added encoding="utf-8"
to the open
command for the out_file
). I thought maybe it was just a problem with my install, so I didn't open an issue, but maybe it's worth seeing if it's a general problem?@slundberg
I included a large part of the save_html
command below for context.
def save_html(out_file, plot_html):
""" Save html plots to an output file.
"""
internal_open = False
if type(out_file) == str:
out_file = open(out_file, "w", encoding="utf-8") # <--- change here
# out_file = open(out_file, "w")
internal_open = True
out_file.write("<html><head><script>\n")
# dump the js code
bundle_path = os.path.join(os.path.split(__file__)[0], "resources", "bundle.js")
with io.open(bundle_path, encoding="utf-8") as f:
bundle_data = f.read()
out_file.write(bundle_data)
out_file.write("</script></head><body>\n")
out_file.write(plot_html.data)
out_file.write("</body></html>\n")
from shap.
That's tricky, but might not be too bad. The right way to do it is to save an SVG file with SVG tags that get displayed by React. Perhaps there is a way in iPython to get the html contents of the output cell?
When I needed to do it myself I either took a screen capture or for paper-quality figures manually snipped out the SVG tags from the document tree in the browser.
from shap.
I wonder how to export to csv the values used for the dot plot (default) and bar plot (global).
For the moment I can get the raw data for the SHAP values.
df_shap_values = pd.DataFrame(shap_values, index=X.index, columns=X.columns)
df_shap_values.to_csv('df_shap_values.csv')
But this of course lacks any transformation, normalization or cleaning that these plots are doing.
Is there any way to export these?
Is all the code behind here https://github.com/slundberg/shap/blob/master/shap/plots/_summary.py?
from shap.
Enabling saving of shap values as a CSV would be great feature to have.
from shap.
I guess I should have been a bit more specific. I'm looking to use these visualizations at a much larger scale than would be supported in a Jupyter notebook -- something that requires running a script through thousands of samples at the command line.
That pretty much eliminates anything that uses iPython's rendering engine.
from shap.
This worked!
from shap.
#668 fixes this :)
from shap.
from shap.
That is what I figured after looking through the code. The best I could expect without a huge amount of effort is to essentially make a custom fork of IML that saves the HTML to a file instead of using the iPython display. Thanks for the note!
from shap.
Hi @coulls ,
I was wondering if you managed to find the way to generate outputs from command line.
Ivan
from shap.
Hey everyone!
When I first asked this question, I did some digging into the interpretable ML package (https://github.com/interpretable-ml/iml) that these visualizations are based on. Unfortunately, when I looked at it, there was some pretty heavy reliance on Javascript output directly into the Jupyter notebook, so there was no easy way to move it to another graphics backend.
If anyone happens to have a solution, please feel free to chime in!
from shap.
Hi all!
I found a similar application to shap. In this package, the images can be exported to html (including a notebook):
https://marcotcr.github.io/lime/tutorials/Lime%20-%20basic%20usage%2C%20two%20class%20case.html
May be this can help us?
Ivan
from shap.
Indeed, LIME was around even before SHAP. However, the approach is slightly different (parameterizations are different in some key ways). In my experience, SHAP has been more 'accurate' or in-line with human interpretation than LIME.
from shap.
I should note that that dependence_plot
and summary_plot
are already in matplotlib and so can be saved to a file. force_plot
is by default drawn using javascript, but someone has contributed a matplotlib version that can obtained by using the matplotlib=True
option. Hope that helps clarify the current state of things :)
from shap.
Thanks @coulls for your comments. but do you think that the approach used by LIME to generate an HTML file could be applied to SHAP?
from shap.
Thaks @slundberg for letting me know that it is possible to save dependence_plot() and summary_plot() to a file. Please let me know if I got this right. After the command dependence_plot(), can I use plt.savefig() to generate a graphic output?
Regarding LIME capability to generate an HTML file. From a DOS terminal, I can use the command "explainer.save_to_file(path + filename)" - I attached an example for your reference.
Many thanks,
Ivan
from shap.
Hi @slundberg thanks a lot for adding this functionality!
from shap.
Hi @slundberg ,
Following your advice, I used these commands:
s_plot= plt.figure()
shap.summary_plot(shap_values_1[0], features= X_test, title= "Toy")
s_plot.savefig('C:\Users\IMarroquin\Documents\SHAP_Model_1_55_summary.png')
The file got created. But the title is missing - and also in the displayed image. Do you have suggestions?
Then, I decided to do the following (since I have a multi-class classifier):
s_plot= plt.figure()
plt.subplot(311)
shap.summary_plot(shap_values_1[0], features= X_test, title= "Toy")
plt.title("Toy")
plt.subplot(312)
shap.summary_plot(shap_values_1[1], features= X_test, title= "Bike")
plt.title("Bike")
plt.subplot(313)
shap.summary_plot(shap_values_1[2], features= X_test, title= "Shoes")
plt.title("Shoes")
s_plot.savefig('C:\Users\IMarroquin\Documents\SHAP_Model_1_55_summary.png')
Failed with these messages:
raise ValueError("'box_aspect' and 'fig_aspect' must be positive")
ValueError: 'box_aspect' and 'fig_aspect' must be positive
Any ideas?
Many thanks,
Ivan
from shap.
Try using show=False
in summary_plot
to prevent it from displaying the figure before you save it :)
from shap.
Hi @slundberg ,
Thanks for the suggestion. Unfortunately, I still have the same issue. So, I decided to create individual plots.
Kind regards,
Ivan
from shap.
Ah. Well if you happen to find a solution at some point please share it back here!
from shap.
First off, great library and I very much appreciate the continually improving functionality.
I was able to get the output from the forceplots - both the initjs() and matplotlib variations, but got and error when I tried to use save_html() for both formats. Any idea as to what's going on here?
from shap.
@slundberg is there a release on PyPi with the fix available?
from shap.
@kjanko I'm planning to get a new release out this week that will have this
from shap.
Closing as save_html
addresses the original issue
from shap.
Related Issues (20)
- BUG: LightGBM with multiclass interaction TreeShap produces explainer error HOT 12
- ENH: Integrate Fasttreeshap speedup into SHAP HOT 2
- BUG: `base_score` attribute of the `XGBTreeModelLoader` is broken for all exponential losses (e.g. tweedie, poisson) HOT 4
- BUG: ERROR USING LLAMA-2 HOT 15
- BUG: 0.45.0 update breaks pytorch example on docs HOT 1
- x
- BUG: Error using Falcon for text-generation HOT 4
- BUG: Error when using DeepExplainer on LSTM Model HOT 1
- ENH: Partition Explainer for Video Models
- Does Feature/Column Order of dataset matter while calculating SHAP values? HOT 3
- When will the paddlepaddle framework be supported HOT 1
- BUG: LookupError: gradient registry has no entry for: shap_TensorListStack HOT 1
- BUG: shap summary plot for 3 group classification HOT 1
- ENH: Include directionality of feature association in beeswarm plot
- ENH: Support SeLU and activation function in Pytorch Deep Explainer
- BUG: tensorflow DeepExplainer SHAP explanations do not sum up to the model's output HOT 1
- Question: Using SHAP with GPT-4 via API HOT 1
- BUG: Warning: unrecognized nn.Module: Chomp1d HOT 2
- CI broken: mistralai Mistral-7B-v0.1 Tokenizer no longer accessible
- BUG: summary_plot ignores plot_type for TreeExplainer
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from shap.