FuseKernel Regression
FuseKernel fuses complementary kernel channels and decodes them with kernel ridge regression. This example fuses the XGBoost tree co-membership kernel with a learned multi-scale sp
# %%
# 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='YOUR_LICENSE_KEY')
# %%
# Import required modules
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import pandas as pd
from modeva import DataSet, ModelZoo, TestSuite
# %%
# Load and prepare dataset
ds = DataSet()
ds.load(name="CaliforniaHousing")
ds.set_random_split()
# %%
# Train FuseKernel
# ----------------------------------------------------------
# FuseKernel fuses complementary kernel channels -- here the XGBoost tree
# co-membership kernel and the learned multi-scale spectral kernel (MS-SKM) --
# and decodes them with kernel ridge regression. Every channel is fit on a
# support fold and the mixture weights and ridge are selected on a held-out
# query fold (``fit_method="grid"``), so the selection is leakage-free.
from modeva.models import MoFuseKernelRegressor
model = MoFuseKernelRegressor(
name="FuseKernel",
use_xgb=True, use_spectral=True, # channels to fuse
fit_method="grid", # leakage-free, query-scored
spectral_params={"H": 4, "K": 8, "kernel": "laplace", "epochs": 50},
interpret_ref_size=400, interpret_n_grid=20,
random_state=0,
)
mz = ModelZoo(dataset=ds)
mz.add_model(model)
mz.train_all()
mz.leaderboard()
# %%
# Accuracy
# ----------------------------------------------------------
# FuseKernel is a standard MoDeVa model, so the whole TestSuite applies.
ts = TestSuite(ds, model)
ts.diagnose_accuracy_table().table
# %%
# Inherent Interpretation (FANOVA)
# ----------------------------------------------------------
# With a spectral channel present, FuseKernel exposes a functional-ANOVA
# decomposition of the fused predictor through the standard ``interpret_*``
# methods.
results = ts.interpret_fi()
results.table
# %%
results = ts.interpret_ei()
results.table
# %%
results = ts.interpret_effects(features="MedInc")
results.plot(results.get_figure_names()[0])
# %%
results = ts.interpret_effects(features="Latitude")
results.plot(results.get_figure_names()[0])
# %%
results = ts.interpret_local_fi(sample_index=1)
results.table
# %%
# Channel Decomposition
# ----------------------------------------------------------
# The fused prediction is an exact additive sum over channels.
# ``channel_contributions`` returns each channel's contribution in target units.
result = model.channel_contributions(ds.test_x)
result.table
# %%
# Inherent GP Predictive Uncertainty
# ----------------------------------------------------------
# For regression the prediction intervals are the closed-form Gaussian-process
# posterior of the fused kernel, so ``diagnose_reliability`` reflects the
# model's own uncertainty.
ts.diagnose_reliability().table
# %%
mean, var = model.predict_dist(ds.test_x)
intervals = model.predict_interval(ds.test_x) # (n, 2) at the calibrated level
pd.DataFrame({"mean": mean[:5], "std": np.sqrt(var)[:5],
"low": intervals[:5, 0], "high": intervals[:5, 1]})
# %%
# Weakness Localization
# ----------------------------------------------------------
# Slicing surfaces low-accuracy regions of input space.
ts.diagnose_slicing_accuracy(features="MedInc").table