WRITE · TRACE · UNDERSTAND

Project: fit, select, refit and serialize a ridge workflow

Rows contain unique string id, numeric feature list x and target y.

Read lesson: Project: fit, select, refit and serialize a ridge workflow →

Your experiment

Rows contain unique string id, numeric feature list x and target y. Require three nonempty identity-disjoint finite rectangular partitions and positive finite penalties. For each penalty, fit population means/stds and centered-target ridge only on train; constant-feature scale is one. Select minimum validation MSE, smaller penalty on an exact tie. Refit preprocessing and ridge on train+validation, then evaluate test once. Return alpha, validation_mse, test_mse, predictions, fit_ids and a JSON-compatible artifact with means, scales, coefficients and unpenalized intercept. Solve (Z.T@Z+alpha*I)w=Z.T@(y-mean_y).

Python runs in your browser. Your draft stays on this device.

What to look for

Review every fitted statistic's provenance. Reproduce predictions after JSON serialization, test constant columns, poison held-out labels and explain how dataset/schema versions would enter a real artifact registry. Replay follows the main orchestration function; helper computations execute and are checked separately.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Project: fit, select, refit and serialize a ridge workflow works

The idea

Ridge regression is least squares with a penalty alpha on the size of the coefficients, which keeps them stable when features are correlated or data is scarce. The strength of the penalty cannot be learned from the rows used for fitting, so it has to be chosen on data the fit did not see. That choice is where leaks creep in.

ridge_workflow(train, validation, test, alphas) keeps the three roles apart. Means, scales and coefficients come from train only, each alpha is scored on validation, and the best one wins. The pipeline is then refitted on train plus validation, and test is used exactly once, for the final error. The returned artifact holds everything needed to predict again: means, scales, coefficients and intercept.

How it works

  1. Check the three partitions: nonempty, unique string IDs across all of them, equal feature width, finite values, positive penalties.
  2. For each alpha, standardise with the train means and population standard deviations, using a scale of one for a constant feature.
  3. Solve (Z.T @ Z + alpha * I) w = Z.T @ (y - mean_y), keeping the mean of y as an unpenalised intercept.
  4. Score each fit by mean squared error on validation and choose the lowest, with exact ties going to the smaller alpha.
  5. Refit the scaling and the ridge on train plus validation with the chosen alpha.
  6. Predict test once, report test_mse, and return the artifact as plain lists and floats.

What it costs

Per fit
O(n·d² + d³)
Fits
one per alpha, plus one
Memory
O(n·d + d²)

Each fit builds a d by d matrix from n rows and solves it, and the workflow repeats that once per candidate penalty and once more for the refit.

Where it is used

  • scikit-learn's RidgeCV chooses alpha by cross-validation and then refits on all the training data it was given.
  • scikit-learn's Pipeline exists so that a scaler is fitted on the training folds only, which prevents this kind of leak.
  • Genomic prediction in plant and animal breeding uses ridge regression (RR-BLUP) because there are far more genetic markers than samples.

Watch out for

  • Computing means and standard deviations over all rows before splitting, which lets validation and test values shape the features.
  • Choosing alpha by test error, which makes the reported test score optimistic because the test set took part in selection.
  • Penalising the intercept or skipping standardisation, so the penalty falls unevenly on features depending on their units.