Giter VIP home page Giter VIP logo

Comments (24)

slundberg avatar slundberg commented on May 5, 2024 29

Just noting here that there is now a matplotlib=True option to force_plot that was kindly added by @jverre. There is more to be done on it, but it is pretty good and worth checking out for anyone who needs to save a plot or show a plot without JS.

from shap.

keithcallenberg avatar keithcallenberg commented on May 5, 2024 7

One extra motivation might be that the JS visualization won't currently work with JupyterLab. I guess a lab extension might be needed? But having a matplotlib-based alternative as a backup would be really helpful in the meantime.

from shap.

RamishaRaniK avatar RamishaRaniK commented on May 5, 2024 6

To save force plot, add this to force plot matplotlib= True, show= False. Even this working on spyder
'
def heart_disease_risk_factors(model, patient):
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(patient)
shap.initjs()

return shap.force_plot(explainer.expected_value[1], shap_values[1], 
 patient,matplotlib=True,show=False)

plt.clf()
data_for_prediction = X_test.iloc[2,:].astype(float)
heart_disease_risk_factors(model, data_for_prediction)
 plt.savefig("gg.png",dpi=150, bbox_inches='tight')`

from shap.

cbeauhilton avatar cbeauhilton commented on May 5, 2024 2

I was having trouble exporting the SVGs cleanly, and found a nice tool from NY Times' github that makes it easy for SVG novices like me. Thought it might be useful to others:
https://nytimes.github.io/svg-crowbar/svg-crowbar-2.js

from shap.

noreallyimfine avatar noreallyimfine commented on May 5, 2024 2

Hi!

Is there a way to use plotly.tools mpl_to_plotly to convert a force_plot to a plotly plot.

I'm hoping to have a reactive force plot in a plotly dash app.

from shap.

slundberg avatar slundberg commented on May 5, 2024 1

from shap.

slundberg avatar slundberg commented on May 5, 2024 1

from shap.

den-run-ai avatar den-run-ai commented on May 5, 2024 1

Maybe using altair instead of raw D3.js would help resolve this issue?

from shap.

harrytrinh2 avatar harrytrinh2 commented on May 5, 2024 1

I got this error for force_plot Exception: matplotlib = True is not yet supported for force plots with multiple samples!

from shap.

slundberg avatar slundberg commented on May 5, 2024

Glad you find it helpful!

The additive force diagram in the JS visualization code is pretty involved, so I don't have plans to port it to matplotlib. However, you could use a simple bar chart instead in matplotlib. If there ends up being more to it than just pyplot.bar() then it could be worth including in this package for convenience. You can also make a summary plot with only one feature for a quick result: shap.summary_plot(shap_values[0:1,:], X.iloc[0:1,:], color_bar=False)

If you really need to get that additive force diagram figure outside of a JS environment it would need to be saved as an SVG (either manually by copying the SVG code using the browser dev tools, or updating the package). Or of course just screen captured if you don't care about rasterization.

Hope those ideas help :)

from shap.

slundberg avatar slundberg commented on May 5, 2024

...I was thinking more about this and I don't think porting the additive force plot to matplotlib would be all that hard, it would just involve drawing a lot of custom polygons and text. I don't have a need that would justify me doing it at the moment, but if you want to give it a shot I would be happy to help.

from shap.

asstergi avatar asstergi commented on May 5, 2024

OK, if I find some time, I'll start working on that. I can't promise though! :)
Can you give me some hints on what the JS visualization code is doing? I mean, how does it select which features to plot? All of them or just a few? Anything else I need to know?

from shap.

slundberg avatar slundberg commented on May 5, 2024

That would fix the issue. Though it would essentially be a rewrite of the code, which would take some work. I haven't used it, but Altair seems like it could be a good fit.

from shap.

psinger avatar psinger commented on May 5, 2024

Is there any way to use the plotting functionality currently in jupyterlab?

from shap.

brylie avatar brylie commented on May 5, 2024

One approach might be to use Plot.ly Python bindings:
https://plot.ly/python/network-graphs/

from shap.

slundberg avatar slundberg commented on May 5, 2024

I think the React and D3 JS code needs to be re-bundled as a JupyterLab compatible plugin, but I have not sorted that out yet so unless there is a way to disable jupyterlab's security of not running JS code I don't think the force_plot will work in jupyter lab (though the other plots will).

from shap.

graciofilipe avatar graciofilipe commented on May 5, 2024

Hey all! Is there any way to save the HTML from a python script? I.e. without notebooks, to produce the HTML that shows the plot? I've been trying , but all I can extract is this:

<div id='iASXPVVKAVNYTJGJN6K5C'>
<div style='color: #900; text-align: center;'>
  <b>Visualization omitted, Javascript library not loaded!</b><br>
  Have you run `initjs()` in this notebook? If this notebook was from another
  user you must also trust this notebook (File -> Trust notebook). If you are viewing
  this notebook on github the Javascript has been stripped for security. If you are using
  JupyterLab this error is because a JupyterLab extension has not yet been written.
</div></div>
 <script>
   if (window.SHAP) SHAP.ReactDom.render(
    SHAP.React.createElement(SHAP.AdditiveForceVisualizer, {"outNames": ["output value"], "baseValue": 14.230186462402344, "outValue": 16.489500045776367, "link": "identity", "featureNames": ["CRIM", "ZN", "INDUS", "CHAS", "NOX", "RM", "AGE", "DIS", "RAD", "TAX", "PTRATIO", "B", "LSTAT"], "features": {"0": {"effect": 0.21455369889736176, "value": 0.00632}, "1": {"effect": 0.0013466646196320653, "value": 18.0}, "2": {"effect": 0.018625210970640182, "value": 2.31}, "4": {"effect": -0.40719929337501526, "value": 0.538}, "5": {"effect": -1.1618353128433228, "value": 6.575}, "6": {"effect": -0.05129408836364746, "value": 65.2}, "7": {"effect": -0.34201446175575256, "value": 4.09}, "8": {"effect": -0.0404462069272995, "value": 1.0}, "9": {"effect": 0.005487415939569473, "value": 296.0}, "10": {"effect": 0.050436582416296005, "value": 15.3}, "11": {"effect": 0.027335213497281075, "value": 396.9}, "12": {"effect": 3.9443178176879883, "value": 4.98}}, "plot_cmap": "RdBu", "labelMargin": 20}),
    document.getElementById('iASXPVVKAVNYTJGJN6K5C')
  );
</script>

Thanks for your help!

from shap.

slundberg avatar slundberg commented on May 5, 2024

@graciofilipe You need the code you pasted (inside a valid HTML document) and also the JS code from shap.initjs().

from shap.

graciofilipe avatar graciofilipe commented on May 5, 2024

that worked!
(for the others: you have to find bundle.js)

from shap.

slundberg avatar slundberg commented on May 5, 2024

@noreallyimfine I am not familiar with that conversion process so I can't say how it might go.

from shap.

CnBDM-Su avatar CnBDM-Su commented on May 5, 2024

Hi!

Is there a way to use plotly.tools mpl_to_plotly to convert a force_plot to a plotly plot.

I'm hoping to have a reactive force plot in a plotly dash app.

could you tell the detail about it, how can I convert it to plotly? I need to use shap in dash too. @noreallyimfine

from shap.

yangtaotai avatar yangtaotai commented on May 5, 2024

I got this error for force_plot Exception: matplotlib = True is not yet supported for force plots with multiple samples!

I think your error came from that you are trying to plot multiple rows from your data. I believe the error is saying that you can only plot a single row of data with force_plot!

from shap.

zahs123 avatar zahs123 commented on May 5, 2024

Hi when i use matplotlib= True, i get this error:

numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')

from shap.

wllwang8 avatar wllwang8 commented on May 5, 2024

Exception: matplotlib = True is not yet supported for force plots with multiple samples!

I can plot the figure if no save, when i want to save figure, add matplotlib=True and other not change. Why it does not work? How to save the figure? Thanks! (shap==0.39.0)

image

shap.initjs() # 显示图
shap.plots.force(explainer.expected_value, shap_values_valuesarr, shap_values_data,matplotlib=True, show=False)
plt.savefig("xxx.png",dpi=300) #可以保存图片
plt.show()

from shap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.