Paul's Python CoursePPC

Project 11 of 12, for Paul

Calculus for ML

Calculus for machine learning, on the hill in the park: slopes measured numerically, the gradient straight uphill, and the chain rule saying which way to turn the dials on the Archive's new thermometer, a model with two settings.

About an hour and a half, in 18 steps.

Sign up free to open itLog in

Act 5 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • PaulThe next floor up is mine, machine learning, and training a model is walking downhill. First, though, we need a hill.
  • PaulThe park hill's a formula. How steep is the path, and which way is straight up from the gorse?
  • YahyaAnd the Archive's new thermometer reads one and a half degrees low. Either that or the air's wrong, so it's the thermometer.
  • PaulThey're all slopes, the three of them, so measure them and work out the ones you can.

hill_height(east, north) is the park hill's survey: its height in metres, at a point that many metres east and north of the park gate. The path runs due north from the gate to the top, and path_height(metres) gives its height that far along. Both are written.

Write four functions:

  • slope(f, x, step=1e-5): the slope of the function f at x, measured a small step either side.
  • partial_slopes(f, east, north, step=1e-5): the two slopes of f(east, north), stepping east with north held still, then stepping north with east held still, as (east_slope, north_slope).
  • uphill(f, east, north): the way straight uphill, as (bearing, steepness): the bearing of the arrow (east_slope, north_slope), from 0 up to 360, and its length.
  • miss_slopes(gain, offset, reading, true): the thermometer's corrected reading is gain * reading + offset. Return the slopes of its squared miss, (gain * reading + offset - true) ** 2, for gain and for offset, worked out with the chain rule.
What it prints
Steepest on the path: 650 m from the gate, slope 0.243, about 1 in 4
At the top: slopes 0.000 east, 0.000 north
From the gorse: straight uphill is 340 degrees, slope 0.212
Thermometer check (reads 8.5, should be 10.0):
  gain slope -25.5: turn the gain up
  offset slope -3.0: turn the offset up

What you learn

What's in it

  1. How steep, right here
  2. How steep, not how high
  3. The night the glass fell
  4. A step either side
  5. Two steps apart
  6. A slope for each way you face
  7. East, then north
  8. Straight uphill
  9. Uphill is west
  10. Rates multiply
  11. Inside and out
  12. Which way to turn the dials
  13. Reading high
  14. How Paul would do it
  15. Fix Riya's version
  16. Make the hill survey
  17. Ship it