Created
April 23, 2020 12:34
-
-
Save UrszulaCzerwinska/c324a8ec8e37ebe4758f4affe52456be 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 simulate_with_shap(x,idx, col, val_range, explainer, pipeline_trans=None): | |
| """ | |
| Function starts with an instance `idx` of dataset x and varies values of selected column `col`, | |
| it needs eplainer for dataset x and optionally it can use the pipeline transformer to transform values as in the initial model | |
| Parameters | |
| ---------- | |
| n_min : int | |
| minimal value to simulate | |
| n_max : int | |
| maximal value to simulate | |
| n_step: int | |
| step for the simulation values | |
| x : pd.Data Frame | |
| dataset | |
| idx : int | |
| selected instance | |
| col : str or int | |
| Indicate name of the column or the index | |
| explainer : shap.explainer | |
| pipeline_trans : pipeline | |
| sklearn transformation of the dataset (optional), if absent dataset used as is | |
| """ | |
| if isinstance(col, str) : | |
| i = x.columns.get_loc(col) | |
| else : | |
| i = col | |
| rg = val_range | |
| r = x.iloc[np.repeat(idx, len(rg))].reset_index(drop=True) | |
| r.iloc[:,i] = rg | |
| if pipeline_trans: | |
| R = pipeline_trans.transform(r) | |
| else: | |
| R = r | |
| hypothetical_shap_values = explainer.shap_values(R) | |
| hypothetical_predictions = explainer.expected_value + hypothetical_shap_values.sum(axis=1) | |
| hypothetical_predictions = 1 / (1 + np.exp(-hypothetical_predictions)) | |
| y_r = np.array([0 if i <= 0.5 else 1 for i in hypothetical_predictions ]) | |
| return (r, R, hypothetical_shap_values, hypothetical_predictions, y_r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment