Weak-Cluster Detection and Repair (FuseKernel + MoE)

Detect the weak regions of a model with FuseKernel, then repair them with a residual-driven Mixture of Experts. FuseKernel clusters samples in its learned kernel geometry and breaks performance down per cluster (automatic weakness detection); MoMoERegressor with cluster_method="ltc" then fits a specialised expert per learning-trajectory cluster, improving accuracy most where the base model was weakest.

For fast detection the FuseKernel uses only the XGBoost tree co-membership kernel (use_rbf=False, use_spectral=False) at the base model’s depth, decoded with the Nystrom solver – no dense RBF or spectral fit is needed just to locate the weak clusters. A subsample of CaliforniaHousing keeps the example quick.

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

import warnings
warnings.filterwarnings("ignore")

import numpy as np
import pandas as pd
from sklearn.metrics import r2_score
from modeva import DataSet
from modeva.models import MoXGBRegressor, MoFuseKernelRegressor, MoMoERegressor

Load and subsample the dataset

ds = DataSet()
ds.load(name="CaliforniaHousing")
ds.set_active_samples()
sub = ds.subsample_random(dataset="main", sample_size=3000)
ds.set_active_samples(dataset="main", sample_idx=sub.value["sample_idx"])
ds.set_random_split()

Base model

A depth-3 XGBoost whose weak regions we will detect and repair.

base = MoXGBRegressor(name="base", max_depth=3, n_estimators=100,
                      learning_rate=0.1, random_state=0)
base.fit(ds.train_x, ds.train_y.ravel())
MoXGBRegressor(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,
               feature_weights=None, gamma=None, grow_policy=None,
               importance_type=None, interaction_constraints=None,
               learning_rate=0.1, max_bin=None, max_cat_threshold=None,
               max_cat_to_onehot=None, max_delta_step=None, max_depth=3,
               max_leaves=None, min_child_weight=None, missing=nan,
               monotone_constraints=None, multi_strategy=None, n_estimators=100,
               n_jobs=None, 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.


Detect weak clusters with FuseKernel

FuseKernel clusters samples in its learned kernel geometry; diagnose_weak_clusters breaks train/test performance down per cluster so the weak regions surface automatically. For fast detection we use only the tree co-membership kernel at the base model’s depth (use_rbf=False, use_spectral=False), decoded with Nystrom.

fk = MoFuseKernelRegressor(name="FuseKernel", use_xgb=True, use_rbf=False,
                           use_spectral=False, fit_method="grid", solver="nystrom",
                           gbdt_params={"n_estimators": 100, "max_depth": 3})
fk.fit(ds.train_x, ds.train_y.ravel())

diag = fk.diagnose_weak_clusters(ds, n_clusters=6)
diag.table
cluster train_n train_r2 train_rmse train_mae test_n test_r2 test_rmse test_mae
0 0 402 0.8666 0.3937 0.2790 90 0.5985 0.6785 0.4179
1 1 370 0.7497 0.3769 0.2413 109 0.6912 0.3721 0.2476
2 2 449 0.8187 0.3381 0.2344 101 0.6983 0.4525 0.3237
3 3 435 0.8606 0.4334 0.3257 93 0.7631 0.5212 0.3934
4 4 346 0.8323 0.5139 0.3898 88 0.6574 0.7856 0.6153
5 5 398 0.8226 0.3441 0.2545 119 0.7401 0.4829 0.3258
6 ALL 2400 0.8802 0.4012 0.2852 600 0.7607 0.5546 0.3780


The weakest cluster and its test-row mask.

worst = diag.value["worst_clusters"]
worst_id = int(worst.iloc[0]["cluster"])
weak_mask = np.asarray(diag.value["labels_test"]) == worst_id
print(f"weak cluster = {worst_id}, test samples in it = {int(weak_mask.sum())}")
worst
weak cluster = 0, test samples in it = 90
cluster train_n train_r2 train_rmse train_mae test_n test_r2 test_rmse test_mae
0 0 402 0.8666 0.3937 0.2790 90 0.5985 0.6785 0.4179
4 4 346 0.8323 0.5139 0.3898 88 0.6574 0.7856 0.6153
1 1 370 0.7497 0.3769 0.2413 109 0.6912 0.3721 0.2476


Repair with a residual-driven Mixture of Experts

MoMoERegressor with cluster_method="ltc" fits a baseline, clusters samples by their learning trajectories (where the base model struggles), and fits a specialised expert per cluster with a gate routing between them.

moe = MoMoERegressor(name="MoE-repair", n_clusters=5, cluster_method="ltc",
                     expert="xgboost", max_depth=3, n_estimators=100, random_state=0)
moe.fit(ds.train_x, ds.train_y.ravel())
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,
               feature_weights=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=3,
               max_leaves=None, min_child_weight=None, missing=nan,
               monotone_constraints=None, multi_strategy=None, n_clusters=5,
               n_estimators=100, n_jobs=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.


Before vs after

The repair helps overall, and most on the FuseKernel-detected weak cluster.

Xte, yte = np.asarray(ds.test_x), np.asarray(ds.test_y).ravel()

def r2(model, mask=None):
    p = model.predict(Xte)
    if mask is None:
        mask = np.ones(len(yte), dtype=bool)
    return round(r2_score(yte[mask], p[mask]), 4)

pd.DataFrame({
    ("overall", "base"):                          {"R2": r2(base)},
    ("overall", "MoE repair"):                     {"R2": r2(moe)},
    (f"weak cluster {worst_id}", "base"):          {"R2": r2(base, weak_mask)},
    (f"weak cluster {worst_id}", "MoE repair"):    {"R2": r2(moe, weak_mask)},
}).T
R2
overall base 0.7314
MoE repair 0.7711
weak cluster 0 base 0.5912
MoE repair 0.6868


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

Gallery generated by Sphinx-Gallery