Problem 503647 · medium · Level 05 Advanced Algorithms & Graphs

Bikes, Rain and a Fair Penalty

ridge regression · regularisation · standardisation · normal equations · data leakage · multiple regression

A bike-share scheme predicts daily rentals from [temperature, humidity, wind, rain]. These are on very different scales, so a penalty on the size of the weights would fall unevenly on them. The fix is to put every feature on the same scale first.

Write ridge_predict(X_train, y_train, X_test, lam) that returns one predicted rental count (a float) per row of X_test:

  1. Scale. For each feature, compute the mean m and the standard deviation s (divide by n) over the training rows. A value v of that feature becomes z = (v - m) / s, in training and test rows alike. A feature with s = 0 becomes 0 everywhere.
  2. Fit. On the scaled training rows, find the intercept c and the weights w that minimise Σ (c + w·z_i - y_i)² + lam · Σ_j w_j². The intercept is not penalised.
  3. Predict c + w·z for every scaled test row.

The setup provides solve(A, b), which solves a system of linear equations A·x = b given as a list of rows and a list, and bike_days(n, seed), which returns (X, y) for n days of the scheme.

Examples

Input:  X_train = [[0], [2]], y_train = [1, 5], X_test = [[1], [4]], lam = 2.0
Output: [3.0, 6.0]
Explanation: the feature has mean 1 and standard deviation 1, so the training rows become -1
and 1 and the test rows 0 and 3. The best intercept is the mean rental 3, and the weight
minimises (3 - w - 1)² + (3 + w - 5)² + 2w², which gives w = 1.

Input:  X_train = [[1, 10], [2, 30], [3, 20]], y_train = [3, 7, 5], X_test = [[2, 20], [4, 40]], lam = 1.0
Output: [5.0, 8.272727272727273]

Constraints

  • 1 <= len(X_train) <= 2000, 1 <= d <= 8 features, 1 <= len(X_test) <= 2000, lam > 0
  • floats are compared with a tolerance of 1e-6

Goals

  • Standardise features with statistics of the training data only
  • Solve the ridge normal equations with an unpenalised intercept
  • Transform new rows with the training statistics before predicting
Starting Python…