Linear Regression
TYPE 'linear' fits a least-squares regression with optional L2 regularization (ridge) via batch gradient descent. The first column of the training SELECT is the continuous label; remaining columns are features. The trainer iterates training iterations over the data source until the per-iteration weight delta falls below tolerance or max_iters is reached.
When to use it
- Continuous targets with roughly linear relationships to the features.
- A baseline you want to beat before reaching for trees or DNNs.
- An interpretable model where the per-feature weight is the deliverable.
When NOT to use it
- Strong feature interactions or non-linearities — use engineered interaction features or DNN regression; the native tree trainers are classification-only.
- Unscaled features spanning many orders of magnitude — batch gradient descent will converge poorly without standardisation.
- Categorical features without one-hot encoding (encode categories numerically before training).
Syntax
CREATE MODEL <name>(DOUBLE, DOUBLE[, DOUBLE ...]) RETURNS DOUBLE
TYPE { 'linear' | 'linear_regression' | 'linear-regression' }
OPTIONS (...)
AS SELECT <label>, <f1>, <f2>, ... FROM <source>;
The function call signature drops the label — you pass features only at inference.
Options
| Option | Default | Type | Range | What it does |
|---|---|---|---|---|
learning_rate | 0.01 | float | > 0 | batch gradient descent step size |
l2_lambda | 0.0 | float | >= 0 | L2 regularization strength (ridge); 0 disables |
max_iters | 100 | int | >= 1 | Maximum training iterations |
tolerance | 1e-4 | float | >= 0 | Convergence threshold on weight delta between iterations |
partitions | 1 | int | >= 1 | Training partitions; see execution and memory limits |
Examples
These fragments assume the named source tables exist. Match training and inference feature order and preprocessing. For a complete dataset and runnable script, follow the linked tutorial.
Minimal:
CREATE MODEL price(DOUBLE, DOUBLE) RETURNS DOUBLE
TYPE 'linear'
AS SELECT list_price AS label, sqft, bedrooms FROM listings;
SELECT listing_id, price(sqft, bedrooms) AS estimated_price
FROM new_listings;
Tuned with ridge + extended budget:
CREATE MODEL revenue_forecast(DOUBLE, DOUBLE, DOUBLE) RETURNS DOUBLE
TYPE 'linear'
OPTIONS (
learning_rate = 0.005,
l2_lambda = 0.01,
max_iters = 1000,
tolerance = 1e-6,
partitions = 4
)
AS SELECT
revenue_usd AS label,
marketing_spend,
site_visits,
avg_order_value
FROM weekly_metrics;
Output shape
price(f1, f2, ...) returns a DOUBLE predicted value for each row.
Tuning notes
- Standardise features (z-score or min-max) before training. batch gradient descent convergence is dramatically better with comparable feature scales.
- If loss oscillates, halve
learning_rate. If it crawls, try0.05or0.1. - Add
l2_lambda(start at0.001–0.01) when feature count is high relative to row count to control overfit. - Bump
max_itersfirst ifconverged = falseis reported; otherwise relaxtolerance. - Benchmark
partitionsfor your workload; scaling is not necessarily proportional.
Convergence and quality
TrainingMetrics reports iterations and converged. converged = true means consecutive-iteration weight delta dropped under tolerance; false with iterations == max_iters means the budget ran out.
EVALUATE MODEL emits rmse and mae when the held-out query labels its first column label and the prediction is named prediction (the canonical projection from a model call). Use both — RMSE penalises large errors quadratically while MAE is robust.
Related
-
Logistic regression — same trainer shape, classification head