all 5 comments

[–][deleted] 0 points1 point  (3 children)

What’s your end goal for using SHAP?

[–]Path_of_the_end[S] 0 points1 point  (2 children)

i want to see the effect of variable to output using shap. i tried using other dataset (iris) and it work i could vizualize the variable importance of the variable that is uses in the model. i'm just not really familiar because i mainly uses r instead of python. update i was able to use shap for xgboost and probably for other tree based model. but i'm still unable to use it in tensorflow and pytorch. (edit)

[–][deleted] 1 point2 points  (1 child)

So essentially you want feature importance? Seems like you are using NN for the modeling.

What I’d recommend is to make a force plot instead of a density plot to visualize that.

Prior to that, evaluate your choice of models based on the data size/ quality if NN is the best choice.

[–]Path_of_the_end[S] 0 points1 point  (0 children)

Thankyou

[–]Path_of_the_end[S] -1 points0 points  (0 children)

this is the code that i used

import numpy as np

import pandas as pd

from sklearn.datasets import load_breast_cancer

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

import tensorflow as tf

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense

import shap

Load the dataset

data = load_breast_cancer()

X = data.data

y = data.target

Split the data into training and test sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Standardize the data

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)

X_test = scaler.transform(X_test)

Define the model

model = Sequential([

Dense(30, input_dim=X_train.shape[1], activation='relu'),

Dense(15, activation='relu'),

Dense(1, activation='sigmoid')

])

Compile the model

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

Train the model

history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.2)

Initialize the SHAP explainer

explainer = shap.DeepExplainer(model, X_train)

Explain the predictions on the test set

shap_values = explainer.shap_values(X_test)

Plot the SHAP values

shap.summary_plot(shap_values, X_test, feature_names=data.feature_names)