Problem 475843 · easy · Level 04 Non-Linear Data Structures

When Does the Heating Come On?

logistic regression · gradient descent · decision boundary · probability · log loss

A smart thermostat logged the outdoor temperature at 7 a.m. (xs, in °C) and whether the heating switched on that morning (ys, 1 for on, 0 for off). The owner wants a model that gives the probability of heating for any temperature, and the temperature at which it becomes more likely than not.

Write fit_heating(xs, ys, lr, steps, queries):

  1. Model: p(x) = σ(w·x + b) with σ(z) = 1 / (1 + e^(-z)). Start at w = 0.0, b = 0.0.
  2. Run steps steps of batch gradient descent on the average log loss with learning rate lr, using the partial derivatives mean((p - y)·x) for w and mean(p - y) for b, both computed before either parameter changes.
  3. Return the tuple (w, b, boundary, probs): boundary is the temperature where p = 0.5, that is, where w·x + b = 0 (None if w == 0), and probs lists p(q) for every q in queries.

Examples

Input:  xs = [-4, -2, 0, 1, 3, 4, 6, 8, 9, 12, 14, 17], ys = [1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0],
        lr = 0.05, steps = 2000, queries = [0, 5, 10]
Output: (-0.652380248778182, 3.3133855958070217, 5.0789177048393706,
         [0.9648851715155475, 0.5128682456823128, 0.038775593055103585])
Explanation: the weight is negative (colder mornings mean heating), and the probability crosses
one half at about 5.08 °C.

Input:  the same data, lr = 0.05, steps = 0, queries = [0, 5]
Output: (0.0, 0.0, None, [0.5, 0.5])

Constraints

  • 1 <= len(xs) == len(ys) <= 500, -30 <= x <= 40, labels are 0 or 1
  • 0 <= steps <= 5000, 0 < lr <= 1, 0 <= len(queries) <= 50
  • floats are compared with a tolerance of 1e-6

Goals

  • Train a one-feature logistic regression by batch gradient descent from zero
  • Turn the trained model into probabilities for new inputs
  • Find the input where the predicted probability crosses one half
Starting Python…