Open In Colab

Mixture of Expert (MoE) Regression

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 MoMoERegressor, MoXGBRegressor

Load and prepare dataset for regression

ds = DataSet()
ds.load(name="BikeSharing")
ds.set_random_split()
ds.set_target("cnt")

ds.scale_numerical(features=("cnt",), method="log1p")
ds.preprocess()

Train models

model = MoMoERegressor(max_depth=2)
model.fit(ds.train_x, ds.train_y)
MoMoERegressor(base_score=None, booster=None, callbacks=None,
               colsample_bylevel=None, colsample_bynode=None,
               colsample_bytree=None, device=None, early_stopping_rounds=None,
               enable_categorical=False, eval_metric=None, feature_types=None,
               gamma=None, grow_policy=None, importance_type=None,
               interaction_constraints=None, learning_rate=None, max_bin=None,
               max_cat_threshold=None, max_cat_to_onehot=None,
               max_delta_step=None, max_depth=2, max_leaves=None,
               min_child_weight=None, missing=nan, monotone_constraints=None,
               multi_strategy=None, n_estimators=None, n_jobs=None,
               name='MoMoERegressor', num_parallel_tree=None, ...)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


Basic accuracy analysis

ts = TestSuite(ds, model)
results = ts.diagnose_accuracy_table()
results.table
MSE MAE R2
train 0.0921 0.2109 0.9541
test 0.1145 0.2320 0.9434
GAP 0.0224 0.0211 -0.0107


Local MOE weights interpretation

results = ts.interpret_local_moe_weights()
results.plot()


Data drift test between cluster “1” with the rest samples

results = ts.interpret_moe_cluster_analysis()
data_results = ds.data_drift_test(**results.value[2]["data_info"],
                                  distance_metric="PSI",
                                  psi_method="uniform",
                                  psi_bins=10)
data_results.plot("summary")


Interpret feature importance

results = ts.interpret_fi()

Expert No. 0

results.plot("0")


Expert No. 2

results.plot("2")


Interpret effect importance

results = ts.interpret_ei()

Expert No. 0

results.plot("0")


Expert No. 2

results.plot("2")


Interpret effects

results = ts.interpret_effects(features="hr")

Expert No. 0

results.plot("0")


Expert No. 2

results.plot("2")


Expert of all clusters

results.plot("all")


Cluster performance analysis

results = ts.interpret_moe_cluster_analysis()
results.plot()


Distribution difference summary between cluster-0 with the rest

data_results = ds.data_drift_test(**results.value[0]["data_info"],
                                  distance_metric="PSI",
                                  psi_method="uniform",
                                  psi_bins=10)
data_results.plot("summary")


Distributional difference for hr between cluster-0 with the rest

data_results.plot(("density", "hr"))


Local feature importance analysis

results = ts.interpret_local_fi(dataset='train', sample_index=1)

Expert No. 0

results.plot("0")


Expert No. 2

results.plot("2")


Local effect importance analysis

results = ts.interpret_local_ei(dataset='train', sample_index=1)

Expert No. 0

results.plot("0")


Expert No. 2

results.plot("2")


Total running time of the script: (0 minutes 5.157 seconds)

Gallery generated by Sphinx-Gallery