Start here

Run it

The bikes from ml-time-series, rented each hour in Washington from Monday 2 July to Sunday 30 December 2012: half a year, where the video had two. Every hour is there: the 40 when the bikes were closed, most of them for Hurricane Sandy, are written as 0. X holds the calendar: the hour, and the day of the week, 0 for Monday.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

1Last week, copied

Run it

From Time series and forecasting

A week is 168 hours, so shift(168) puts the same hour last week beside every hour: the copy, the forecast any model has to beat. Run the cell. It draws two weeks, two peaks each working day and one long rise at the weekend, then prints Monday 1 October at 8 in the morning.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

784 bikes went out, and the copy says 750, as in the video.

2Walk forward

Your turn

From Time series and forecasting

Walk the copy over the video's thirteen weeks, 1 October to 30 December. For each week, walk.split gives the places of the hours before its Monday, and of the week itself. Keep the copy's miss for each week in copy_misses. The copy needs no training, so past is not used yet.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

miss takes the real counts of the week first, then the copy's counts of the same hours. .iloc[week] picks those hours.

One way to do it
walk = TimeSeriesSplit(n_splits=13, test_size=168)
copy_misses = []
for past, week in walk.split(rentals):
    copy_misses.append(miss(rentals.iloc[week], copy.iloc[week]))
print(round(np.mean(copy_misses), 1))

3The calendar's trees, walked forward

Your turn

From Time series and forecasting

Write walked_miss(columns): for each of the thirteen weeks, it fits boosted trees on those columns of X over the past hours, forecasts the week, and keeps the miss. It gives back the average of the thirteen. Then try the calendar alone.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

Step 2's loop, with a fit on the past and a forecast for the week before each miss. Make the trees with random_state=0, so your numbers are these.

One way to do it
def walked_miss(columns):
    misses = []
    for past, week in walk.split(X):
        trees = HistGradientBoostingRegressor(random_state=0)
        trees.fit(X[columns].iloc[past], rentals.iloc[past])
        guess = trees.predict(X[columns].iloc[week])
        misses.append(miss(rentals.iloc[week], guess))
    return np.mean(misses)

calendar = walked_miss(["hour", "weekday"])
print(round(calendar, 1))
Prints
81.3

4Last week as an input, and hours at random

Run it

From Time series and forecasting

Run the cell. Last week's count joins the calendar as a column, and walked forward the trees now beat the copy. Then the same trees are scored on hours dealt at random, as for a table. Every test hour then has its neighbours, an hour before and an hour after, among the training rows: the future helps. So that miss is too small to be true. Walking forward gives the one you'll really get.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

5What the forecast leans on

Your turn

From Feature importance and SHAP and Time series and forecasting

Take the first Monday's forecaster: trees fitted on every hour before 1 October, with last week's count. Shuffle each input over its week, ten times, scored by the miss (scoring="neg_mean_absolute_error"), and keep the falls in falls. A fall is how many bikes an hour the miss grows by.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

Last week's count leads, and it makes no one ride: it tells the forecast this autumn's level. A fall says what the model uses, not what makes people ride.

Hint

Fit on X.loc[before, columns], then hand permutation_importance the week's rows of the same columns, its real counts, the score and n_repeats=10, random_state=0.

One way to do it
columns = ["hour", "weekday", "last_week"]
before = X.index < "2012-10-01"
week = (X.index >= "2012-10-01") & (X.index < "2012-10-08")
forecaster = HistGradientBoostingRegressor(random_state=0).fit(X.loc[before, columns], rentals[before])
result = permutation_importance(forecaster, X.loc[week, columns], rentals[week],
                                scoring="neg_mean_absolute_error", n_repeats=10, random_state=0)
falls = pd.Series(result.importances_mean, columns)
print(falls.round(1))
Prints
hour          90.3
weekday       27.6
last_week    115.7
dtype: float64

6The week before

On your own

From Time series and forecasting

On your own now. Add the same hour two weeks back as a column of X, two_weeks_ago, and keep in with_both the walked miss of trees on all four inputs.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

Two weeks are 336 hours. walked_miss takes the four names.

One way to do it
X["two_weeks_ago"] = rentals.shift(336)
with_both = walked_miss(["hour", "weekday", "last_week", "two_weeks_ago"])
print(round(with_both, 1))
Prints
67.3

7How the credit is shared

On your own

From Feature importance and SHAP and Time series and forecasting

On your own again. Shuffle each of the four inputs of the first Monday's forecaster, as in step 5, and keep the falls in shared. Look at which input lost the most when the week before arrived.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

The two weeks' counts already hold the shape of each day, so they now share the credit the hour had. A small fall can mean shared, not useless.

Hint

Step 5's cell with the four names. Then hold shared next to falls.

One way to do it
four = ["hour", "weekday", "last_week", "two_weeks_ago"]
forecaster = HistGradientBoostingRegressor(random_state=0).fit(X.loc[before, four], rentals[before])
result = permutation_importance(forecaster, X.loc[week, four], rentals[week],
                                scoring="neg_mean_absolute_error", n_repeats=10, random_state=0)
shared = pd.Series(result.importances_mean, four)
print(shared.round(1))
Prints
hour              34.4
weekday            8.8
last_week        100.9
two_weeks_ago     54.2
dtype: float64

Run on Pyodide 314, Python 3.14, pandas 3.0, matplotlib 3.10 and scikit-learn 1.8. Checked on .