Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/ai-and-statistics/matlab-classify-tabular-data

matlab-classify-tabular-data

Unverified

Use this skill to classify tabular data end-to-end in MATLAB — load a dataset, prepare and clean it, select promising classifiers, train them, and compare accuracies with cross-validation, holdout, or hyperparameter optimization plus statistical tests. TRIGGER when: user asks to classify tabular data, pick classifiers for a dataset, compare classifier accuracy, run cross-validation or a holdout evaluation, or find the best model with statistical uncertainty. DO NOT TRIGGER when: user has non-tabular inputs (images, sequences, time series), wants a regression model, is training a specific neural network architecture (use matlab-train-network), or wants cost-sensitive learning or an arbitrary class-prior vector (this skill only supports the built-in uniform-prior toggle for imbalanced data).

FileKindSize
SKILL.mdskill md36.1 KB
manifest.yamlconfig524 B
references/README.mdmarkdown3.1 KB
references/dataprep.mdmarkdown7.5 KB
references/hpo.mdmarkdown11.5 KB
references/save-and-export.mdmarkdown7.0 KB
references/select-classifiers-imbalanced.mdmarkdown3.0 KB
references/select-classifiers.mdmarkdown9.4 KB
scripts/README.mdmarkdown2.3 KB
scripts/helpers/aggregate_nested_cv_loss.mother470 B
scripts/helpers/apply_preproc.mother4.1 KB
scripts/helpers/build_model_definitions.mother16.6 KB
scripts/helpers/compute_data_flags.mother2.8 KB
scripts/helpers/compute_pairwise_pvalues_cv.mother623 B
scripts/helpers/compute_pairwise_pvalues_holdout.mother556 B
scripts/helpers/count_categorical_levels.pbinary376 B
scripts/helpers/export_workflow_script.mother15.2 KB
scripts/helpers/find_zero_variance_columns.mother810 B
scripts/helpers/is_bag_of_tokens.mother181 B
scripts/helpers/make_holdout_split.mother387 B
scripts/helpers/plot_accuracy_bars.mother994 B
scripts/helpers/plot_boosting_curve.mother1.2 KB
scripts/helpers/plot_pvalue_heatmap.mother465 B
scripts/helpers/plot_training_time.mother797 B
scripts/helpers/resolve_recipe.mother1.1 KB
scripts/helpers/resume_boosting_cv.mother543 B
scripts/helpers/resume_boosting_holdout.mother448 B
scripts/helpers/retrain_with_hpo.mother2.7 KB
scripts/helpers/retrain_with_resume.mother2.5 KB
scripts/helpers/sanitize_model_name.mother799 B
scripts/helpers/save_selected_models.mother4.0 KB
scripts/helpers/score_holdout_test.mother278 B
scripts/helpers/stratified_subsample.pbinary367 B
scripts/helpers/train_and_score_cv.mother405 B
scripts/helpers/train_and_score_holdout.mother1.1 KB
scripts/model_catalog/branch_categorical.mother4.2 KB
scripts/model_catalog/branch_many_missing.mother3.1 KB
scripts/model_catalog/branch_regular.mother4.1 KB
scripts/model_catalog/branch_sparse.mother1.3 KB
scripts/model_catalog/branch_wide.mother2.0 KB
scripts/model_catalog/classifier_branches.mother2.2 KB
scripts/model_catalog/classifier_thresholds.mother2.2 KB
scripts/model_catalog/imbalanced_boosting.mother3.0 KB
scripts/model_catalog/model_recipe.mother3.5 KB

references/save-and-export.md

View on GitHub
# Save Trained Models and Export a Retraining Workflow Script

This reference is dispatched from the orchestrator after Step 12 (Summary) has been delivered to the user. It covers the two optional wrap-up steps:

- **Step 13** — save any of the trained models to a `.mat` file for later reuse (`save_selected_models`).
- **Step 14** — emit a self-contained MATLAB script that reproduces the entire workflow on new data (`export_workflow_script`). Only offered if the user saved at least one model in Step 13.

Present the two decisions in order. Do not read this reference until Step 12 has been reported, because the user's answer to "which models should I save?" depends on the per-model accuracy table they just saw.

## Step 13 — save trained models (optional)

Ask the user whether they want to save any of the trained models to a `.mat` file for later reuse.

Present the full model list with a suggested default (top-tier models), and let the user pick freely:

> Would you like to save any of the trained models to a `.mat` file? You can pick individual models, "top" for the top-tier list, "all", or "none".
>
> **Top tier:** [top-tier model names]
> **All models:** [full model list]

If the user says "none", skip to Step 14.

If the user picks one or more models, ask about the save form and location.

### Save form

Ask the user:

> Save as **compact** (deployment-ready, smaller file — recommended) or **full** (keeps training data reference, supports resubstitution loss and CV)? (compact / full)

Default is compact. Record the answer as `useCompact` (logical). The helper calls `compact()` on every model that supports it when `useCompact = true`.

### Save location

Propose a default and let the user override:

> I'll save these to `<default-path>`. Enter a different path to override, or press Enter to accept.

**Default path.** Use `fullfile(pwd, sprintf('classification_models_%s.mat', string(datetime('now', 'Format', 'yyyyMMdd_HHmmss'))))`. Do not write anywhere outside the user's current working folder without asking.

### Call the helper

```matlab
metadata = struct( ...
    'evalPath',        evalPath, ...          % 'cv' or 'holdout'
    'flags',           flags, ...
    'interpretability',interpretability, ...
    'useUniformPrior', useUniformPrior, ...
    'modelNames',      {modelNames}, ...
    'accuracy',        acc, ...
    'accuracyCI',      accCI);

savedPath = save_selected_models(selectedNames, modelDefs, trainedModels, ...
    X, Y, savePath, metadata, useCompact);
fprintf('Saved %d model(s) to %s\n', numel(selectedNames), savedPath);
```

- `trainedModels` is a cell array aligned with `modelDefs`. On the holdout path each cell holds the trained model returned by `train_and_score_holdout`; on the CV path each cell holds the cross-validated model returned by `train_and_score_cv`. The helper detects CV-wrapped models and **retrains them on the full `(X, Y)` via `modelDefs(k).fitFcn`** so the saved model is directly usable via `predict(loadedModel, XNew)`.
- On the CV path, pass the full `X` and `Y` as arguments 4 and 5. On the holdout path, pass `XTrain` and `YTrain` — but note the helper only uses them when a cell contains a CV wrapper, which the holdout path never produces.
- The helper sanitizes model names for `save`: `LinearSVM-OVO` becomes the variable name `LinearSVM_OVO`. The original name is preserved in `metadata.modelNameMap`.

## Step 14 — export retraining workflow script (optional)

If the user saved models in Step 13, offer to generate a self-contained MATLAB script that reproduces the entire workflow on new data drawn from the same distribution:

> Would you like me to generate a script that retrains these models on new data (same columns, same class labels)? The script will replay the data-preparation steps you chose, re-fit the models, and save them. (yes / no)

If the user says no, the workflow is complete.

If yes, propose a default script path and let the user override it, then call the bundled helper:

```matlab
config = struct( ...
    'skillPath',            skillPath, ...
    'selectedNames',        {selectedNames}, ...
    'preproc',              preproc, ...
    'interpretability',     interpretability, ...
    'useUniformPrior',      useUniformPrior, ...
    'hpoModels',            {hpoModelNames}, ...       % {} if none
    'resumeCycles',         resumeCycles, ...          % struct() if none
    'useCompact',           useCompact, ...
    'modelSavePath',        modelSavePath, ...
    'datasetLabel',         datasetLabel);

scriptPath = export_workflow_script(scriptPath, config);
fprintf('Workflow script written to %s\n', scriptPath);
```

`flags` from Step 2 is intentionally NOT part of `config` — the exported script re-runs `compute_data_flags` on the new data so branch dispatch reflects whatever the retraining input actually looks like.

**The imbalanced boosting overlay is auto-detected — you normally omit `useImbalancedOverlay`.** The emitted script recomputes `flags` on the new data and rebuilds the overlay whenever `flags.isImbalanced` is true (the runtime analogue of the selection-time gate in `select-classifiers-imbalanced.md`), so on same-distribution new data the overlay-only recipes (`RoughRUSBoost`, `RoughAdaBoost`, …) resolve in `modelDefs` automatically — even when `selectedNames` includes them. The RUSBoost `smallestClassSize` gate is re-evaluated on the new data by the overlay itself, so a new dataset whose minority class is too small will simply drop RUSBoost during the normal filter (as at selection time).

`useImbalancedOverlay` is an optional **force-on override** (default `false`): the emitted line is `useImbalancedOverlay = <config> || flags.isImbalanced`. Set it `true` only to pin the overlay on regardless of the recomputed flags — e.g. the interactive run applied it and you want it kept even if the new data happens to read as balanced. For the ordinary same-distribution retraining case, leave it unset.

### What the emitted script does

The generated `.m` loads new data (`Xnew`, `Ynew`) from a `.mat` file the user supplies, replays every recorded `preproc` op via `apply_preproc`, recomputes `flags` on the new data, rebuilds `modelDefs` via the same `build_model_definitions` call (with the original `interpretability` and `useUniformPrior`), and trains only the selected models. Models on `hpoModelNames` are re-trained with **single-shot HPO on the full training set** — not the nested-CV design from Step 11 — because nested CV does not produce a single deployable parameter set. The emitted script includes a note surfacing this to the user, and reminds them that the interactive-run accuracy is a slight overestimate for this configuration.

### What the emitted script does not carry over

Stratified subsampling from Step 5 (a training-speed hack, not a data-preparation decision), and interactive Step 10 "resume with more trees" decisions unless captured in `resumeCycles`. Everything else — feature omissions, class removals, class merges, zero-variance drops — replays exactly.

---

Copyright 2026 The MathWorks, Inc.
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 321kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 321kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 321kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 197kUpdated Claude CodeCodexVerifiedInstall