Created
April 21, 2020 15:30
-
-
Save UrszulaCzerwinska/b7853f5f0b209f2f89e2a3590dd6f329 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import plotly.io as pio | |
| pio.templates.default = "plotly_white" | |
| from plotly.subplots import make_subplots | |
| import pandas as pd | |
| import plotly.express as px | |
| import matplotlib.pylab as pl | |
| import numpy as np | |
| def dependence_plot_classes(feature_name,X_background_display, y_background, shap_values) : | |
| """ | |
| Create a plot showing relationship between a feature and the shapley values for this feature, colored by the class value. | |
| Parameters | |
| ---------- | |
| feature_name : str or int | |
| Indicate name of the column or the index | |
| X_background_display : pandas.DataFrame | |
| Data frame of feature values (# samples x # features) with non tranformed features values of preference | |
| y_background : list or np.array | |
| the classes - can be real or predicted | |
| shap_values : np.array | |
| shapley values matrx | |
| """ | |
| if isinstance(feature_name, str): | |
| i = X_background_display.columns.get_loc(feature_name) | |
| else : | |
| i = feature_name | |
| assert(i not in X_background_display.columns), "Column name not in data frame columns" | |
| a = shap_values[:,i] | |
| b = X_background_display.iloc[:,i].values | |
| res = y_background.astype(int).astype(str) | |
| assert len(a) == len(b) | |
| y_name = "Shapley values for "+ X_background_display.columns[i] | |
| x_name = X_background_display.columns[i] | |
| df = pd.DataFrame({y_name:a,x_name:b,"y_label": res}) | |
| fig = px.scatter(df, x=x_name, y=y_name, color="y_label", marginal_y="histogram", marginal_x="histogram",width=500, height=500,opacity=0.5) | |
| fig.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello author, I encountered this issue while using your code. Could you please take a look at where the problem lies.
TypeError Traceback (most recent call last)
Cell In[52], line 1
----> 1 dependence_plot_classes('Carbonation Time',training_feature_value, training_target_value, shap_values)
Cell In[49], line 42, in dependence_plot_classes(feature_name, X_background_display, y_background, shap_values)
39 df = pd.DataFrame({y_name:a,x_name:b,"y_label": res})
40 fig = px.scatter(df, x=x_name, y=y_name, color="y_label", marginal_y="histogram", marginal_x="histogram",width=500, height=500,opacity=0.5)
---> 42 fig.show()
File ~\anaconda3\Lib\site-packages\plotly\basedatatypes.py:3398, in BaseFigure.show(self, *args, **kwargs)
3365 """
3366 Show a figure using either the default renderer(s) or the renderer(s)
3367 specified by the renderer argument
(...)
3394 None
3395 """
3396 import plotly.io as pio
-> 3398 return pio.show(self, *args, **kwargs)
File ~\anaconda3\Lib\site-packages\plotly\io_renderers.py:388, in show(fig, renderer, validate, **kwargs)
385 fig_dict = validate_coerce_fig_to_dict(fig, validate)
387 # Mimetype renderers
--> 388 bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
389 if bundle:
390 if not ipython_display:
File ~\anaconda3\Lib\site-packages\plotly\io_renderers.py:296, in RenderersConfig._build_mime_bundle(self, fig_dict, renderers_string, **kwargs)
293 if hasattr(renderer, k):
294 setattr(renderer, k, v)
--> 296 bundle.update(renderer.to_mimebundle(fig_dict))
298 return bundle
File ~\anaconda3\Lib\site-packages\plotly\io_base_renderers.py:95, in PlotlyRenderer.to_mimebundle(self, fig_dict)
91 if config:
92 fig_dict["config"] = config
94 json_compatible_fig_dict = json.loads(
---> 95 to_json(fig_dict, validate=False, remove_uids=False)
96 )
98 return {"application/vnd.plotly.v1+json": json_compatible_fig_dict}
File ~\anaconda3\Lib\site-packages\plotly\io_json.py:199, in to_json(fig, validate, pretty, remove_uids, engine)
196 for trace in fig_dict.get("data", []):
197 trace.pop("uid", None)
--> 199 return to_json_plotly(fig_dict, pretty=pretty, engine=engine)
File ~\anaconda3\Lib\site-packages\plotly\io_json.py:123, in to_json_plotly(plotly_object, pretty, engine)
119 opts["separators"] = (",", ":")
121 from _plotly_utils.utils import PlotlyJSONEncoder
--> 123 return json.dumps(plotly_object, cls=PlotlyJSONEncoder, **opts)
124 elif engine == "orjson":
125 JsonConfig.validate_orjson()
File ~\anaconda3\Lib\json_init_.py:238, in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
232 if cls is None:
233 cls = JSONEncoder
234 return cls(
235 skipkeys=skipkeys, ensure_ascii=ensure_ascii,
236 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
237 separators=separators, default=default, sort_keys=sort_keys,
--> 238 **kw).encode(obj)
File ~\anaconda3\Lib\site-packages_plotly_utils\utils.py:59, in PlotlyJSONEncoder.encode(self, o)
52 """
53 Load and then dump the result using parse_constant kwarg
54
55 Note that setting invalid separators will cause a failure at this step.
56
57 """
58 # this will raise errors in a normal-expected way
---> 59 encoded_o = super(PlotlyJSONEncoder, self).encode(o)
60 # Brute force guessing whether NaN or Infinity values are in the string
61 # We catch false positive cases (e.g. strings such as titles, labels etc.)
62 # but this is ok since the intention is to skip the decoding / reencoding
63 # step when it's completely safe
65 if not ("NaN" in encoded_o or "Infinity" in encoded_o):
File ~\anaconda3\Lib\json\encoder.py:200, in JSONEncoder.encode(self, o)
196 return encode_basestring(o)
197 # This doesn't pass the iterator directly to ''.join() because the
198 # exceptions aren't as detailed. The list call should be roughly
199 # equivalent to the PySequence_Fast that ''.join() would do.
--> 200 chunks = self.iterencode(o, _one_shot=True)
201 if not isinstance(chunks, (list, tuple)):
202 chunks = list(chunks)
File ~\anaconda3\Lib\json\encoder.py:258, in JSONEncoder.iterencode(self, o, _one_shot)
253 else:
254 _iterencode = _make_iterencode(
255 markers, self.default, _encoder, self.indent, floatstr,
256 self.key_separator, self.item_separator, self.sort_keys,
257 self.skipkeys, _one_shot)
--> 258 return _iterencode(o, 0)
File ~\anaconda3\Lib\site-packages_plotly_utils\utils.py:136, in PlotlyJSONEncoder.default(self, obj)
134 except NotEncodable:
135 pass
--> 136 return _json.JSONEncoder.default(self, obj)
File ~\anaconda3\Lib\json\encoder.py:180, in JSONEncoder.default(self, o)
161 def default(self, o):
162 """Implement this method in a subclass such that it returns
163 a serializable object for
o, or calls the base implementation164 (to raise a
TypeError).(...)
178
179 """
--> 180 raise TypeError(f'Object of type {o.class.name} '
181 f'is not JSON serializable')
TypeError: Object of type Explanation is not JSON serializable