Skip to content

Instantly share code, notes, and snippets.

@UrszulaCzerwinska
Created April 23, 2020 12:34
Show Gist options
  • Select an option

  • Save UrszulaCzerwinska/c324a8ec8e37ebe4758f4affe52456be to your computer and use it in GitHub Desktop.

Select an option

Save UrszulaCzerwinska/c324a8ec8e37ebe4758f4affe52456be to your computer and use it in GitHub Desktop.
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