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):
- Model:
p(x) = σ(w·x + b)withσ(z) = 1 / (1 + e^(-z)). Start atw = 0.0,b = 0.0. - Run
stepssteps of batch gradient descent on the average log loss with learning ratelr, using the partial derivativesmean((p - y)·x)forwandmean(p - y)forb, both computed before either parameter changes. - Return the tuple
(w, b, boundary, probs):boundaryis the temperature wherep = 0.5, that is, wherew·x + b = 0(Noneifw == 0), andprobslistsp(q)for everyqinqueries.
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 are0or10 <= 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