<jupyter_start><jupyter_text>Feature importance with SHAP<jupyter_code>import numpy as np
import shap #pip install shap

from asap_essay_scoring import data
from asap_essay_scoring import learners

# Load training
dm = data.DataManager(target = 'domain1_score')
dt = dm.prepare_data(['len_benchmark', 'wordvec', 'token'])

# Train on all data
learner = learners.Lgbm()
learner.train(dt)

# import lightgbm
# learner.bst.feature_importance(importance_type='gain')
# Randomly sample 1000 essays for fast visualization:
sel = np.random.choice(range(dt.X.shape[0]), size=1000, replace=False)
X = dt.X.iloc[sel]
shap_values = shap.TreeExplainer(learner.bst).shap_values(X)
shap.summary_plot(shap_values, X, max_display = 7)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11497326203208556, 'fraction_numerical': 0.014705882352941176, 'mean_word_length': 7.230769230769231, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '77461', 'license': 'MIT', 'n_tokens_mistral': 282, 'n_tokens_neox': 266, 'n_words': 58, 'path': 'notebooks/feature_importances.ipynb', 'repo_name': 'zkurtz/ASAP-AES-workbench', 'size': '748'}