<jupyter_start><jupyter_text>Random draws from 8 guassian rvs<jupyter_code>np.random.seed=2
X=np.random.randn(8,1000)<jupyter_output><empty_output><jupyter_text>Fitting a linear model on the drawn samples<jupyter_code>Y=1*X[0,] + 2*X[1,] - 3*X[2,] - 4*X[3,] + 5*X[4,] + 6*X[5,] + 7*X[6,] + 10 * X[7,]
plt.plot(X[7,],Y,'o')<jupyter_output><empty_output><jupyter_text>Splitting the data to train,test,split<jupyter_code>sc = StandardScaler()
X_std=sc.fit_transform(X.T)
df_X_std=pd.DataFrame(data=X_std)
test_size=0.30
rand_state=11

q0=-100
q1=np.percentile(Y,25.0)
q2=np.percentile(Y,50.0)
q3=np.percentile(Y,75.0)
q4=100

bins=[q0,q1,q2,q3,q4]

y_binned = np.digitize(Y, bins=bins,right=True)

X_train, X_dev_test, y_train, y_dev_test = train_test_split(df_X_std,Y,stratify=y_binned,
                                             test_size=test_size, shuffle=True,random_state=rand_state)

X_dev, X_test, y_dev, y_test = train_test_split(X_dev_test,y_dev_test,
                                             test_size=0.5, shuffle=False,random_state=rand_state)<jupyter_output><empty_output><jupyter_text>Deep model<jupyter_code>def coeff_determination(y_true, y_pred):
    SS_res =  K.sum(K.square( y_true-y_pred ))
    SS_tot = K.sum(K.square( y_true - K.mean(y_true) ) )
    return ( 1 - SS_res/(SS_tot + K.epsilon()) )
def model(input_shape):
    X_input=Input(shape=input_shape)
    X=Dense(64,name='layer_1',activation='tanh',kernel_initializer = glorot_uniform(seed=0))(X_input)
    X=Dropout(0.25)(X)
    X=Dense(64,name='layer_2',activation='tanh',kernel_initializer = glorot_uniform(seed=0))(X)
    X=Dropout(0.25)(X)
#     X=Dense(64,name='layer_3',activation='tanh',kernel_initializer = glorot_uniform(seed=0))(X)
#     X=Dropout(0.25)(X)
#     X=Dense(64,name='layer_4',activation='tanh',kernel_initializer = glorot_uniform(seed=0))(X)
#     X=Dropout(0.25)(X)
    X=Dense(1 ,name='output',activation='linear')(X)
    model = Model(inputs = X_input, outputs = X, name='PhaseModel')
    model.summary()
    return model
model1=model((X_std.shape[1],))
Adam=optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
model1.compile(optimizer=Adam,
               loss='mean_squared_error', 
               metrics=[coeff_determination])<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>R2_train=[]
R2_dev=[]
R2_test=[]
for i in range(300):
    model1.fit(X_train, y_train,
                  batch_size=1000,
                  epochs=50,
                  validation_data=(X_dev, y_dev),
                  shuffle=False,
                  verbose=0)
    R2_train.append(model1.evaluate(X_train,y_train)[1])
    R2_dev.append(model1.evaluate(X_dev,y_dev)[1])
    R2_test.append(model1.evaluate(X_test,y_test)[1])<jupyter_output><empty_output><jupyter_text>Results  $R^2$<jupyter_code>n_epochs=np.arange(len(R2_train))
plt.figure(figsize=(18,12))
plt.plot(n_epochs,R2_train,'--',label='Train')
plt.plot(n_epochs[-1],R2_dev[-1],'o',markersize=12,label='Dev')
plt.plot(n_epochs[-1],R2_test[-1],'x',markersize=12,label='Test')
#plt.ylim((0.5,1))
plt.ylabel(r'$R^2$')
plt.xlabel('#Epochs')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Predicted vs true values<jupyter_code>y_pred_test=model1.predict(X_test)
plt.plot(y_pred_test,y_test,'o')
plt.xlabel(r'$Y_{true}$',fontsize=12)
plt.ylabel(r'$Y_{predicted}$',fontsize=12)
plt.show()<jupyter_output><empty_output><jupyter_text>Feature importance  SHAP explainer<jupyter_code>#backgournd=shap.kmeans(X_train,10)
backgournd=X_train
explainer = shap.KernelExplainer(model1.predict, backgournd)
shap_values= explainer.shap_values(X_test.iloc[0:50])
df_shap_values=pd.DataFrame(data=shap_values[0],
                            columns=['var_'+str(i) for i in range(8)],
                            index=['sample_'+str(i) for i in range(shap_values[0].shape[0])])<jupyter_output><empty_output><jupyter_text>Local SHAP values (for a single instance)<jupyter_code>idx=7   ## Index of the desired sample instance
local_shap_vs_true_coef=pd.DataFrame(index=df_shap_values.columns)
local_shap_vs_true_coef['SHAP']=df_shap_values.iloc[idx].values/df_shap_values.iloc[idx,0]
local_shap_vs_true_coef['True coef']=[1,2,-3,-4,5,6,7,10]
local_shap_vs_true_coef.plot(kind='bar',figsize=(18,12))
plt.show()<jupyter_output><empty_output><jupyter_text>Global SHAP (averaged over X_test)<jupyter_code>Global_shap_vs_true_coef=pd.DataFrame(index=df_shap_values.columns)
Global_shap_vs_true_coef['SHAP']=df_shap_values.mean(axis=0)
Global_shap_vs_true_coef['SHAP']=Global_shap_vs_true_coef['SHAP']/Global_shap_vs_true_coef['SHAP'].iloc[0]
Global_shap_vs_true_coef['True coef']=[1,2,-3,-4,5,6,7,10]
Global_shap_vs_true_coef.plot(kind='bar',figsize=(18,12))<jupyter_output><empty_output><jupyter_text>Global Absolute SHAP (averaged over X_test)<jupyter_code>Global_abs_shap_vs_true_coef=pd.DataFrame(index=df_shap_values.columns)
Global_abs_shap_vs_true_coef['SHAP']=np.abs(df_shap_values).mean(axis=0)
Global_abs_shap_vs_true_coef['SHAP']=Global_abs_shap_vs_true_coef['SHAP']/Global_abs_shap_vs_true_coef['SHAP'].iloc[0]
Global_abs_shap_vs_true_coef['True coef']=[1,2,-3,-4,5,6,7,10]
Global_abs_shap_vs_true_coef.plot(kind='bar',figsize=(18,12))<jupyter_output><empty_output><jupyter_text>Deep SHAP<jupyter_code>Deep_background=X_train
Deep_exp=shap.DeepExplainer(model1, Deep_background)
Deep_shap_values = Deep_exp.shap_values(X_test.values)
df_Deep_shap_values=pd.DataFrame(data=Deep_shap_values[0],
                            columns=X_test.columns,
                            index=X_test.index)<jupyter_output><empty_output><jupyter_text>local Deep SHAP<jupyter_code>idx=7   ## Index of the desired sample instance
local_shap_vs_true_coef=pd.DataFrame(index=df_Deep_shap_values.columns)
local_shap_vs_true_coef['Deep SHAP']=df_Deep_shap_values.iloc[idx].values/df_Deep_shap_values.iloc[idx,0]
local_shap_vs_true_coef['True coef']=[1,2,-3,-4,5,6,7,10]
local_shap_vs_true_coef.plot(kind='bar',figsize=(18,12))
plt.show()<jupyter_output><empty_output><jupyter_text>GLobal Deep SHAP<jupyter_code>Global_shap_vs_true_coef=pd.DataFrame(index=df_Deep_shap_values.columns)
Global_shap_vs_true_coef['Deep SHAP']=df_Deep_shap_values.mean(axis=0)
Global_shap_vs_true_coef['Deep SHAP']=Global_shap_vs_true_coef['Deep SHAP']/Global_shap_vs_true_coef['Deep SHAP'].iloc[0]
Global_shap_vs_true_coef['True coef']=[1,2,-3,-4,5,6,7,10]
Global_shap_vs_true_coef.plot(kind='bar',figsize=(18,12))<jupyter_output><empty_output><jupyter_text>Global absolute Deep SHAP<jupyter_code>Global_abs_shap_vs_true_coef=pd.DataFrame(index=df_Deep_shap_values.columns)
Global_abs_shap_vs_true_coef['Deep SHAP']=np.abs(df_Deep_shap_values).mean(axis=0)
Global_abs_shap_vs_true_coef['Deep SHAP']=Global_abs_shap_vs_true_coef['Deep SHAP']/Global_abs_shap_vs_true_coef['Deep SHAP'].iloc[0]
Global_abs_shap_vs_true_coef['True coef']=[1,2,-3,-4,5,6,7,10]
Global_abs_shap_vs_true_coef.plot(kind='bar',figsize=(18,12))
# def lime_pred(x,model=model1):
#     out=model.predict(x)
#     return out.squeeze()
# lime_explainer = lime.lime_tabular.LimeTabularExplainer(X_train, feature_names=list(df_X_std.columns), 
#                                                    #class_names=y_train,
#                                                    verbose=False, 
#                                                    mode='regression',
#                                                   discretize_continuous=False)
# y_true=[]
# y_lime=[]
# for i in range(0,Y.shape[0],10):
#     lime_exp = lime_explainer.explain_instance(df_X_std.iloc[i].values, lime_pred)
#     y_true.append(Y[i])
#     y_lime.append(lime_exp.local_pred[0])
# x_dummy=np.arange(-50,50,0.1)
# plt.figure(figsize=(18,12))
# plt.plot(y_true,y_lime,'o',alpha=0.5)
# plt.plot(x_dummy,x_dummy,'--')
# plt.ylabel(r'$Y_{LIME}$')
# plt.xlabel(r'$Y_{True}$')
# plt.show()<jupyter_output><empty_output><jupyter_text>Local LIME values (local:for a single data instance)<jupyter_code># exp = lime_explainer.explain_instance(X_test.iloc[3,:], lime_pred)
# exp.show_in_notebook(show_table=True)
# lime_vals=[val[1] for val in exp.as_list()]
# lime_vals.reverse()
# lime_vs_true_coef=pd.DataFrame(index=df_shap_values.columns)
# lime_vs_true_coef['LIME']=lime_vals
# lime_vs_true_coef['True coef']=[1,2,-3,-4,5,6,7,10]
# lime_vs_true_coef.plot(kind='bar',figsize=(18,12))<jupyter_output><empty_output><jupyter_text>SKATER<jupyter_code># skater_pred_model = InMemoryModel(model1.predict)
# interpreter = Interpretation()
# interpreter.load_data(df_X_std)
# imp=interpreter.feature_importance.feature_importance(skater_pred_model)
# plt.bar(n,imp/imp[0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15526164462334674, 'fraction_numerical': 0.03346751006325474, 'mean_word_length': 6.28918692372171, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '350263', 'license': 'MIT', 'n_tokens_mistral': 3805, 'n_tokens_neox': 3660, 'n_words': 301, 'path': 'Kernel SHAP vs LIME.ipynb', 'repo_name': 'ArashSaeidpour/model-agnostic-interp', 'size': '8695'}