Regression cohorts¶
fit_profiler profiles regressors too. You pick which slice of cases to describe with
target_mask, and pass y= to add per-archetype diagnostics against the true values.
In short
- Pass
task_type=TaskType.REGRESSIONand atarget_maskfor the slice to profile, such as the top 15% of claim amounts. - With
y=, each archetype gets the mean true value, mean prediction, mean residual, MAE and RMSE. - Change the mask to ask another question: large residuals, or systematic over-prediction.
- Rules need a binary target, so they are not mined on a regressor.
In practice the model comes from ml4
This page trains a plain XGBRegressor for brevity. For a regressor built by
ml4, see Explain an ml4 model.
1. Train a regressor¶
import numpy as np
import pandas as pd
from xgboost import XGBRegressor
from sklearn.model_selection import train_test_split
df = pd.read_parquet("claims.parquet")
y = df.pop("claim_amount")
X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.2, random_state=42)
reg = XGBRegressor(n_estimators=500, max_depth=6).fit(X_train, y_train)
2. Profile the high-value tail¶
from parleml_mli import (
ClusteringConfig,
ExplanationsConfig,
PdpConfig,
PermutationImportanceConfig,
ProfilingConfig,
SpaceType,
TaskType,
fit_profiler,
)
threshold = float(np.quantile(y_train, 0.85)) # top 15% by claim amount
config = ProfilingConfig(
clustering=ClusteringConfig(space_type=SpaceType.LEAF),
explanations=ExplanationsConfig(
pdp=PdpConfig(),
permutation_importance=PermutationImportanceConfig(),
),
)
result = fit_profiler(
model=reg,
X=X_train,
y=y_train, # enables RMSE / MAE / residuals
target_mask=(y_train > threshold), # the cohort to profile
task_type=TaskType.REGRESSION,
config=config,
)
3. Per-archetype residual diagnostics¶
With y= provided and task_type=REGRESSION, the summary gains
ground-truth diagnostics:
print(result.summary_df[[
"cluster_id", "n_target_cases",
"mean_y_true", "median_y_true", "std_y_true",
"mean_prediction", "mean_residual", # signed bias
"mae", "rmse",
]])
mean_residual surfaces archetypes where the regressor is systematically biased;
rmse / mae surface where it's least reliable.
Other useful cohorts¶
Swap the target_mask to ask a different question:
y_pred = reg.predict(X_train)
# Where is the regressor most uncertain? (large absolute residuals)
result = fit_profiler(
model=reg, X=X_train, y=y_train,
target_mask=(np.abs(y_train - y_pred) > np.quantile(np.abs(y_train - y_pred), 0.9)),
task_type=TaskType.REGRESSION,
)
# Sub-types of systematic over-prediction
result = fit_profiler(
model=reg, X=X_train, y=y_train,
target_mask=((y_pred - y_train) > np.quantile(y_pred - y_train, 0.9)),
task_type=TaskType.REGRESSION,
)
Rules need a binary target
To mine rules on a regression problem, binarize via target_mask and mine
against a classifier of the same family. On a regressor, ProfilingConfig(rules=...) is
skipped with a UserWarning and result.rules_ stays None. See the
Profiling guide.