Train one model per group

Fit a penguin body-mass model for each species, join each held-out bird to its model, and compare predictions with a species-average baseline.

In the Palmer penguins data, a Gentoo and an Adélie can have similar bill lengths and very different body masses. If you know the species at prediction time, you can fit a separate relationship for each group. Here you will use GROUP BY to train the models and a join to choose which one scores each bird.

Run the three examples in order in one DuckDB session after installing duckboost. The tables are temporary. The measurements come from the Palmer penguins dataset.

Set aside birds from each species

Keep birds with a recorded mass and all three measurements. Stratify the split by species so each model has both training birds and a held-out group to score.

LOAD duckboost;

CREATE TEMP TABLE birds AS
SELECT species, body_mass_g::DOUBLE AS mass_g,
       [bill_length_mm, bill_depth_mm, flipper_length_mm]::DOUBLE[] AS features
FROM read_csv('https://raw.githubusercontent.com/allisonhorst/palmerpenguins/master/inst/extdata/penguins.csv', nullstr = 'NA')
WHERE species IS NOT NULL AND body_mass_g IS NOT NULL
  AND bill_length_mm IS NOT NULL AND bill_depth_mm IS NOT NULL
  AND flipper_length_mm IS NOT NULL;

CREATE TEMP TABLE sampled AS
FROM duckboost_initial_split('birds', prop := 0.8, strata := species, seed := 42);

SELECT species,
       count(*) FILTER (WHERE split = 'train') AS training_birds,
       count(*) FILTER (WHERE split = 'test') AS test_birds
FROM sampled
GROUP BY species
ORDER BY species;
┌───────────┬────────────────┬────────────┐
│  species  │ training_birds │ test_birds │
├───────────┼────────────────┼────────────┤
│ Adelie    │ 121            │ 30         │
│ Chinstrap │ 54             │ 14         │
│ Gentoo    │ 98             │ 25         │
└───────────┴────────────────┴────────────┘

Save the model beside its group key

duckboost_train is an aggregate. Grouping by species produces one row per species, with its fitted model and the average training mass. That mean gives you a baseline prediction for a new bird when all you know is its species.

CREATE TEMP TABLE fits AS
SELECT species,
       avg(mass_g) AS baseline_g,
       duckboost_train(mass_g, features, MAP {
         'n_estimators': '40',
         'max_depth': '2',
         'learning_rate': '0.1'
       }) AS model
FROM sampled
WHERE split = 'train'
GROUP BY species;

SELECT species, round(baseline_g) AS mean_training_mass_g
FROM fits
ORDER BY species;
┌───────────┬──────────────────────┐
│  species  │ mean_training_mass_g │
├───────────┼──────────────────────┤
│ Adelie    │ 3674.0               │
│ Chinstrap │ 3744.0               │
│ Gentoo    │ 5078.0               │
└───────────┴──────────────────────┘

Score the holdout against both predictions

Join on species. Every test bird receives the fitted model and mean for its own species. A left join also keeps a bird visible if its species is missing from the model table. In that case its prediction is NULL, and the difference between the two row counts exposes the gap.

WITH scored AS (
  SELECT b.species, b.mass_g, f.baseline_g,
         CASE WHEN f.model IS NOT NULL
              THEN duckboost_predict(f.model, b.features)
         END AS predicted_g
  FROM sampled AS b
  LEFT JOIN fits AS f USING (species)
  WHERE b.split = 'test'
)
SELECT species, count(*) AS test_birds, count(predicted_g) AS scored_birds,
       round(avg(abs(mass_g - baseline_g)), 1) AS baseline_mae_g,
       round(avg(abs(mass_g - predicted_g)), 1) AS model_mae_g
FROM scored
GROUP BY species
ORDER BY species;
┌───────────┬────────────┬──────────────┬────────────────┬─────────────┐
│  species  │ test_birds │ scored_birds │ baseline_mae_g │ model_mae_g │
├───────────┼────────────┼──────────────┼────────────────┼─────────────┤
│ Adelie    │ 30         │ 30           │ 312.1          │ 217.7       │
│ Chinstrap │ 14         │ 14           │ 234.9          │ 247.1       │
│ Gentoo    │ 25         │ 25           │ 332.7          │ 250.2       │
└───────────┴────────────┴──────────────┴────────────────┴─────────────┘

Both errors are mean absolute error in grams. First check that every test bird was scored. Then compare the last two columns within each species. On this split the trees reduce error for Adélie and Gentoo. For the 14 held-out Chinstrap birds, the training mean gives a smaller error. Reporting all three comparisons makes that difference visible.

For comparison with a single model across species, the native trainer guide fits a model with a categorical feature. To carry the group-specific models into another process, see Train, predict, and export SQL.