<jupyter_start><jupyter_text>Neural Networks interpretability notebook<jupyter_code>#Import statements
import os
import matplotlib.pyplot as plt
import pandas as pd
import shap
import matplotlib.pyplot as plt
from pycaret import classification<jupyter_output><empty_output><jupyter_text>Exploratory data analysisImport to specify correctly the data path. Initally we can make an easy exploration.<jupyter_code>data_folder_path = os.path.join('..', 'data')
data_file = 'ds.csv'
df = pd.read_csv(os.path.join(data_folder_path, data_file))
df.describe()<jupyter_output><empty_output><jupyter_text>Classification Set-upDefinition of main model hyperparameters. Numeric features and target with full description available.<jupyter_code>classification_setup = classification.setup(
    data=df,
    target='gender',
    numeric_features=[c for c in df.columns if c not in ['gender', 'matchPeriod', 'PlayerType']]
)<jupyter_output>Setup Succesfully Completed!<jupyter_text>Basic multi-level perceptron implementation form Scikit Library. Evaluation with a 10-fold cross validation.<jupyter_code>nn_model = classification.create_model('mlp')
classification.save_model(nn_model, 'nn_model_19')
nn_model
tuned_nn_model = classification.tune_model(nn_model)
classification.save_model(tuned_nn_model, 'tunned_nn_model_19')
tuned_nn_model<jupyter_output>Transformation Pipeline and Model Succesfully Saved<jupyter_text>SHAP Kernel Explainer for NNImplementing SHAP kernel explainer with a 120 sampling.<jupyter_code>explainer = shap.KernelExplainer(tuned_nn_model.predict_proba, shap.kmeans(classification_setup[2], 120))
shap_values = explainer.shap_values(classification_setup[3])
len(shap_values)
shap.initjs()
#gender (Female = 0, Male = 1)
gender = 0
instance = 10
shap.force_plot(explainer.expected_value[gender], shap_values[gender][instance,:], classification_setup[3].iloc[instance,:])
instance += 1
shap.force_plot(explainer.expected_value[gender], shap_values[gender][instance,:], classification_setup[3].iloc[instance,:])
shap.force_plot(explainer.expected_value[gender], shap_values[gender][instance,:], classification_setup[3].iloc[instance,:], show=False, matplotlib=True).savefig(f"individual_plot_{instance}.png",format = "png",dpi = 300,bbox_inches = 'tight')
shap.summary_plot(shap_values[0], classification_setup[3], max_display=15, show=False)
#plt.show()
plt.savefig("summary_plot.png",format = "png",dpi = 150,bbox_inches = 'tight')
feature = -1
feature += 1
shap.dependence_plot(feature,shap_values[0], classification_setup[3], interaction_index=None, show=False)
feature_name = classification_setup[3].columns[feature]
shap.dependence_plot(feature,shap_values[0], classification_setup[3], interaction_index=None, show=False)
plt.savefig(f"dependance_{feature_name}.png",format = "png",dpi = 150,bbox_inches = 'tight')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11542497376705142, 'fraction_numerical': 0.015040223854494578, 'mean_word_length': 9.79245283018868, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '624892', 'license': 'MIT', 'n_tokens_mistral': 994, 'n_tokens_neox': 937, 'n_words': 175, 'path': 'classification/nn_analysis.ipynb', 'repo_name': 'marcgarnica13/ml-interpretability-european-football', 'size': '2859'}