SHAP force plot in python - python

I'm new to using the SHAP library in python.
I'm trying to create a force plot in order to view the output of a single specific observation.
This is the code I used:
shap.force_plot(
explainer.expected_value,shap_values[4102,:],
x_validation.iloc[4102,:],matplotlib=True
)
The code works but I just wanted to be sure - does this code provide the force plot for the observation in row 4102?
Is there anything missing/some unnecessary parameters?

Related

Matplotlib plot function output differs from seaborn's lineplot

I want to plot a PDF function given data which follows a normal distribution. Mainly I followed this link.
Now, if I am working on the data created like on that website (x=np.linspace()) and I plot it with either seaborn.lineplot() or matplotlib.pyplot.plot(), I get a normal curve as shown on the website linked above. But when I do this with my own data (which I believe is normal, but with a lot more data points) instead of initializing it with np.linspace I get a clear normal curve with seaborn's lineplot and a messy normal curve with matplotlib's plot function.
I have tried to look for default arguments on both functions but couldn't find any (except estimator) which would cause this behavior. The estimator argument of Seaborn's lineplot was the only argument that looked like it could do something like this but setting it to None did not make any difference (and it kind of makes sense I think since the y value is always same for a specific x so averaging out will produce the same value).
I used to think both functions are the same, but then why do they have different output?
The Seaborn lineplot function has the default parameter sort=True.
So unless you tell it not to, it'll order the data for you. This is not something which pyplot.plot() does, instead it'll draw lines between the points in the order provided.
If you want to order the data before plotting it using Pyplot, there's a good solution for how to do that.

Is there a Python function to compute isosurface end-cap geometry like the 'isocaps' function in Matlab?

I want to get some 3D models defined by implicit functions like F(x,y,z)=0.
It can be done using the 'isosurface' and 'isocaps' functions in MATLAB.
In my case, the constructed models need post-process in Python so it would be better if the modelling can be done in Python.
'mayavi.mlab.Contour3d' and 'plotly.graph_objects.Isosurface' are able to display the isosurface while 'skimage.measure.marching_cubes' can be used to extract the trangler mesh.
Can anyone help me find a way in Python to get the isosurface end-cap?
I'm looking for a way to do the exact same thing. Another user gave me the following solution for my own request.
Is there a way to fill one side of the gyroid surface by using Mayavi?
You have to use an extra tool for visualization to do it. I'm using Mayavi for generating the structures I need but I can't get the surface end-cap by using it.
Maybe the answer to my question can help you out.

Show individual cluster elements in a dendrogram graph

It is easy to use two images to showcase what I am working to create. I have the following Python dendrogram, created from the following code (not currently reproducible, but wanted to show the code regardless):
# Initialize Plot
plt.figure(figsize=(18,9))
hierarchy.dendrogram(
Z=Z,
p=20,
orientation="top",
truncate_mode='lastp',
leaf_rotation=45.,
leaf_font_size=15.,
)
plt.show()
It is fairly simple and straightforward to this point. However, in an effort to better visualize the clusters, I'd like to show this same dendrogram, with string values for the elements in the cluster below the cluster, as such (created demo by pasting image into Excel and typing values):
Is this possible to do in Python with the dendrogram function, or in any other way? A hacky approach that uses subplots + a 2nd "graph" that is actually a table could be a possible solution, however it would be good if a less-hacky solution existed.

Update source data through chart in Python

Is there any way in Python to modify the source data by moving data points in the chart?
This is intended to work in streamlit: I'd like to make it convenient for users to edit multiple parameters of the same kind visually changing the curve which represents series of parameters and their corresponding value. As far as I know, streamlit currently supports Matplotlib, Plotly, Altair, Bokeh, Vega Lite, Deck.GL, dagre-d3 charts, but not sure whether any of those libraries provide this functionality.

Python Plotly in Power BI

I'm trying to build a Power BI tool for some data analysis, and one of the plots I need is an inverse quantile plot (quantiles on x axis, values on y axis). Power BI does not have this, and I can't find one on the app marketplace, so am using Python to code up what I need.
The static plot from pandas.DataFrame.plot() works fine but lacks the pinache of an interactive plot. I've coded up the plot I need using plotly, and ran it with py.iplot(), but Power BI tells me
No image was created. The Python code didn't result in creation of any visuals. Make sure your Python script results in a plot to the Python default device
There was no error, and I confirmed the code is fine by running the plot using py.plot(), and viewed the result in the browser. My code is:
import plotly.plotly as py
import plotly.graph_objs as go
# get the quantiles and reshape
qs = dataset.groupby(by='HYDROCARBON_TYPE').Q42018_AbsDevi.quantile(q=[0.01,0.05,0.1,0.2,0.25,0.5,0.75,0.8,0.9,0.95,0.99]).unstack().transpose()
# plot it
traces = []
for col in qs.columns:
traces.append(go.Scatter(x=qs.index, y=qs[col], name=col))
py.plot(traces,filename='basic-line')
Why would this not be working?
I wasn't able to find a solution using PowerBI, Plotly and Python, nor was I able to reproduce your errors. Regarding your errors, I ended up with visualizations that were either timed out or reporting a data type error. But we can get back to that if that's still interesting after another suggested solution, because I have been able to produce an interactive q-plot using PowerBI, plotly, ggplot and an R script visual like this:
Assuming that your main priorities are to make an interactive quantile plot in PowerBI, and that Python as a tool comes second, just follow the steps outlined in this post, and replace the R script with this:
source('./r_files/flatten_HTML.r')
############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
####################################################
################### Actual code ####################
df <- data.frame(y = Values$Data)
# Build basic ggplot
g <- ggplot(df, aes(sample = y))
# Add quantile details
g = g + stat_qq() + stat_qq_line()
############# Create and save widget ###############
p = ggplotly(g);
internalSaveWidget(p, 'out.html');
####################################################
That should do the trick. Don't hesitate to let me know if this does not work for you.
You can take a look at this blog post. It describes how to add an interactive javascript Plotly Chart in Power BI. Its quite easy.
Kind regards,
Steve.

Categories

Resources