skore lets you extend the built-in automated checks with your own.
This example shows how to write a custom check function and register it
with a report via add().
We start by defining a simple check that flags models with a very large
number of features. The check inspects the test data attached to the
report. We throw an exception when the test data is not available to avoid
running the check when it is not applicable. The check function is wrapped in a
Check instance and registered with the report via
add().
The docs_url argument is optional. When provided as a full URL (starting
with "http"), it is used as-is. When it is a plain anchor string
it points to the skore automated checks user guide. When omitted entirely,
no documentation link is shown.
We set the severity to “tip” to indicate that this is not an issue to fix,
but a cautionary note about the dataset. Severity can also be set to “issue” to
indicate when there is an issue to fix.
importnumpyasnpfromskoreimportCheck,CheckNotApplicableclassCustomCheck1(Check):code="CSTM001"title="High feature count"report_types=["estimator"]severity="tip"docs_url="https://scikit-learn.org/stable/modules/feature_selection.html#feature-selection"defcheck_function(self,report):"""Flag when the number of features exceeds a threshold."""ifreport.X_testisNone:raiseCheckNotApplicable()n_features=report.X_test.shape[1]ifn_features>50:return(f"The dataset has {n_features} features which may hurt model performance. ""Consider feature selection or dimensionality reduction.")returnNone
[SKD001] Potential overfitting. Significant train/test gaps were found for 3/4 default predictive metrics.
[SKD006] Coefficient interpretation. Features are not on the same scale: coefficient magnitudes are not directly comparable as feature importance.
[SKD011] Golden feature. A model trained on feature(s) ['feature_0'] alone has similar performance to a model trained on all the features, on the default predictive metrics. This may signal data leakage or excessive reliance on a single feature.
[SKD012] Useless features. Feature(s) ['feature_1', 'feature_10', 'feature_11', 'feature_12', 'feature_13', 'feature_14', 'feature_15', 'feature_16', 'feature_17', 'feature_18', 'feature_19', 'feature_2', 'feature_20', 'feature_21', 'feature_22', 'feature_23', 'feature_24', 'feature_25', 'feature_26', 'feature_27', 'feature_28', 'feature_29', 'feature_3', 'feature_30', 'feature_31', 'feature_32', 'feature_34', 'feature_35', 'feature_37', 'feature_38', 'feature_39', 'feature_4', 'feature_40', 'feature_41', 'feature_42', 'feature_43', 'feature_44', 'feature_45', 'feature_46', 'feature_47', 'feature_48', 'feature_49', 'feature_5', 'feature_50', 'feature_52', 'feature_53', 'feature_54', 'feature_55', 'feature_56', 'feature_57', 'feature_58', 'feature_59', 'feature_6', 'feature_60', 'feature_61', 'feature_62', 'feature_63', 'feature_64', 'feature_65', 'feature_66', 'feature_67', 'feature_68', 'feature_69', 'feature_7', 'feature_70', 'feature_71', 'feature_72', 'feature_73', 'feature_74', 'feature_75', 'feature_76', 'feature_77', 'feature_78', 'feature_79', 'feature_8', 'feature_9'] have permutation importance overlapping with zero and could likely be dropped without degrading performance. Dropping redundant features may also improve model performance.
[CSTM001] High feature count. The dataset has 80 features which may hurt model performance. Consider feature selection or dimensionality reduction.
The report_types attribute of Check is a list that controls which
reports the check runs on. Let’s write a check that is specific to cross-validation
reports: it flags metrics with high variance across splits. We set the severity to
“issue” to indicate that this is an issue to fix.
We will corrupt the first fold of the target to illustrate the check.
We see that our new check appears along another similar issue that detects folds that
are outliers in terms of performance metrics.
importpandasaspdy_noisy=y.copy()y_noisy[:len(y_noisy)//5]=rng.normal(size=len(y_noisy)//5)cv_report=evaluate(LinearRegression(),X,y_noisy,splitter=5)classCustomCheck2(Check):code="CSTM002"title="High score variance across CV splits"report_types=["cross-validation"]docs_url=Noneseverity="issue"defcheck_function(self,report):"""Flag high score variance across CV splits."""frames=[sub_report.metrics.summarize(data_source="test").dataforsub_reportinreport.reports_]scores=pd.concat(frames,ignore_index=True)high_var_metrics=[metric_nameformetric_name,groupinscores.groupby("metric_verbose_name")ifgroup["score"].std()>0.1]ifhigh_var_metrics:returnf"Metrics with high variance: {', '.join(high_var_metrics)}."returnNonecv_report.checks.add([CustomCheck2()])cv_report.checks.summarize()
[SKD001] Potential overfitting. Significant train/test gaps were found for 4/4 default predictive metrics.
[SKD009] Model worse than baseline. Test scores are not significantly better than a HistGradientBoosting baseline for 4/4 default predictive metrics.
[CSTM002] High score variance across CV splits. Metrics with high variance: MAE, MAPE, RMSE, R².
[SKD006] Coefficient interpretation. Features are not on the same scale: coefficient magnitudes are not directly comparable as feature importance.
[SKD011] Golden feature. A model trained on feature(s) ['feature_0'] alone has similar performance to a model trained on all the features, on the default predictive metrics. This may signal data leakage or excessive reliance on a single feature.
[SKD012] Useless features. Feature(s) ['feature_1', 'feature_10', 'feature_12', 'feature_13', 'feature_14', 'feature_15', 'feature_16', 'feature_17', 'feature_18', 'feature_2', 'feature_20', 'feature_21', 'feature_22', 'feature_23', 'feature_24', 'feature_25', 'feature_26', 'feature_27', 'feature_28', 'feature_29', 'feature_30', 'feature_31', 'feature_33', 'feature_34', 'feature_35', 'feature_36', 'feature_37', 'feature_38', 'feature_39', 'feature_4', 'feature_40', 'feature_41', 'feature_43', 'feature_44', 'feature_45', 'feature_46', 'feature_47', 'feature_48', 'feature_49', 'feature_5', 'feature_50', 'feature_52', 'feature_53', 'feature_54', 'feature_55', 'feature_56', 'feature_57', 'feature_58', 'feature_59', 'feature_6', 'feature_60', 'feature_61', 'feature_62', 'feature_64', 'feature_65', 'feature_66', 'feature_67', 'feature_68', 'feature_69', 'feature_7', 'feature_70', 'feature_71', 'feature_72', 'feature_73', 'feature_74', 'feature_75', 'feature_76', 'feature_77', 'feature_78', 'feature_79', 'feature_8', 'feature_9'] have permutation importance overlapping with zero and could likely be dropped without degrading performance. Dropping redundant features may also improve model performance.
[SKD004] High class imbalance. ML task is not binary classification. Got regression.
[SKD005] Underrepresented classes. ML task is not multiclass classification. Got regression.
[SKD007] MDI biased for high-cardinality features. Estimator is not a tree-based model: it does not have a `feature_importances_` attribute.
[SKD013] Train-test overlap in time series. No datetime column found.
[SKD014] Hyperparameters at search edge. Estimator is not a BaseSearchCV instance. Got LinearRegression.
[SKD015] Hyperparameters worth tuning. Estimator is not a BaseSearchCV instance. Got LinearRegression.
[SKD016] Estimator not tuned. No parameter to recommend for the estimator.
No checks were skipped in fast mode.
No checks were muted.
Mute a check by passing its code to ignore, e.g. .checks.summarize(ignore=['SKD001']).
Estimator-scoped checks on cross-validation and comparison reports#
Estimator-scoped checks such as CustomCheck1 are only executed on
EstimatorReport instances, not on
CrossValidationReport instances. To run the same logic on both report types,
set report_types=["estimator","cross-validation"] and adapt the check function
to handle each report type.
Comparison reports aggregate checks across their component reports.
[LinearRegression] Features are not on the same scale: coefficient magnitudes are not directly comparable as feature importance.
[SKD007] MDI biased for high-cardinality features.
[RandomForestRegressor] High-cardinality features detected: feature_0, feature_1, feature_2 (and 77 more). Mean Decrease in Impurity (MDI) importance is biased toward such features. Consider using permutation importance for a more robust alternative.
[LinearRegression, RandomForestRegressor] A model trained on feature(s) ['feature_0'] alone has similar performance to a model trained on all the features, on the default predictive metrics. This may signal data leakage or excessive reliance on a single feature.