<jupyter_start><jupyter_text>Load data<jupyter_code>import pandas as pd
from data.utils import split_data
# Feature names
columns = [
    'Checking Status', 'Duration', 'Credit History', 'Purpose',
    'Credit Amount', 'Savings Account/Bonds', 'Employment Since',
    'Installment Rate', 'Personal Status', 'Debtors/Guarantors',
    'Residence Duration', 'Property Type', 'Age',
    'Other Installment Plans', 'Housing Ownership',
    'Number Existing Credits', 'Job', 'Number Liable', 'Telephone',
    'Foreign Worker', 'Good Customer'
]
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'
]

# Load data.
df = pd.read_table('data/german.data', sep=' ', header=None,
                   index_col=None, names=columns)

# Make categorical columns numerical.
for column in categorical_columns:
    df[column] = pd.Categorical(df[column]).codes

# Make labels 0/1.
df['Good Customer'] = df['Good Customer'] - 1
# Split data.
train, val, test = split_data(df.values, seed=123,
                              val_portion=0.1,
                              test_portion=0.1)
Y_train = train[:, -1]
Y_val = val[:, -1]
Y_test = test[:, -1]
train = train[:, :-1]
val = val[:, :-1]
test = test[:, :-1]<jupyter_output><empty_output><jupyter_text>Train model<jupyter_code>import numpy as np
from sklearn.metrics import log_loss
from sklearn.ensemble import GradientBoostingClassifier
# Train model.
model = GradientBoostingClassifier(n_estimators=100)
model.fit(train, Y_train)
# Base rate performance.
p = np.array([np.sum(Y_train == i) for i in np.unique(Y_train)]) / len(Y_train)
print('Base rate cross entropy = {:.3f}'.format(
    log_loss(Y_val, p[np.newaxis].repeat(len(val), 0))))

# Model performance.
print('Model cross entropy = {:.3f}'.format(
    log_loss(Y_val, model.predict_proba(val))))<jupyter_output>Base rate cross entropy = 0.602
Model cross entropy = 0.514<jupyter_text>Calculate feature importance<jupyter_code>import sage
import matplotlib.pyplot as plt
# Setup
f = lambda x: model.predict_proba(x)
imputer = sage.utils.MarginalImputer(train, 512)
sampler = sage.PermutationSampler(f, imputer, 'cross entropy')

# Calculate SAGE values
sage_values = sampler(xy=(test, Y_test),
                      batch_size=2 ** 10,
                      n_permutations=2 ** 12,
                      bar=True)
# Plot results
feature_names = columns[:-1]
sage_values.plot(feature_names)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13846153846153847, 'fraction_numerical': 0.0150093808630394, 'mean_word_length': 4.117082533589252, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '120266', 'license': 'MIT', 'n_tokens_mistral': 939, 'n_tokens_neox': 885, 'n_words': 219, 'path': 'credit.ipynb', 'repo_name': 'jjanizek/sage', 'size': '2665'}