Dictionary of Applied Machine Learning · transfer learning

transfer learning — Python demo

Updated on 2026-11-03

Numerical companion to the entry transfer learning: it recomputes what the entry states and prints one line per check

Backs the entry's story on real weather records: every weather station is its own learning task (predict the maximum temperature of a day from its minimum temperature with a straight line), a task is characterized by the probability distribution of its data points, and transfer learning couples the tasks of similar stations. Two stations first, Krems and Melk, each with a decision tree learned from five labeled days, coupled by requiring the two trees to agree on a test set of feature values; then a collection of nine Lower Austrian stations as the nodes of a similarity graph whose edge weights fall off with the distance between stations, coupled by GTVMin; and finally the special case of a node with no labeled data at all, which is SSL. Self-contained (numpy/matplotlib only, stdlib urllib for the download), fixed seeds.

Run it without installing anything:
uv run https://dictionaryofml.org/terms/transferlearning.py
uv downloads this script and the pinned NumPy and Matplotlib it needs, then runs it; the script fetches any input file it uses. To keep the output files, download transferlearning.py into a folder and run uv run transferlearning.py there. With NumPy and Matplotlib already installed, python3 transferlearning.py, from any directory — it writes its output files into the current directory. Fixed seeds, so the printed numbers reproduce exactly. Download transferlearning.py · Notebook · Open in Colab

Before running a downloaded copy, scan it: uvx bandit -q -t B102,B307,B602,B605 transferlearning.py (B102,B307,B602,B605 are the exec, eval and shell checks of bandit) reports nothing: the script runs no shell command and evaluates no generated code.

The script, block by block

One cell per block of the script: the code, and what that code printed when it last ran here

setup

"""
transferlearning.py — numerical companion to the glossary entry
'transfer learning'.

Purpose
-------
Backs the entry's story on real weather records: every weather station
is its own learning task (predict the maximum temperature of a day from
its minimum temperature with a straight line), a task is characterized
by the probability distribution of its data points, and transfer
learning couples the tasks of similar stations.  Two stations first,
Krems and Melk, each with a decision tree learned from five labeled days,
coupled by requiring the two trees to agree on a test set of feature
values; then a collection of nine Lower Austrian stations as
the nodes of a similarity graph whose edge weights fall off with the
distance between stations, coupled by GTVMin; and finally the special case of a node with no
labeled data at all, which is SSL.  Self-contained
(numpy/matplotlib only, stdlib urllib for the download), fixed seeds.

Setup
-----
Daily minimum and maximum air temperature of 2020 at nine GeoSphere
Austria stations.  Each station keeps only five labeled days, drawn at
random, as its local training set, which is what makes transfer worth
having; the remaining days of its year are its held-out days.  A local
hypothesis is a decision tree of depth two.  Isolated learning fits the
tree to the five days.  Coupled learning adds a penalty on the
disagreement, over a test set of feature values, between the trees of
connected stations: the GTV with the mean squared disagreement on the
test set as the discrepancy measure.

Blocks
------
[B-days]   The 366 days of 2020 at nine stations downloaded, none
           missing.  Krems and Melk, 32 km apart, have nearly the same
           relation between minimum and maximum temperature: the
           straight lines fitted to their full years differ by at most
           three degrees across the feature range.
[B-two]    Two tasks coupled by agreement on a test set: each station
           learns a decision tree of depth two from five labeled days
           alone, then the two trees are refitted alternately to their own
           days plus the test features labeled by the other tree, which
           minimizes the two training errors plus a penalty on the mean
           squared disagreement over the test set.  The coupled trees
           disagree less on the test set, and their average held-out
           error over the two stations is lower than that of the trees
           learned alone.
[B-graph]  Nine stations as nodes of a similarity graph, edge weights
           exp(-d^2 / 2 s^2) for the distance d between stations, three
           nearest neighbors each.  GTVMin -- the local training errors
           plus a weighted sum, over the edges, of the mean squared
           disagreement of the two trees on the test set -- solved by
           alternating weighted tree fits, lowers the average held-out
           error against isolated fitting and lowers it at a majority of
           the nodes.
[B-ssl]    A node without any labeled day: its isolated fit does not
           exist, GTVMin still assigns it a tree, fitted to the test
           features labeled by its neighbors' predictions, and that tree
           beats predicting the mean of its neighbors' labels.

Outputs
-------
transferlearning_days.csv   : station, tmin, tmax, labeled (1 if one of
                              the station's five training days)
transferlearning_graph.csv  : station, longitude, latitude, and the
                              neighbour list with edge weights
transferlearning_edges.csv  : one row per edge: x1,y1,x2,y2 (map
                              coordinates) and the weight
transferlearning_two_krems.csv, _two_melk.csv : the five labeled days of
                              the two-station panels
transferlearning_treegrid.csv : the isolated and the coupled trees on a grid
transferlearning_testfeat.csv : the test features the two trees must agree on
transferlearning_testfeat_plot.csv : the 20 of them (every second) the figure marks
transferlearning_danube.csv, _wachau.csv : the course of the Danube and the
                              four stations around the Wachau valley, for
                              the map above the two panels
transferlearning_nodes.csv, _edges_strong.csv, _edges_weak.csv : the graph
                              for the figure (edges as NaN-separated paths)
transferlearning_errors.csv : rank, station, held-out error of the tree
                              learned alone, of the GTVMin tree, and of the
                              GTVMin tree when that station has no labeled day
transferlearning.png        : matplotlib preview (checking only)
"""

# /// script
# requires-python = ">=3.10"
# dependencies = ["matplotlib==3.10.7", "numpy==2.3.4"]
# ///

import json
import urllib.request

import numpy as np
import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt

from pathlib import Path

OUT_DIR = Path(__file__).parent

report = []


def check(name, ok):
    report.append((name, bool(ok)))
    print(f"  [{'ok' if ok else 'FAIL'}] {name}")


# the nine stations: GeoSphere id, name, longitude, latitude
STATIONS = [
    (3805, "Krems", 15.622, 48.419),
    (5413, "Melk", 15.331, 48.210),
    (5609, "St. Poelten", 15.631, 48.200),
    (3811, "Langenlois", 15.697, 48.472),
    (1920, "Zwettl", 15.204, 48.618),
    (905, "Retz", 15.942, 48.761),
    (2117, "Horn", 15.644, 48.675),
    (5316, "Amstetten", 14.895, 48.109),
    (5904, "Wien", 16.356, 48.249),
]


def km_between(a, b):
    """Distance in km between two (lon, lat) pairs, flat-earth at 48 N."""
    dx = (a[0] - b[0]) * 111.32 * np.cos(np.deg2rad(48.4))
    dy = (a[1] - b[1]) * 110.57
    return float(np.hypot(dx, dy))

B-days

The 366 days of 2020 at nine stations downloaded, none missing. Krems and Melk, 32 km apart, have nearly the same relation between minimum and maximum temperature: the straight lines fitted to their full years differ by at most three degrees across the feature range.

days = {}
for sid, name, lon, lat in STATIONS:
    url = ("https://dataset.api.hub.geosphere.at/v1/station/historical/"
           f"klima-v2-1d?parameters=tlmin,tlmax&station_ids={sid}"
           "&start=2020-01-01&end=2020-12-31")
    with urllib.request.urlopen(url, timeout=120) as resp:
        pars = json.load(resp)["features"][0]["properties"]["parameters"]
    x = np.array(pars["tlmin"]["data"], dtype=float)
    y = np.array(pars["tlmax"]["data"], dtype=float)
    keep = ~(np.isnan(x) | np.isnan(y))
    days[name] = (x[keep], y[keep])
check("[B-days]   366 days of 2020 at nine stations downloaded, none missing",
      all(len(days[n][0]) == 366 for _, n, _, _ in STATIONS))


def lsq(x, y):
    """Least-squares straight line, returned as (w0, w1) = (intercept, slope)."""
    A = np.c_[np.ones_like(x), x]
    return np.linalg.lstsq(A, y, rcond=None)[0]


full = {n: lsq(*days[n]) for n in days}
grid = np.linspace(-10.0, 25.0, 71)
gap = float(np.max(np.abs((full["Krems"][0] + full["Krems"][1] * grid)
                          - (full["Melk"][0] + full["Melk"][1] * grid))))
print(f"    Krems and Melk are {km_between((15.622, 48.419), (15.331, 48.210)):.0f} km apart; "
      f"full-year lines y = {full['Krems'][1]:.2f} x + {full['Krems'][0]:.1f} and "
      f"y = {full['Melk'][1]:.2f} x + {full['Melk'][0]:.1f}")
check(f"[B-days]   the full-year lines of Krems and Melk differ by at most {gap:.1f} deg C "
      f"across the feature range: the two tasks are similar", gap < 3.0)
  [ok] [B-days]   366 days of 2020 at nine stations downloaded, none missing
    Krems and Melk are 32 km apart; full-year lines y = 1.12 x + 9.6 and y = 1.18 x + 7.6
  [ok] [B-days]   the full-year lines of Krems and Melk differ by at most 2.7 deg C across the feature range: the two tasks are similar
    held-out error, five labeled days and a depth-2 tree: isolated Krems 53.7, Melk 41.1; coupled Krems 39.9, Melk 27.1

B-two

Two tasks coupled by agreement on a test set: each station learns a decision tree of depth two from five labeled days alone, then the two trees are refitted alternately to their own days plus the test features labeled by the other tree, which minimizes the two training errors plus a penalty on the mean squared disagreement over the test set. The coupled trees disagree less on the test set, and their average held-out error over the two stations is lower than that of the trees learned alone.

# Two tasks, Krems and Melk, each with FIVE labeled days and a decision
# tree of depth two as its hypothesis.  Isolated: the tree fitted to the
# five days.  Coupled: the two training errors plus a penalty
# lam * mean over a test set T of (h1(x) - h2(x))^2, where T holds feature
# values drawn uniformly from [-10, 10].  Minimizing over one tree with the
# other fixed is a weighted tree fit to the own days plus the test features
# labeled by the other tree, so the joint problem is solved by alternating
# those two fits until neither tree changes.
N_TWO, DEPTH, N_TEST, LAM_TWO = 5, 2, 40, 1.0
two_rng = np.random.default_rng(5)
few = {n: np.sort(two_rng.choice(366, size=N_TWO, replace=False)) for n in ("Krems", "Melk")}
test_feat = np.sort(two_rng.uniform(-10.0, 10.0, N_TEST))
for n in days:                                    # the five labeled days of the other stations
    if n not in few:
        few[n] = np.sort(two_rng.choice(366, size=N_TWO, replace=False))
with open(OUT_DIR / "transferlearning_days.csv", "w") as fh:
    fh.write("station,tmin,tmax,labeled\n")
    for n in days:
        lab = np.zeros(366, dtype=bool); lab[few[n]] = True
        for xi, yi, li in zip(*days[n], lab):
            fh.write(f"{n},{xi:g},{yi:g},{int(li)}\n")


def fit_tree(x, y, w, depth):
    """Weighted CART decision tree for a numeric label (squared error), as nested dicts."""
    def wmean(i): return float(np.average(y[i], weights=w[i]))
    def wsse(i): return float(np.sum(w[i] * (y[i] - wmean(i)) ** 2))
    def build(idx, d):
        if d == 0 or len(idx) < 2 or np.ptp(x[idx]) == 0:
            return wmean(idx)
        order = idx[np.argsort(x[idx])]; xs = x[order]; best = None
        for k in range(1, len(order)):
            if xs[k] == xs[k - 1]:
                continue
            sse = wsse(order[:k]) + wsse(order[k:])
            if best is None or sse < best[0]:
                best = (sse, 0.5 * (xs[k - 1] + xs[k]), order[:k], order[k:])
        return wmean(idx) if best is None else {"t": best[1], "l": build(best[2], d - 1), "r": build(best[3], d - 1)}
    return build(np.arange(len(x)), depth)


def tree_predict(tree, xq):
    xq = np.asarray(xq, dtype=float)
    if not isinstance(tree, dict):
        return np.full(xq.shape, tree)
    left = xq <= tree["t"]
    out = np.empty(xq.shape); out[left] = tree_predict(tree["l"], xq[left]); out[~left] = tree_predict(tree["r"], xq[~left])
    return out


def own(n):
    x, y = days[n]; return x[few[n]], y[few[n]]


tree_iso = {n: fit_tree(*own(n), np.ones(N_TWO) / N_TWO, DEPTH) for n in few}
tree_two = {n: tree_iso[n] for n in ("Krems", "Melk")}
for _ in range(20):                                # alternate until neither tree moves
    moved = False
    for n, other in (("Krems", "Melk"), ("Melk", "Krems")):
        x, y = own(n)
        t = fit_tree(np.r_[x, test_feat], np.r_[y, tree_predict(tree_two[other], test_feat)],
                     np.r_[np.ones(N_TWO) / N_TWO, LAM_TWO * np.ones(N_TEST) / N_TEST], DEPTH)
        moved |= t != tree_two[n]; tree_two[n] = t
    if not moved:
        break


def disagreement(trees):
    return float(np.mean((tree_predict(trees["Krems"], test_feat) - tree_predict(trees["Melk"], test_feat)) ** 2))


def heldout_tree(n, tree):
    x, y = days[n]; m = np.ones(366, dtype=bool); m[few[n]] = False
    return float(np.mean((y[m] - tree_predict(tree, x[m])) ** 2))


dis_iso, dis_two = disagreement(tree_iso), disagreement(tree_two)
err_iso_two = {n: heldout_tree(n, tree_iso[n]) for n in ("Krems", "Melk")}
err_two = {n: heldout_tree(n, tree_two[n]) for n in ("Krems", "Melk")}
print("    held-out error, five labeled days and a depth-2 tree: isolated "
      + ", ".join(f"{n} {err_iso_two[n]:.1f}" for n in err_iso_two)
      + "; coupled " + ", ".join(f"{n} {err_two[n]:.1f}" for n in err_two))
check(f"[B-two]    coupling the two trees lowers their mean squared disagreement on the "
      f"{N_TEST} test features, {dis_iso:.1f} -> {dis_two:.1f}", dis_two < dis_iso)
check(f"[B-two]    and lowers the average held-out error of the two stations, "
      f"{np.mean(list(err_iso_two.values())):.1f} -> {np.mean(list(err_two.values())):.1f}",
      np.mean(list(err_two.values())) < np.mean(list(err_iso_two.values())))
  [ok] [B-two]    coupling the two trees lowers their mean squared disagreement on the 40 test features, 32.2 -> 0.1
  [ok] [B-two]    and lowers the average held-out error of the two stations, 47.4 -> 33.5
  [ok] [B-two] the figure marks twenty of the forty test features

10/10 checks pass
wrote /Users/junga1/dictionaryappliedml/pythondemos/transferlearning_days.csv, /Users/junga1/dictionaryappliedml/pythondemos/transferlearning_graph.csv, /Users/junga1/dictionaryappliedml/pythondemos/transferlearning_edges.csv, /Users/junga1/dictionaryappliedml/pythondemos/transferlearning_errors.csv, /Users/junga1/dictionaryappliedml/pythondemos/transferlearning.png

B-graph

Nine stations as nodes of a similarity graph, edge weights exp(-d^2 / 2 s^2) for the distance d between stations, three nearest neighbors each. GTVMin -- the local training errors plus a weighted sum, over the edges, of the mean squared disagreement of the two trees on the test set -- solved by alternating weighted tree fits, lowers the average held-out error against isolated fitting and lowers it at a majority of the nodes.

# Nine stations as nodes of a similarity graph.  Edge weight between two
# stations: exp(-d^2 / (2 s^2)) for their distance d, s = 40 km, keeping the
# three nearest neighbors of every node (symmetrized).  GTVMin with the
# trees: sum_i (own training error) + lam sum_{i<j} W_ij mean_T (h_i - h_j)^2.
# With all trees but h_i fixed, the penalty is (sum_j W_ij) times the mean
# squared deviation of h_i from the W-weighted mean of its neighbors'
# predictions on T (plus a constant), so one block step is a weighted tree
# fit to the own days plus T labeled by that mean; the blocks alternate.
names = [n for _, n, _, _ in STATIONS]
coords = {n: (lon, lat) for _, n, lon, lat in STATIONS}
N, S_KM, K_NN = len(names), 40.0, 3
W = np.zeros((N, N))
for i in range(N):
    d = np.array([km_between(coords[names[i]], coords[names[j]]) if j != i else np.inf for j in range(N)])
    for j in np.argsort(d)[:K_NN]:
        W[i, j] = W[j, i] = max(W[i, j], np.exp(-d[j] ** 2 / (2 * S_KM ** 2)))
check(f"[B-graph]  the similarity graph is connected: {int((W > 0).sum() // 2)} edges over "
      f"{N} nodes", np.all(np.linalg.matrix_power((W > 0) + np.eye(N, dtype=bool), N) > 0))


def gtvmin_trees(lam, has_labels, rounds=40):
    """GTVMin over the graph with a tree per node, by alternating block fits."""
    trees = {n: (tree_iso[n] if has_labels[n] else 0.0) for n in names}
    for _ in range(rounds):
        moved = False
        for i, n in enumerate(names):
            wsum = W[i].sum()
            target = sum(W[i, j] * tree_predict(trees[names[j]], test_feat) for j in range(N) if W[i, j] > 0) / wsum
            if has_labels[n]:
                x, y = own(n)
                xs, ys, ws = np.r_[x, test_feat], np.r_[y, target], np.r_[np.ones(N_TWO) / N_TWO, lam * wsum * np.ones(N_TEST) / N_TEST]
            else:
                xs, ys, ws = test_feat, target, np.ones(N_TEST) / N_TEST
            t = fit_tree(xs, ys, ws, DEPTH)
            moved |= t != trees[n]; trees[n] = t
        if not moved:
            break
    return trees


LAM = 1.0
tree_gtv = gtvmin_trees(LAM, {n: True for n in names})
err_iso = np.array([heldout_tree(n, tree_iso[n]) for n in names])
err_gtv = np.array([heldout_tree(n, tree_gtv[n]) for n in names])
print("    held-out error of the tree learned alone vs the GTVMin tree:")
for i, n in enumerate(names):
    print(f"      {n:12s} {err_iso[i]:6.1f} -> {err_gtv[i]:6.1f}")
check(f"[B-graph]  GTVMin lowers the average held-out error over the nine stations, "
      f"{err_iso.mean():.1f} -> {err_gtv.mean():.1f}", err_gtv.mean() < err_iso.mean())
check(f"[B-graph]  and lowers it at {int((err_gtv < err_iso).sum())} of the {N} stations",
      (err_gtv < err_iso).sum() > N / 2)
  [ok] [B-graph]  the similarity graph is connected: 19 edges over 9 nodes
    held-out error of the tree learned alone vs the GTVMin tree:
      Krems          53.7 ->   37.5
      Melk           41.1 ->   24.4
      St. Poelten    84.2 ->   38.0
      Langenlois     68.3 ->   39.6
      Zwettl        102.4 ->   40.9
      Retz           34.4 ->   26.1
      Horn           50.3 ->   44.4
      Amstetten      49.4 ->   41.6
      Wien           29.5 ->   30.8
  [ok] [B-graph]  GTVMin lowers the average held-out error over the nine stations, 57.0 -> 35.9
  [ok] [B-graph]  and lowers it at 8 of the 9 stations
    Melk without labels: GTVMin tree, held-out error 33.5; predicting the neighbors' mean label: 74.9

B-ssl

A node without any labeled day: its isolated fit does not exist, GTVMin still assigns it a tree, fitted to the test features labeled by its neighbors' predictions, and that tree beats predicting the mean of its neighbors' labels.

# One node without labeled days: Melk's five labels are withheld.  Its
# isolated fit does not exist; GTVMin still assigns it a tree, fitted to
# the test features labeled by its neighbors' predictions.
tree_ssl = gtvmin_trees(LAM, {n: n != "Melk" for n in names})
i_melk = names.index("Melk")
err_ssl_melk = heldout_tree("Melk", tree_ssl["Melk"])
nbrs = [names[j] for j in range(N) if W[i_melk, j] > 0]
mean_nbr = float(np.mean([own(n)[1].mean() for n in nbrs]))
x_m, y_m = days["Melk"]
err_const = float(np.mean((y_m - mean_nbr) ** 2))
nbr_pred = sum(W[i_melk, j] * tree_predict(tree_ssl[names[j]], test_feat) for j in range(N) if W[i_melk, j] > 0) / W[i_melk].sum()
pull = float(np.sqrt(np.mean((tree_predict(tree_ssl["Melk"], test_feat) - nbr_pred) ** 2)))
print(f"    Melk without labels: GTVMin tree, held-out error {err_ssl_melk:.1f}; predicting the "
      f"neighbors' mean label: {err_const:.1f}")
check(f"[B-ssl]    with no labeled day at Melk, GTVMin still yields a tree, within "
      f"{pull:.2f} deg C (root mean square over the test features) of its neighbors' weighted predictions", pull < 2.0)
check(f"[B-ssl]    and that tree beats predicting the neighbors' mean label, "
      f"{err_ssl_melk:.1f} against {err_const:.1f}", err_ssl_melk < 0.6 * err_const)

# ----------------------------------------------------------------- CSV
with open(OUT_DIR / "transferlearning_graph.csv", "w") as fh:
    fh.write("station,lon,lat,neighbors\n")
    for i, n in enumerate(names):
        nb = ";".join(f"{names[j]}:{W[i, j]:.2f}" for j in range(N) if W[i, j] > 0)
        fh.write(f"{n},{coords[n][0]},{coords[n][1]},{nb}\n")
with open(OUT_DIR / "transferlearning_edges.csv", "w") as fh:
    fh.write("x1,y1,x2,y2,weight\n")
    for i in range(N):
        for j in range(i + 1, N):
            if W[i, j] > 0:
                fh.write(f"{coords[names[i]][0]},{coords[names[i]][1]},"
                         f"{coords[names[j]][0]},{coords[names[j]][1]},{W[i, j]:.3f}\n")
# tables the entry's figures read directly
for n in ("Krems", "Melk"):                       # the five labeled days of each panel
    with open(OUT_DIR / f"transferlearning_two_{n.lower()}.csv", "w") as fh:
        fh.write("tmin,tmax\n")
        for xi, yi in zip(*own(n)):
            fh.write(f"{xi:g},{yi:g}\n")
tgrid = np.linspace(-12.0, 24.0, 721)
with open(OUT_DIR / "transferlearning_treegrid.csv", "w") as fh:
    fh.write("tmin,krems_iso,krems_two,melk_iso,melk_two\n")
    cols = [tree_predict(tree_iso["Krems"], tgrid), tree_predict(tree_two["Krems"], tgrid),
            tree_predict(tree_iso["Melk"], tgrid), tree_predict(tree_two["Melk"], tgrid)]
    for k, g in enumerate(tgrid):
        fh.write(f"{g:.2f}," + ",".join(f"{c[k]:.3f}" for c in cols) + "\n")
with open(OUT_DIR / "transferlearning_testfeat.csv", "w") as fh:
    fh.write("tmin\n")
    for t in test_feat:
        fh.write(f"{t:.3f}\n")
# the entry's figure marks every second test feature, twenty of the forty;
# the coupling uses all of them
with open(OUT_DIR / "transferlearning_testfeat_plot.csv", "w") as fh:
    fh.write("tmin\n")
    for t in test_feat[::2]:
        fh.write(f"{t:.3f}\n")
check("[B-two] the figure marks twenty of the forty test features",
      len(test_feat[::2]) == 20)
# the map above the two panels: the Danube through the Wachau valley, from
# Ybbs to Tulln (approximate course, longitude/latitude), and the stations
# around the valley
DANUBE = [(15.08, 48.17), (15.21, 48.21), (15.33, 48.23), (15.37, 48.25), (15.40, 48.30),
          (15.41, 48.36), (15.47, 48.40), (15.52, 48.395), (15.60, 48.41), (15.70, 48.37),
          (15.73, 48.35), (15.90, 48.35), (16.05, 48.33)]
with open(OUT_DIR / "transferlearning_danube.csv", "w") as fh:
    fh.write("lon,lat\n")
    for lon, lat in DANUBE:
        fh.write(f"{lon},{lat}\n")
with open(OUT_DIR / "transferlearning_wachau.csv", "w") as fh:
    fh.write("station,lon,lat\n")
    for n in ("Melk", "Krems", "Langenlois", "St. Poelten"):
        fh.write(f"{n},{coords[n][0]},{coords[n][1]}\n")
with open(OUT_DIR / "transferlearning_nodes.csv", "w") as fh:
    fh.write("station,lon,lat\n")
    for n in names:
        fh.write(f"{n},{coords[n][0]},{coords[n][1]}\n")
for tag, keep in (("strong", lambda w: w >= 0.5), ("weak", lambda w: w < 0.5)):
    with open(OUT_DIR / f"transferlearning_edges_{tag}.csv", "w") as fh:
        fh.write("x,y\n")                            # NaN rows break the path between edges
        for i in range(N):
            for j in range(i + 1, N):
                if W[i, j] > 0 and keep(W[i, j]):
                    fh.write(f"{coords[names[i]][0]},{coords[names[i]][1]}\n"
                             f"{coords[names[j]][0]},{coords[names[j]][1]}\nnan,nan\n")
with open(OUT_DIR / "transferlearning_errors.csv", "w") as fh:
    fh.write("rank,station,isolated,gtvmin,gtvmin_unlabeled\n")
    for i, n in enumerate(names):
        fh.write(f"{N - i},{n},{err_iso[i]:.2f},{err_gtv[i]:.2f},{heldout_tree(n, tree_ssl[n]):.2f}\n")

# -------------------------------------------------------------- preview
fig, axes = plt.subplots(1, 3, figsize=(13.0, 3.9))
for ax, n in zip(axes[:2], ("Krems", "Melk")):
    x, y = own(n)
    ax.plot(x, y, "ko", ms=5, label="the five labeled days")
    ax.plot(test_feat, np.full(N_TEST, -5.0), "k|", ms=6, label="test features")
    ax.plot(tgrid, tree_predict(tree_iso[n], tgrid), "k:", lw=1.4, label="tree learned alone")
    ax.plot(tgrid, tree_predict(tree_two[n], tgrid), "k-", lw=1.6, label="tree coupled with the other station")
    ax.set_xlabel("minimum temperature (deg C)"); ax.set_ylabel("maximum temperature (deg C)")
    ax.set_title(f"{n}: one learning task"); ax.legend(frameon=False, fontsize=7)
ax = axes[2]
for i in range(N):
    for j in range(i + 1, N):
        if W[i, j] > 0:
            ax.plot([coords[names[i]][0], coords[names[j]][0]], [coords[names[i]][1], coords[names[j]][1]],
                    "-", color="0.4", lw=0.5 + 3.0 * W[i, j])
for n in names:
    ax.plot(*coords[n], "ko", ms=5); ax.annotate(n, coords[n], textcoords="offset points", xytext=(4, 4), fontsize=7)
ax.set_xlabel("longitude"); ax.set_ylabel("latitude"); ax.set_title("similarity graph (edge width = weight)")
fig.tight_layout()
fig.savefig(OUT_DIR / "transferlearning.png", dpi=110)

n_ok = sum(ok for _, ok in report)
print(f"\n{n_ok}/{len(report)} checks pass")
print(f"wrote {OUT_DIR / 'transferlearning_days.csv'}, "
      f"{OUT_DIR / 'transferlearning_graph.csv'}, {OUT_DIR / 'transferlearning_edges.csv'}, "
      f"{OUT_DIR / 'transferlearning_errors.csv'}, {OUT_DIR / 'transferlearning.png'}")
if n_ok != len(report):
    raise SystemExit(1)
  [ok] [B-ssl]    with no labeled day at Melk, GTVMin still yields a tree, within 0.01 deg C (root mean square over the test features) of its neighbors' weighted predictions
  [ok] [B-ssl]    and that tree beats predicting the neighbors' mean label, 33.5 against 74.9
Preview figure produced by transferlearning.py
The preview figure the block B-ssl writes when the script runs