Note
Go to the end to download the full example code.
Tree Ensemble Models (Classification)
Installation
# To install the required package, use the following command:
# !pip install modeva
Authentication
# To get authentication, use the following command: (To get full access please replace the token to your own token)
# from modeva.utils.authenticate import authenticate
# authenticate(auth_code='eaaa4301-b140-484c-8e93-f9f633c8bacb')
Import required modules
from modeva import DataSet
from modeva import TestSuite
from modeva.models import (MoLGBMClassifier,
MoXGBClassifier,
MoCatBoostClassifier,
MoGradientBoostingClassifier,
MoRandomForestClassifier)
Load and prepare dataset
ds = DataSet()
ds.load(name="TaiwanCredit")
ds.set_random_split()
ds.set_target("FlagDefault")
Train model
You may replace the model by anyone of the following, including MoGradientBoostingClassifier, MoRandomForestClassifier, MoXGBClassifier, MoCatBoostClassifier
model = MoLGBMClassifier(max_depth=2, verbose=-1, random_state=0)
model.fit(ds.train_x, ds.train_y.ravel())
Basic accuracy analysis
ts = TestSuite(ds, model)
results = ts.diagnose_accuracy_table()
results.table
Feature importance analysis
results = ts.interpret_fi()
results.plot(n_bars=10)
Effect importance analysis
results = ts.interpret_ei()
results.plot(n_bars=10)
Local feature importance analysis
results = ts.interpret_local_fi(dataset='train', sample_index=1, centered=True)
results.plot(n_bars=10)
Local effect importance analysis
results = ts.interpret_local_ei(dataset='train', sample_index=1)
results.plot(n_bars=10)
Main effect plot
For numerical feature
results = ts.interpret_effects(features="PAY_1")
results.plot()
Extract the detail information of the effect, e.g., split points and values.
results.value["Details"]
{'PAY_1': {'fidx': (5,), 'values': array([-1.0534514 , -1.16997403, -0.61438626, 0.38886216, 0.33472527,
0.24835294]), 'splits': [array([ -inf, -1.00000002e-35, 1.00000002e-35, 1.50000000e+00,
3.50000000e+00, 4.50000000e+00, inf])], 'right_inclusive': True}, 'PAY_6': {'fidx': (10,), 'values': array([-0.01286074, 0.0764496 , -0.05644409]), 'splits': [array([ -inf, 1.00000002e-35, 3.50000000e+00, inf])], 'right_inclusive': True}, 'LIMIT_BAL': {'fidx': (0,), 'values': array([ 0.32395616, 0.2880562 , 0.23693366, 0.24081257, 0.1242532 ,
0.11563613, 0.01055193, -0.02299061, -0.03081845, -0.05087281]), 'splits': [array([ -inf, 25000., 45000., 75000., 95000., 125000., 145000.,
155000., 285000., 305000., inf])], 'right_inclusive': True}, 'BILL_AMT1': {'fidx': (11,), 'values': array([-0.03576016, -0.00935902, 0.09906073, 0.0790521 , 0.13835849,
0.10284346, 0.14139967, 0.12624014, 0.10861737, 0.07145943,
0.04787676, 0.04199568, 0.02953576, 0.00354041, 0.01641528,
0.05408157, 0.0590748 ]), 'splits': [array([ -inf, -1.00000002e-35, 2.77488155e+00, 2.89376030e+00,
2.99629270e+00, 3.02180920e+00, 3.34879130e+00, 3.84176620e+00,
3.93374030e+00, 3.94615745e+00, 4.18843615e+00, 4.21968930e+00,
4.27368400e+00, 4.29991030e+00, 5.03414900e+00, 5.04367255e+00,
5.40019150e+00, inf])], 'right_inclusive': True}, 'BILL_AMT2': {'fidx': (12,), 'values': array([ 0.10091232, -0.04900599, -0.00214606, 0.04257239, 0.07686822]), 'splits': [array([ -inf, -2.0588013 , 4.6097598 , 5.20135065, 5.3489307 ,
inf])], 'right_inclusive': True}, 'BILL_AMT3': {'fidx': (13,), 'values': array([-0.04677684, -0.01265278, -0.04687064, -0.03267184, 0.03566952,
0.04341139, 0.08114306, 0.09737877, 0.12260411, 0.15647769,
0.27079123, 0.30592314]), 'splits': [array([ -inf, 1.00000002e-35, 4.19129730e+00, 4.27521925e+00,
4.67514110e+00, 4.77373500e+00, 4.77944850e+00, 5.04415915e+00,
5.33446600e+00, 5.35396525e+00, 5.39070830e+00, 5.51115655e+00,
inf])], 'right_inclusive': True}, 'PAY_AMT1': {'fidx': (17,), 'values': array([ 0.07084209, 0.04034364, 0.02922558, 0.00996485, -0.01358633,
-0.00676261, -0.0349187 , 0.07045835, 0.01075141, -0.02214641,
-0.04500528, -0.0578455 , -0.07412234, 0.0728663 , 0.01136341,
-0.0287415 , -0.06786967]), 'splits': [array([ -inf, 1.00000002e-35, 8.74094000e-01, 2.94448235e+00,
3.00881300e+00, 3.17623590e+00, 3.20452690e+00, 3.26611415e+00,
3.47719370e+00, 3.66048595e+00, 3.69050610e+00, 3.75308490e+00,
3.86308480e+00, 4.19509665e+00, 4.38260215e+00, 4.40198600e+00,
4.83255660e+00, inf])], 'right_inclusive': True}, 'PAY_AMT2': {'fidx': (18,), 'values': array([ 0.14818567, 0.0977724 , 0.07496113, 0.04027566, 0.03114211,
0.00813082, -0.02643381, -0.15991107, -0.18238135, -0.24355615]), 'splits': [array([ -inf, 3.1765252 , 3.2149762 , 3.30135555, 3.69910025,
3.77103605, 4.00889805, 4.14935775, 4.17617815, 4.2861982 ,
inf])], 'right_inclusive': True}, 'PAY_AMT3': {'fidx': (19,), 'values': array([ 0.08127522, 0.07458042, 0.05490845, 0.0198504 , 0.01355191,
0.00178131, -0.01815329, -0.02270906, -0.0345931 , -0.06555223,
-0.08572601]), 'splits': [array([ -inf, 1.00000002e-35, 2.65801040e+00, 2.83600735e+00,
2.90417395e+00, 2.93043925e+00, 2.95640850e+00, 3.38810125e+00,
3.39820050e+00, 3.65340550e+00, 3.84522210e+00, inf])], 'right_inclusive': True}, 'EDUCATION': {'fidx': (2,), 'values': array([-0.45725397, 0.00387936, -0.01658904]), 'splits': [array([ -inf, 1.00000002e-35, 2.50000000e+00, inf])], 'right_inclusive': True}, 'PAY_AMT4': {'fidx': (20,), 'values': array([ 0.05424298, 0.02541088, -0.00441816, -0.03026423]), 'splits': [array([ -inf, 1.00000002e-35, 3.00065090e+00, 3.31249470e+00,
inf])], 'right_inclusive': True}, 'MARRIAGE': {'fidx': (3,), 'values': array([ 0.0423956 , -0.12718679]), 'splits': [array([-inf, 1.5, inf])], 'right_inclusive': True}, 'AGE': {'fidx': (4,), 'values': array([-0.08920078, -0.03964159, 0.01700743, 0.04028322]), 'splits': [array([-inf, 35.5, 39.5, 45.5, inf])], 'right_inclusive': True}, 'PAY_5': {'fidx': (9,), 'values': array([-0.15075029, 0.01901355]), 'splits': [array([ -inf, 1.00000002e-35, inf])], 'right_inclusive': True}, 'BILL_AMT4': {'fidx': (14,), 'values': array([-0.01398571, 0.02474484, 0.03286639, 0.04270729, 0.05822533,
0.08875921, 0.18554294]), 'splits': [array([ -inf, 4.09817635, 4.69402145, 4.7208288 , 5.1276295 ,
5.33973635, 5.4496842 , inf])], 'right_inclusive': True}, 'SEX': {'fidx': (1,), 'values': array([ 0.04318783, -0.04318783]), 'splits': [array([-inf, 1.5, inf])], 'right_inclusive': True}, 'BILL_AMT5': {'fidx': (15,), 'values': array([-0.00471474, -0.02418091, -0.00817011, 0.13564104]), 'splits': [array([ -inf, 3.8332428, 4.9599828, 5.390088 , inf])], 'right_inclusive': True}, 'BILL_AMT6': {'fidx': (16,), 'values': array([-0.01883298, 0.04975605, 0.05801912]), 'splits': [array([ -inf, 2.89070025, 4.477859 , inf])], 'right_inclusive': True}, 'PAY_AMT6': {'fidx': (22,), 'values': array([ 0.04949369, 0.09821501, 0.07463474, 0.01185624, -0.05536766,
-0.17904737, -0.23254459]), 'splits': [array([ -inf, 0.874094 , 2.9355069 , 3.98238435, 3.99578865,
4.297246 , 4.7525315 , inf])], 'right_inclusive': True}, 'PAY_2': {'fidx': (6,), 'values': array([-0.07558263, 0.1635372 , 0.09918538, -0.02073921]), 'splits': [array([-inf, 1.5, 2.5, 3.5, inf])], 'right_inclusive': True}, 'PAY_3': {'fidx': (7,), 'values': array([-0.26863568, -0.19129001, 0.13112096, 0.06531028]), 'splits': [array([ -inf, -1.00000002e-35, 1.50000000e+00, 3.50000000e+00,
inf])], 'right_inclusive': True}, 'PAY_AMT5': {'fidx': (21,), 'values': array([-0.01371426, 0.05253822]), 'splits': [array([ -inf, 4.4334417, inf])], 'right_inclusive': True}, 'PAY_4': {'fidx': (8,), 'values': array([-0.1752667 , -0.12244993, 0.02204623]), 'splits': [array([ -inf, -1.00000002e-35, 1.00000002e-35, inf])], 'right_inclusive': True}}
Main effect plot for categorical feature
For categorical feature
results = ts.interpret_effects(features="EDUCATION")
results.plot()
For 2 features
results = ts.interpret_effects(features=("PAY_1", "PAY_2"))
results.plot()
Total running time of the script: (0 minutes 1.837 seconds)