Open In Colab

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,
               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, objective='reg:squarederror', ...)
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.866582 0.393692 0.278982 90 0.598540 0.678469 0.417868
1 1 370 0.749699 0.376925 0.241336 109 0.691150 0.372062 0.247648
2 2 449 0.818745 0.338055 0.234356 101 0.698334 0.452472 0.323724
3 3 435 0.860552 0.433365 0.325704 93 0.763069 0.521225 0.393364
4 4 346 0.832316 0.513890 0.389757 88 0.657381 0.785572 0.615349
5 5 398 0.822622 0.344135 0.254463 119 0.740075 0.482943 0.325783
6 ALL 2400 0.880246 0.401242 0.285202 600 0.760667 0.554562 0.377999


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.866582 0.393692 0.278982 90 0.598540 0.678469 0.417868
4 4 346 0.832316 0.513890 0.389757 88 0.657381 0.785572 0.615349
1 1 370 0.749699 0.376925 0.241336 109 0.691150 0.372062 0.247648


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,
               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,
               name='MoE-repair', ...)
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.7731
weak cluster 0 base 0.5912
MoE repair 0.6823


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

Gallery generated by Sphinx-Gallery