Created
May 2, 2021 17:14
-
-
Save thistleknot/b59ae900cf57cbd74d7d7aa007770b93 to your computer and use it in GitHub Desktop.
Python shap values
This file contains 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 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:])) |
Author
thistleknot
commented
May 2, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment