<jupyter_start><jupyter_text>Load data, train model<jupyter_code>import sage
import numpy as np
from catboost import CatBoostClassifier
from sklearn.model_selection import train_test_split
# Load data
df = sage.datasets.credit()

# Feature names and categorical columns (for CatBoost model)
feature_names = df.columns.tolist()[:-1]
categorical_columns = [
    'Checking Status', 'Credit History', 'Purpose', 'Credit Amount',
    'Savings Account/Bonds', 'Employment Since', 'Personal Status',
    'Debtors/Guarantors', 'Property Type', 'Other Installment Plans',
    'Housing Ownership', 'Job', 'Telephone', 'Foreign Worker'
]
categorical_inds = [feature_names.index(col) for col in categorical_columns]
# Split data
train, test = train_test_split(
    df.values, test_size=int(0.1 * len(df.values)), random_state=0)
train, val = train_test_split(
    train, test_size=int(0.1 * len(df.values)), random_state=0)
Y_train = train[:, -1].copy().astype(int)
Y_val = val[:, -1].copy().astype(int)
Y_test = test[:, -1].copy().astype(int)
train = train[:, :-1].copy()
val = val[:, :-1].copy()
test = test[:, :-1].copy()
model = CatBoostClassifier(iterations=50,
                           learning_rate=0.3,
                           depth=3)

model = model.fit(train, Y_train, categorical_inds, eval_set=(val, Y_val),
                  verbose=False)<jupyter_output><empty_output><jupyter_text>LossSHAP cooperative game<jupyter_code>from shapreg import removal, games, shapley
from shapreg.utils import crossentropyloss
# Make model callable
model_lam = lambda x: model.predict_proba(x)

# Model extension
marginal_extension = removal.MarginalExtension(train[:128], model_lam)
# Set up game (single prediction)
instance = test[0]
label = Y_test[0]
game = games.PredictionLossGame(marginal_extension, instance, label, crossentropyloss)
# Run estimator
explanation = shapley.ShapleyRegression(game, batch_size=32)
# Plot with 95% confidence intervals
explanation.plot(feature_names, title='LossSHAP Values')<jupyter_output><empty_output><jupyter_text>SAGE cooperative game<jupyter_code>from shapreg import stochastic_games
# Set up game (dataset loss)
game = stochastic_games.DatasetLossGame(marginal_extension, test, Y_test, crossentropyloss)
# Run estimator
explanation = shapley.ShapleyRegression(game)
# Plot with 95% confidence intervals
explanation.plot(feature_names, title='SAGE Values')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12076095947063689, 'fraction_numerical': 0.011993382961124897, 'mean_word_length': 5.700831024930748, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '641650', 'license': 'MIT', 'n_tokens_mistral': 835, 'n_tokens_neox': 793, 'n_words': 185, 'path': 'notebooks/credit.ipynb', 'repo_name': 'iancovert/shapley-regression', 'size': '2418'}