<jupyter_start><jupyter_text>CIU - Example: Random Forest, Loan Application ClassificationThis example demonstrates how to use Py-CIU to explain loan application classification decisions as made by a random forest. Let us first create a synthetic dataset of loan applications, with the features and their approval decisions. The dataset has the following features:* ``age``,* ``assets``* ``monthly_income``,* ``gender_female``,* ``gender_male``,* ``gender_other``,* ``job_type_fixed``,* ``job_type_none``,* ``job_type_permanent``.We import the third-party dependencies, Py-CIU, and a synthetic data generator:<jupyter_code>!pip install -e ../
!pip install sklearn
from sklearn.ensemble import RandomForestClassifier

import project_path
from ciu import determine_ciu
from ciu_tests.loan_data_generator import generate_data<jupyter_output><empty_output><jupyter_text>Now, we run the generator to create our dataset and use it to create a random forest classifier:<jupyter_code>data = generate_data()
train_data = data['train'][1]
test_data = data
test_data_encoded = data['test'][1].drop(['approved'], axis=1)
random_forest = RandomForestClassifier(
    n_estimators=1000,
    random_state=42
)

labels = train_data[['approved']].values.ravel()
data = train_data.drop(['approved'], axis=1)
random_forest.fit(data, labels)<jupyter_output><empty_output><jupyter_text>Now, we take a case and classify it:<jupyter_code>feature_names = [
    'age', 'assets', 'monthly_income', 'gender_female', 'gender_male',
    'gender_other', 'job_type_fixed', 'job_type_none', 'job_type_permanent'
]

case = test_data_encoded.values[0]
example_prediction = random_forest.predict([test_data_encoded.values[0]])
example_prediction_prob = random_forest.predict_proba([test_data_encoded.values[0]])
prediction_index = 0 if example_prediction[0] > 0.5 else 1

print(feature_names)
print(f'Case: {case}; Prediction {example_prediction}; Probability: {example_prediction_prob}')<jupyter_output><empty_output><jupyter_text>We call the CIU function. Note that this requires use to provide a mapping from "raw data" feature names to one-hot encoded feature names. Also, we want to evaluate the CIU not only of features, but also of the feature interaction *income/assets*.<jupyter_code>category_mapping = {
    'gender': ['gender_female', 'gender_male', 'gender_other'],
    'job_type': ['job_type_fixed', 'job_type_none', 'job_type_permanent']
}

feature_interactions = [{'assets_income': ['assets', 'monthly_income']}]

ciu = determine_ciu(
    test_data_encoded.iloc[0, :].to_dict(),
    random_forest.predict_proba,
    {
        'age': [20, 70, True],
        'assets': [-20000, 150000, True],
        'monthly_income': [0, 20000, True],
        'gender_female': [0, 1, True],
        'gender_male': [0, 1, True],
        'gender_other': [0, 1, True],
        'job_type_fixed': [0, 1, True],
        'job_type_none': [0, 1, True],
        'job_type_permanent': [0, 1, True]
    },
    1000,
    prediction_index,
    category_mapping,
    feature_interactions
)<jupyter_output><empty_output><jupyter_text>Finally, we display the contextual importance (importance for class) and utility (typicality for class) in different ways:<jupyter_code>ciu.plot_ci()
ciu.plot_cu()
print(ciu.text_explain())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12575757575757576, 'fraction_numerical': 0.01696969696969697, 'mean_word_length': 6.023404255319149, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, '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': '604322', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1063, 'n_words': 275, 'path': 'examples/Random_Forrest_CIU.ipynb', 'repo_name': 'TimKam/py-ciu', 'size': '3300'}