Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python shap values
import sklearn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
import numpy as np
import shap
shap.initjs()
# select a set of background examples to take an expectation over
background = np.array(data)
#[np.random.choice(X_train.shape[0],100,replace=False)]
model = sklearn.linear_model.LinearRegression()
model.fit(data.iloc[:,1:], data.iloc[:,0])
# explain predictions of the model on four images
e = shap.LinearExplainer(model, data.iloc[:,1:])
#explainer = shap.KernelExplainer(model, X_train, link="logit")
shap_values = e.shap_values(np.array(data.iloc[:,1:]))
shap.summary_plot(shap_values, -np.array(data.iloc[:,1:]))
explainer = shap.Explainer(model, data.iloc[:,1:])
shap.plots.heatmap(explainer(data.iloc[:,1:]))
@thistleknot

This comment has been minimized.

Copy link
Owner Author

@thistleknot thistleknot commented May 2, 2021

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment