Created
April 22, 2020 15:58
-
-
Save UrszulaCzerwinska/b78e85c6e1795d949321209cbe41587a 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
| def interaction_plot(col1, col2, interaction_values, background_display, is_cat=True, width=700, height=500,opacity=0.7): | |
| """ | |
| This function plots shap interaction values between two selected columns (col1 & col2), where x axis are values of column 1, y axis are shap interaction values, and hue (color) is the second column. | |
| Args: | |
| col1 (str) : name of the first feature | |
| col2 (str) : name of the second feature | |
| interaction_values (numpy.array) : matrix obtained with shap shap_interaction_values | |
| background_display (pandas.DataFrame) : the original values for background data used in shap explainer | |
| is_cat (bool) : is the col2 a categorical value, True by default | |
| Returns: | |
| an interactive version of shap dependence_plot | |
| """ | |
| interact_val_array=np.empty(background_display.iloc[:,0].size) | |
| for idx, __ in enumerate(background_display.iloc[:,0]): | |
| i = background_display.columns.get_loc(col1) | |
| j= background_display.columns.get_loc(col2) | |
| res = interaction_values[idx,i,j] | |
| interact_val_array[idx]=res | |
| a = background_display.loc[:,col1].values | |
| b = background_display.loc[:,col2].values | |
| if is_cat: | |
| b = b.astype(str) | |
| assert len(a) == len(b) | |
| y_name = "Shapley interaction values for "+col1+ " & "+col2 | |
| x_name = col1 | |
| color_name = col2 | |
| df = pd.DataFrame({y_name:interact_val_array,x_name:a,color_name:b }) | |
| fig = px.scatter(df, x=x_name, y=y_name, color=color_name,width=width, height=height,opacity=opacity) | |
| fig.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment