{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  },
  "language_info": {
   "name": "python"
  },
  "colab": {
   "name": "encoder.ipynb"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "# encoder \u2014 Python demo\n\nNumerical companion to the entry [encoder](https://dictionaryofml.org/terms/encoder.html) of the [Dictionary of Applied Machine Learning](https://dictionaryofml.org/): it recomputes what the entry states and prints one line per check.\n\nNumerical companion to the glossary entry 'encoder'. The GeoSphere Austria weather station Krems an der Donau (station id 3805) records eight measurements per day. The principal component analysis (PCA) encoder is the matrix whose rows are the eigenvectors of the sample covariance matrix belonging to its largest eigenvalues; it maps the eight scaled measurements of a day to a code of d' numbers, and the decoder, its transpose, maps the code back to eight numbers.\n\nRequires NumPy and Matplotlib only, and uses fixed seeds, so the printed numbers reproduce exactly. Generated from [`pythondemos/encoder.py`](https://dictionaryofml.org/terms/encoder.py); CC BY 4.0.\n\nBefore running a downloaded copy, scan it: `uvx bandit -q -t B102,B307,B602,B605 encoder.py` (B102,B307,B602,B605 are the exec, eval and shell checks of [bandit](https://bandit.readthedocs.io/)) reports nothing: the script runs no shell command and evaluates no generated code."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "# Notebook shim: the script resolves output paths relative to __file__,\n# which a notebook kernel does not define; everything lands in the\n# working directory instead.\nimport os\n__file__ = os.path.join(os.getcwd(), \"encoder.py\")\nos.makedirs(\"pythondemos\", exist_ok=True)"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "\"\"\"Encoders on a year of days at Krems an der Donau and on an excerpt of\nthe Universal Declaration of Human Rights: a map from the raw features of\na data point to a shorter code, judged by how well a decoder rebuilds the\ndata point from that code.\n\nPurpose\n-------\nNumerical companion to the glossary entry 'encoder'.  The GeoSphere\nAustria weather station Krems an der Donau (station id 3805) records\neight measurements per day.  The principal component analysis (PCA)\nencoder is the matrix whose rows are the eigenvectors of the sample\ncovariance matrix belonging to its largest eigenvalues; it maps the eight\nscaled measurements of a day to a code of d' numbers, and the decoder,\nits transpose, maps the code back to eight numbers.\n\nThe demo recomputes what the entry claims.  (1) Encoder followed by\ndecoder returns approximately the day it started from: with a code of two\nnumbers the eight measurements of 1 January 2024 come back to within\n0.80 on the scaled axes, and the reconstruction error averaged over the\n366 days is 2.91 of the 8.00 total variance.  (2) The reconstruction\nerror falls with the code length d' and equals the sum of the eigenvalues\nthe code drops, down to zero at d' = 8.  (3) The encoder matters, not\nonly the code length: a fixed random orthonormal encoder leaves a larger\nreconstruction error at every code length below 8.  (4) The token-to-\nvector step of a language model is an encoder of the same shape -- the\none-hot vector of a token times the embedding matrix is the token's row\nof that matrix -- and here it loses nothing, since distinct rows let the\nnearest row recover every token.  (5) What the code of two numbers drops\nis beyond every decoder and can matter for a later learning task: it\nkeeps 96 percent of the sample variance of the mean temperature and 3\npercent of the wind speed, two days with almost the same code differ by\n3.5 meters per second in wind speed while the decoder returns one value\nfor both, a linear classifier learned by least squares calls windy days\nwith an accuracy of 66 percent from the code against 98 percent from\nthe eight measurements, and one new feature chosen from the labels, the\nlinear discriminant analysis direction, reaches 98 percent as well.\n\nDeterministic: the weather data are a fixed archive year, PCA is an\neigenvalue decomposition, and the random encoder and the embedding matrix\nare drawn from fixed seeds.  Self-contained: numpy and matplotlib only\n(stdlib urllib for the download, which falls back to the committed CSV).\n\nBlocks\n------\n[B-data]    Download the 366 days of 2024 with eight measurements each and\n            scale every measurement to zero sample mean and unit sample\n            variance, so that the eight carry comparable weight.\n[B-encoder] The PCA encoder for a code of two numbers: the rows of the\n            matrix are eigenvectors of the sample covariance matrix, the\n            code of a day is the matrix times its scaled measurements, and\n            the decoder is the transpose. Encoder then decoder returns the\n            day approximately; the entry's named day is reported.\n[B-length]  The reconstruction error against the code length d' = 1 to 8:\n            it equals the sum of the dropped eigenvalues and vanishes at\n            d' = 8, already at d' = 7 because the mean temperature is the\n            midpoint of the lowest and the highest, and a fixed random\n            orthonormal encoder leaves more at every code length below 8.\n[B-token]   The token-to-vector encoder of a language model on an excerpt\n            of the Universal Declaration of Human Rights: a one-hot vector\n            of length |V| times the embedding matrix selects a row, and the\n            nearest row recovers every token, so this encoder loses nothing.\n[B-drops]   What the code of two numbers drops: the kept fraction of each\n            measurement, a pair of days with almost the same code and very\n            different wind speeds, the accuracy of a linear classifier for\n            windy days from the code against the eight measurements and\n            against one linear discriminant analysis feature, and the error\n            an encoder learned on the cold half leaves on the warm half.\n[B-plot]    Preview: the two-number code of 21 days, and the reconstruction\n            error against the code length.\n\nOutputs\n-------\nencoder_weather.csv    : date and the eight measurements (committed copy)\nencoder_code.csv       : date, z1, z2, warm -- the code of all 366 days\nencoder_code_plot.csv  : z1, z2, warm, month -- every 18th of those days,\n                         the 21 dots of the figure\nencoder_errors.csv     : dprime, pca, random -- error against code length\nencoder.png            : matplotlib preview (checking only)\n\"\"\"\n\n# /// script\n# requires-python = \">=3.10\"\n# dependencies = [\"matplotlib==3.10.7\", \"numpy==2.3.4\"]\n# ///\n\nimport json\nimport urllib.request\nfrom pathlib import Path\n\nimport numpy as np\nimport matplotlib\n\nmatplotlib.use(\"Agg\")\nimport matplotlib.pyplot as plt\n\nOUT_DIR = Path(__file__).parent\n\nreport = []\n\n\ndef check(name, ok):\n    report.append((name, bool(ok)))\n    print(f\"  [{'ok' if ok else 'FAIL'}] {name}\")"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-data]** Download the 366 days of 2024 with eight measurements each and scale every measurement to zero sample mean and unit sample variance, so that the eight carry comparable weight."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-data] eight daily measurements at Krems an der Donau, 2024\")\nPARAMS = [\"tlmin\", \"tlmax\", \"tl_mittel\", \"rr\", \"so_h\", \"rf_mittel\",\n          \"p_mittel\", \"vv_mittel\"]\nURL = (\"https://dataset.api.hub.geosphere.at/v1/station/historical/\"\n       f\"klima-v2-1d?parameters={','.join(PARAMS)}&station_ids=3805\"\n       \"&start=2024-01-01&end=2024-12-31\")\nCSV = OUT_DIR / \"encoder_weather.csv\"\ntry:\n    with urllib.request.urlopen(URL, timeout=120) as resp:\n        payload = json.load(resp)\n    params = payload[\"features\"][0][\"properties\"][\"parameters\"]\n    stamps = [t[:10] for t in payload[\"timestamps\"]]\n    M = np.stack([np.array(params[p][\"data\"], dtype=float) for p in PARAMS], 1)\n    M[:, 3] = np.maximum(M[:, 3], 0.0)        # -1 marks a day without rain\n    with open(CSV, \"w\") as fh:\n        fh.write(\"date,\" + \",\".join(PARAMS) + \"\\n\")\n        for day, row in zip(stamps, M):\n            fh.write(day + \",\" + \",\".join(f\"{v:g}\" for v in row) + \"\\n\")\n    print(f\"    downloaded {len(stamps)} days\")\nexcept OSError as err:\n    print(f\"    download failed ({err}); reading the committed CSV\")\n    rows = CSV.read_text().strip().splitlines()[1:]\n    stamps = [r.split(\",\")[0] for r in rows]\n    M = np.array([[float(v) for v in r.split(\",\")[1:]] for r in rows])\n\ncheck(\"[B-data] 366 days with eight measurements each\", M.shape == (366, 8))\ncheck(\"[B-data] no day is missing a measurement\", np.all(np.isfinite(M)))\nX = (M - M.mean(axis=0)) / M.std(axis=0)\nm, d = X.shape\ncheck(f\"[B-data] after scaling, every measurement has sample mean 0 and \"\n      f\"sample variance 1, so the {d} together carry a total variance of \"\n      f\"{float(np.sum(np.var(X, axis=0))):.2f}\",\n      abs(float(np.sum(np.var(X, axis=0))) - d) < 1e-9)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-encoder]** The PCA encoder for a code of two numbers: the rows of the matrix are eigenvectors of the sample covariance matrix, the code of a day is the matrix times its scaled measurements, and the decoder is the transpose. Encoder then decoder returns the day approximately; the entry's named day is reported."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-encoder] the PCA encoder: eigenvectors of the sample covariance\")\nC = X.T @ X / m                                # sample covariance matrix\nevals, evecs = np.linalg.eigh(C)\norder = np.argsort(evals)[::-1]\nevals, evecs = evals[order], evecs[:, order]\ncheck(f\"[B-encoder] the eight eigenvalues of the sample covariance matrix \"\n      f\"sum to the total variance {float(np.sum(evals)):.2f}; the two \"\n      f\"largest are {evals[0]:.2f} and {evals[1]:.2f}\",\n      abs(float(np.sum(evals)) - d) < 1e-9)\n\nDPRIME = 2\nW = evecs[:, :DPRIME].T                        # encoder, d' by d\nZ = X @ W.T                                    # the code of every day\nXhat = Z @ W                                   # decoder: the transpose\ncheck(f\"[B-encoder] the encoder has shape ({DPRIME}, {d}) and its rows are \"\n      f\"orthonormal\", W.shape == (DPRIME, d)\n      and np.allclose(W @ W.T, np.eye(DPRIME)))\nerr2 = float(np.mean(np.sum((X - Xhat) ** 2, axis=1)))\ncheck(f\"[B-encoder] averaged over the {m} days, the squared distance between \"\n      f\"a day and its reconstruction is {err2:.2f} of the total variance \"\n      f\"{d:.2f}, so a code of {DPRIME} numbers keeps \"\n      f\"{100 * (1 - err2 / d):.0f} percent of it\", 0 < err2 < d)\ni0 = stamps.index(\"2024-01-01\")\ngap0 = np.abs(X[i0] - Xhat[i0])\ncheck(f\"[B-encoder] encoder then decoder returns the day approximately: for \"\n      f\"1 January 2024 the code is ({Z[i0, 0]:.2f}, {Z[i0, 1]:.2f}) and the \"\n      f\"eight reconstructed measurements differ from the scaled originals \"\n      f\"by at most {gap0.max():.2f}\", gap0.max() < 1.0)\nwarm = X[:, 1] > np.median(X[:, 1])            # maximum temperature above median\ncheck(f\"[B-encoder] the first code number separates the {int(np.sum(warm))} \"\n      f\"days whose maximum temperature is above the median from the \"\n      f\"{int(np.sum(~warm))} others: its mean is {Z[warm, 0].mean():.2f} on \"\n      f\"the warm days and {Z[~warm, 0].mean():.2f} on the cold ones\",\n      abs(Z[warm, 0].mean() - Z[~warm, 0].mean()) > 1.0)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-length]** The reconstruction error against the code length d' = 1 to 8: it equals the sum of the dropped eigenvalues and vanishes at d' = 8, already at d' = 7 because the mean temperature is the midpoint of the lowest and the highest, and a fixed random orthonormal encoder leaves more at every code length below 8."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-length] reconstruction error against the code length\")\nrng = np.random.default_rng(2024)\nQ = np.linalg.qr(rng.standard_normal((d, d)))[0]     # random orthonormal rows\nerrs_pca, errs_rnd, dropped = [], [], []\nfor k in range(1, d + 1):\n    Wk = evecs[:, :k].T\n    errs_pca.append(float(np.mean(np.sum((X - (X @ Wk.T) @ Wk) ** 2, axis=1))))\n    Rk = Q[:, :k].T\n    errs_rnd.append(float(np.mean(np.sum((X - (X @ Rk.T) @ Rk) ** 2, axis=1))))\n    dropped.append(float(np.sum(evals[k:])))\ncheck(f\"[B-length] the error of the PCA encoder equals the sum of the \"\n      f\"eigenvalues the code drops at every code length (largest difference \"\n      f\"{max(abs(a - b) for a, b in zip(errs_pca, dropped)):.2e})\",\n      np.allclose(errs_pca, dropped))\ncheck(f\"[B-length] the error falls with the code length, from \"\n      f\"{errs_pca[0]:.2f} at one number to {errs_pca[1]:.2f} at two and \"\n      f\"{errs_pca[3]:.2f} at four, and vanishes at {d}: {errs_pca[-1]:.1e}\",\n      np.all(np.diff(errs_pca) < 0) and errs_pca[-1] < 1e-12)\ncheck(f\"[B-length] the smallest eigenvalue is {evals[-1]:.1e}, so one of the \"\n      f\"eight measurements adds nothing the other seven do not already \"\n      f\"carry: a code of seven numbers leaves {errs_pca[6]:.1e}\",\n      evals[-1] < 1e-3)\ncheck(f\"[B-length] the random orthonormal encoder leaves more error at every \"\n      f\"code length below {d} ({errs_rnd[1]:.2f} against {errs_pca[1]:.2f} at \"\n      f\"two numbers), so the encoder matters and not only the code length\",\n      all(r > p + 1e-9 for r, p in zip(errs_rnd[:-1], errs_pca[:-1])))"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-token]** The token-to-vector encoder of a language model on an excerpt of the Universal Declaration of Human Rights: a one-hot vector of length |V| times the embedding matrix selects a row, and the nearest row recovers every token, so this encoder loses nothing."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-token] the token-to-vector encoder on a UDHR excerpt\")\nEXCERPT = (\"All human beings are born free and equal in dignity and rights. \"\n           \"They are endowed with reason and conscience and should act \"\n           \"towards one another in a spirit of brotherhood. Everyone has \"\n           \"the right to life, liberty and security of person.\")\ntokens = EXCERPT.lower().replace(\",\", \"\").replace(\".\", \"\").split()\nvocab = sorted(set(tokens))\nnv, dtok = len(vocab), 4\ncheck(f\"[B-token] the excerpt has {len(tokens)} tokens over a vocabulary of \"\n      f\"{nv} distinct ones, and each is encoded as a vector of {dtok} \"\n      f\"numbers\", nv < len(tokens) and dtok < nv)\nrng_tok = np.random.default_rng(7)\nE = rng_tok.standard_normal((nv, dtok))        # the embedding matrix\nv = vocab.index(\"dignity\")\nonehot = np.zeros(nv)\nonehot[v] = 1.0\ncheck(f\"[B-token] the one-hot vector of a token times the embedding matrix \"\n      f\"is that token's row: for token {v} the two agree to \"\n      f\"{float(np.max(np.abs(onehot @ E - E[v]))):.1e}\",\n      np.allclose(onehot @ E, E[v]))\ncodes = E[[vocab.index(t) for t in tokens]]\nrecovered = [int(np.argmin(np.sum((E - c) ** 2, axis=1))) for c in codes]\ncheck(f\"[B-token] the nearest row recovers the token for all {len(tokens)} \"\n      f\"tokens, so this encoder shortens a vector of length {nv} to one of \"\n      f\"length {dtok} and still loses nothing\",\n      recovered == [vocab.index(t) for t in tokens])"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-drops]** What the code of two numbers drops: the kept fraction of each measurement, a pair of days with almost the same code and very different wind speeds, the accuracy of a linear classifier for windy days from the code against the eight measurements and against one linear discriminant analysis feature, and the error an encoder learned on the cold half leaves on the warm half."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-drops] what the code of two numbers drops\")\nkept = 1.0 - np.mean((X - Xhat) ** 2, axis=0)\ncheck(f\"[B-drops] the code of {DPRIME} numbers does not keep the eight \"\n      f\"measurements equally well: of the unit sample variance of each, it \"\n      f\"keeps {100 * kept[2]:.0f} percent of the mean temperature and \"\n      f\"{100 * kept[7]:.0f} percent of the wind speed\",\n      kept[2] > 0.95 and kept[7] < 0.05)\nwind = M[:, 7]\ncode_dist = np.sqrt(((Z[:, None, :] - Z[None, :, :]) ** 2).sum(axis=2))\nnp.fill_diagonal(code_dist, np.inf)\ntwins = [(abs(wind[p] - wind[q]), p, q) for p in range(m)\n         for q in range(p + 1, m) if code_dist[p, q] < 0.1]\ntwins.sort(reverse=True)\nwind_gap, p0, q0 = twins[0]\nrebuilt = Xhat[[p0, q0], 7] * wind.std() + wind.mean()\ncheck(f\"[B-drops] what the dropped directions carried is beyond every \"\n      f\"decoder: of the {len(twins)} pairs of days whose codes lie closer \"\n      f\"than 0.1 together, {stamps[p0]} and {stamps[q0]} differ most in wind \"\n      f\"speed, {wind[p0]:.1f} against {wind[q0]:.1f} meters per second, and \"\n      f\"the decoder returns {rebuilt[0]:.2f} meters per second for both\",\n      wind_gap > 3.0 and abs(rebuilt[0] - rebuilt[1]) < 0.05)\n\nwindy = np.where(wind > np.median(wind), 1.0, -1.0)\n\n\ndef ls_accuracy(F):\n    \"\"\"Accuracy of a linear classifier learned from F by least squares.\"\"\"\n    G = np.hstack([F, np.ones((len(F), 1))])\n    wls = np.linalg.lstsq(G, windy, rcond=None)[0]\n    return float(np.mean(np.sign(G @ wls) == windy))\n\n\nacc_raw, acc_code = ls_accuracy(X), ls_accuracy(Z)\nacc_calm = float(np.mean(windy < 0))\ncheck(f\"[B-drops] a day counts as windy when its mean wind speed lies above \"\n      f\"the median of the {m} values, which holds for \"\n      f\"{int(np.sum(windy > 0))} of them; a linear classifier learned by \"\n      f\"least squares has an accuracy of {100 * acc_raw:.0f} percent from \"\n      f\"all {d} scaled measurements and {100 * acc_code:.0f} percent from \"\n      f\"the {DPRIME} code numbers, against {100 * acc_calm:.0f} percent for \"\n      f\"calling every day calm\",\n      acc_code < acc_raw - 0.2 and acc_code < acc_calm + 0.05)\ncenters = [X[windy == c].mean(axis=0) for c in (-1.0, 1.0)]\nscatter = sum(np.mean(windy == c)\n              * ((X[windy == c] - centers[s]).T @ (X[windy == c] - centers[s]))\n              / np.sum(windy == c) for s, c in enumerate((-1.0, 1.0)))\nw_lda = np.linalg.solve(scatter, centers[1] - centers[0])\nacc_lda = ls_accuracy((X @ w_lda).reshape(-1, 1))\ncheck(f\"[B-drops] one new feature chosen from the labels instead of from \"\n      f\"the sample covariance matrix, the linear discriminant analysis \"\n      f\"direction, reaches {100 * acc_lda:.0f} percent: a code learned to \"\n      f\"reconstruct well is not a code learned to predict well\",\n      acc_lda > acc_code + 0.2)\n\nmonth = np.array([int(day[5:7]) for day in stamps])\ncoldhalf = (month <= 3) | (month >= 10)\nXcold = X[coldhalf]\ncenter_cold = Xcold.mean(axis=0)\nevals_c, evecs_c = np.linalg.eigh(\n    (Xcold - center_cold).T @ (Xcold - center_cold) / len(Xcold))\nWcold = evecs_c[:, np.argsort(evals_c)[::-1][:DPRIME]].T\nAin = Xcold - center_cold\nerr_in = float(np.mean(np.sum((Ain - (Ain @ Wcold.T) @ Wcold) ** 2, axis=1)))\nAout = X[~coldhalf] - center_cold\nerr_out = float(np.mean(np.sum((Aout - (Aout @ Wcold.T) @ Wcold) ** 2,\n                               axis=1)))\nAwarm = X[~coldhalf]\nerr_year = float(np.mean(np.sum((Awarm - (Awarm @ W.T) @ W) ** 2, axis=1)))\ncheck(f\"[B-drops] an encoder is fixed once learned: the PCA encoder of the \"\n      f\"{int(np.sum(coldhalf))} days of January to March and of October to \"\n      f\"December leaves {err_in:.2f} on those days and {err_out:.2f} on the \"\n      f\"other {int(np.sum(~coldhalf))}, against {err_year:.2f} for the \"\n      f\"encoder of the whole year on the same days\",\n      err_out > 3 * err_year)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-plot]** Preview: the two-number code of 21 days, and the reconstruction error against the code length."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-plot] CSVs and the matplotlib preview\")\nwith open(OUT_DIR / \"encoder_code.csv\", \"w\") as fh:\n    fh.write(\"date,z1,z2,warm\\n\")\n    for day, z, wflag in zip(stamps, Z, warm):\n        fh.write(f\"{day},{z[0]:.4f},{z[1]:.4f},{int(wflag)}\\n\")\nkeep = range(0, m, 18)                          # every 18th day, 21 of 366\nwith open(OUT_DIR / \"encoder_code_plot.csv\", \"w\") as fh:\n    fh.write(\"z1,z2,warm,month\\n\")              # numeric columns only\n    for i in keep:\n        fh.write(f\"{Z[i, 0]:.4f},{Z[i, 1]:.4f},{int(warm[i])},\"\n                 f\"{int(stamps[i][5:7])}\\n\")\ncheck(f\"[B-plot] the thinned file holds every 18th day, {len(list(keep))} of \"\n      f\"the {m}\", len(list(keep)) == 21)\nwith open(OUT_DIR / \"encoder_errors.csv\", \"w\") as fh:\n    fh.write(\"dprime,pca,random\\n\")\n    for k, (p, r) in enumerate(zip(errs_pca, errs_rnd), start=1):\n        fh.write(f\"{k},{p:.6f},{r:.6f}\\n\")\n\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9.4, 3.9))\nidx = list(keep)\ncold = [i for i in idx if not warm[i]]\nhot = [i for i in idx if warm[i]]\nax1.plot(Z[cold, 0], Z[cold, 1], ls=\"none\", marker=\"s\", mfc=\"none\",\n         color=\"black\", ms=5, label=\"cold day (open squares)\")\nax1.plot(Z[hot, 0], Z[hot, 1], ls=\"none\", marker=\"o\", color=\"black\", ms=4,\n         label=\"warm day (filled circles)\")\nax1.set_xlabel(\"first code number z1\")\nax1.set_ylabel(\"second code number z2\")\nax1.set_title(\"the two-number code of 21 of the 366 days\")\nax1.legend(frameon=False, fontsize=8)\nks = np.arange(1, d + 1)\nax2.plot(ks, errs_pca, \"k-\", label=\"PCA encoder (solid curve)\")\nax2.plot(ks, errs_rnd, \"k--\",\n         label=\"random orthonormal encoder (dashed curve)\")\nax2.set_xlabel(\"code length d'\")\nax2.set_ylabel(\"average squared reconstruction error\")\nax2.set_title(\"a longer code leaves less to rebuild\")\nax2.legend(frameon=False, fontsize=8)\nfig.tight_layout()\nfig.savefig(OUT_DIR / \"encoder.png\", dpi=110)\n\nn_ok = sum(ok for _, ok in report)\nprint(f\"\\n{n_ok}/{len(report)} checks pass\")\nif n_ok != len(report):\n    raise SystemExit(1)"
  }
 ]
}