Note
Go to the end to download the full example code.
Linear Tree 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, MoGLMTreeBoostClassifier, MoNeuralTreeClassifier
Load and prepare dataset
ds = DataSet()
ds.load(name="TaiwanCredit")
ds.set_random_split()
ds.set_target("FlagDefault")
LGBM Linear Tree model
model = MoLGBMClassifier(linear_trees=True, 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()
Local feature importance analysis
results = ts.interpret_local_fi(sample_index=1, centered=True)
results.plot()
Boosted GLMTree model
model = MoGLMTreeBoostClassifier(max_depth=1, n_estimators=100,
reg_lambda=0.001, verbose=True, random_state=0)
model.fit(ds.train_x, ds.train_y.ravel())
Iteration 1 with validation loss 0.46286
Iteration 2 with validation loss 0.45391
Iteration 3 with validation loss 0.45545
Iteration 4 with validation loss 0.45813
Iteration 5 with validation loss 0.45458
Iteration 6 with validation loss 0.45238
Iteration 7 with validation loss 0.45290
Iteration 8 with validation loss 0.45338
Iteration 9 with validation loss 0.45465
Iteration 10 with validation loss 0.45425
Iteration 11 with validation loss 0.45525
Iteration 12 with validation loss 0.45535
Early stop as validation loss does not decrease for certain iterations.
Basic accuracy analysis
ts = TestSuite(ds, model)
results = ts.diagnose_accuracy_table()
results.table
Main effect plot for numerical feature
results = ts.interpret_effects(features="PAY_1")
results.plot()
Main effect plot for categorical feature
results = ts.interpret_effects(features="EDUCATION")
results.plot()
Neural Tree model with Monotonicity Constraints
modelnn = MoNeuralTreeClassifier(estimator=model,
nn_temperature=0.0001,
nn_max_epochs=20,
feature_names=ds.feature_names,
mono_increasing_list=("PAY_1",),
mono_sample_size=1000,
reg_mono=10,
verbose=True,
random_state=0)
modelnn.fit(ds.train_x, ds.train_y.ravel())
#### #### MoNeuralTree Training Stage 1: Use Fitted MoGLMTreeBoost ####
#### MoNeuralTree Training Stage 2: Fine-tuning via Gradient Descent ####
Initial training and validation loss: 0.4342 and 0.4552
Epoch 0: Train loss 14.5726, Validation loss 9.1768, Monotonicity loss 0.0592
Epoch 1: Train loss 5.2107, Validation loss 1.3408, Monotonicity loss 0.0150
Epoch 2: Train loss 4.4272, Validation loss 3.1641, Monotonicity loss 0.0101
Epoch 3: Train loss 6.2526, Validation loss 8.4962, Monotonicity loss 0.0033
Epoch 4: Train loss 4.3140, Validation loss 3.2423, Monotonicity loss 0.0030
Epoch 5: Train loss 3.8045, Validation loss 10.1239, Monotonicity loss 0.0026
Epoch 6: Train loss 4.7665, Validation loss 5.4289, Monotonicity loss 0.0022
Epoch 7: Train loss 5.1385, Validation loss 5.1316, Monotonicity loss 0.0044
Epoch 8: Train loss 4.1558, Validation loss 10.4380, Monotonicity loss 0.0013
Epoch 9: Train loss 4.8965, Validation loss 2.1641, Monotonicity loss 0.0008
Epoch 10: Train loss 5.3007, Validation loss 8.5860, Monotonicity loss 0.0002
Epoch 11: Train loss 4.3431, Validation loss 3.5167, Monotonicity loss 0.0005
Epoch 12: Train loss 3.9529, Validation loss 5.5719, Monotonicity loss 0.0000
Training is terminated as validation loss stops decreasing.
Basic accuracy analysis
ts = TestSuite(ds, modelnn)
results = ts.diagnose_accuracy_table()
results.table
Feature importance analysis
results = ts.interpret_fi()
results.plot()
Main effect plot
results = ts.interpret_effects(features="PAY_1")
results.plot()
Total running time of the script: (0 minutes 24.062 seconds)