{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  },
  "language_info": {
   "name": "python"
  },
  "colab": {
   "name": "cluster.ipynb"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "# cluster \u2014 Python demo\n\nNumerical companion to the entry [cluster](https://dictionaryofml.org/terms/cluster.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 'cluster'. The 366 days of 2024 at Krems are data points with the feature vector (morning minimum temperature, maximum daytime temperature). A cluster is a subset of these days that are more similar to each other than to the days outside the subset, similarity being measured by the Euclidean distance between feature vectors.\n\nRequires NumPy and Matplotlib only, and uses fixed seeds, so the printed numbers reproduce exactly. Generated from [`pythondemos/cluster.py`](https://dictionaryofml.org/terms/cluster.py); CC BY 4.0.\n\nBefore running a downloaded copy, scan it: `uvx bandit -q -t B102,B307,B602,B605 cluster.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(), \"cluster.py\")\nos.makedirs(\"pythondemos\", exist_ok=True)"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "\"\"\"A cluster of the days of one year at Krems an der Donau: a subset of\nsimilar data points, and the three ways a clustering method represents it.\n\nPurpose\n-------\nNumerical companion to the glossary entry 'cluster'.  The 366 days of\n2024 at Krems are data points with the feature vector (morning minimum\ntemperature, maximum daytime temperature).  A cluster is a subset of\nthese days that are more similar to each other than to the days outside\nthe subset, similarity being measured by the Euclidean distance between\nfeature vectors.\n\nThe demo checks the entry's claims on the two clusters of cold and warm\ndays: the two clusters partition the days, and two days of the same\ncluster are closer on average than a cold and a warm day; a cluster\ncentroid represents a cluster as the set of days nearer to it than to the\nother centroid, a half-plane bounded by a straight line; a Gaussian\nmixture model represents a cluster by a multivariate normal distribution\nwhose level sets are elongated ellipses, and assigns every day a degree\nof belonging to each cluster; a similarity graph (here the 10-nearest-\nneighbor graph of the days) represents a cluster as a densely connected\nsubset of nodes, which the two clusters are and a split of the days by\ntheir index is not.\n\nDeterministic: k-means starts from the means of the colder and the\nwarmer half of the days, and the EM algorithm starts from its solution.  Self-contained: numpy + matplotlib only; the GeoSphere\ndownload falls back to the committed CSV.\n\nBlocks\n------\n[B-data]      Download the daily minimum and maximum temperature of 2024\n              at Krems (GeoSphere station 3805) or read the committed\n              copy; 366 days, no missing value.\n[B-subset]    The two clusters of k-means as subsets of the days: a\n              partition into 192 cold and 174 warm days; the average\n              distance between two days of one cluster against the\n              average distance between a cold and a warm day; the\n              nearest other day of almost every day is in its cluster.\n[B-centroid]  The cluster as the set of days nearer to its centroid: the\n              boundary between the two clusters is the straight line\n              through the midpoint of the centroids, at a right angle\n              to their connecting segment.\n[B-gmm]       The cluster as a multivariate normal distribution: the EM\n              algorithm never lowers the log-likelihood, the degrees of\n              belonging of a day sum to one, the level sets of the two\n              distributions are ellipses elongated along the direction\n              in which both temperatures rise, and the days shared\n              between the clusters.\n[B-graph]     The cluster as a densely connected subset of the nodes of\n              the 10-nearest-neighbor graph of the days: the fraction of\n              edges inside the two clusters and the ratio cut of the\n              clusters against that of the odd/even split of the days.\n[B-plot]      Every 18th day for the figure (21 of 366 days) with its\n              day of the year, cluster and degree of belonging, the\n              centroids, the boundary line, the two ellipses, and the\n              3-nearest-neighbor graph of the 21 drawn days.\n\nOutputs\n-------\ncluster_weather.csv       : date, tmin, tmax (committed copy).\ncluster_points_plot.csv   : the 21 drawn days with cluster, degree of\n                            belonging to the warm cluster, shared flag.\ncluster_centroids.csv     : the two cluster centroids.\ncluster_boundary.csv      : two end points of the boundary line.\ncluster_ellipse_cold.csv  : level set of the cold cluster's distribution.\ncluster_ellipse_warm.csv  : level set of the warm cluster's distribution.\ncluster_edges_plot.csv    : edges of the 3-NN graph of the 21 drawn days\n                            as segments (tmin, tmax, u, v) with a flag\n                            for an edge between the two clusters.\ncluster.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 daily minimum and maximum temperature of 2024 at Krems (GeoSphere station 3805) or read the committed copy; 366 days, no missing value."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-data] daily minimum and maximum temperature at Krems, 2024\")\nURL = (\"https://dataset.api.hub.geosphere.at/v1/station/historical/\"\n       \"klima-v2-1d?parameters=tlmin,tlmax&station_ids=3805\"\n       \"&start=2024-01-01&end=2024-12-31\")\nCSV = OUT_DIR / \"cluster_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    records = [(stamp[:10], lo, hi)\n               for stamp, lo, hi in zip(payload[\"timestamps\"],\n                                        params[\"tlmin\"][\"data\"],\n                                        params[\"tlmax\"][\"data\"])]\n    with open(CSV, \"w\") as fh:\n        fh.write(\"date,tmin,tmax\\n\")\n        for day, lo, hi in records:\n            fh.write(f\"{day},{lo},{hi}\\n\")\n    print(f\"    downloaded {len(records)} days\")\nexcept OSError as err:\n    print(f\"    download failed ({err}); reading the committed CSV\")\n    rows = CSV.read_text().strip().splitlines()[1:]\n    records = [(d, float(a), float(b))\n               for d, a, b in (r.split(\",\") for r in rows)]\n\ncheck(\"[B-data] 366 days for 2024\", len(records) == 366)\ncheck(\"[B-data] no day is missing a temperature\",\n      all(lo is not None and hi is not None for _, lo, hi in records))\nX = np.array([[lo, hi] for _, lo, hi in records], dtype=float)\nm = len(X)\nmonths = np.array([int(d[5:7]) for d, _, _ in records])\n\n\ndef pairwise_distances(A, B):\n    \"\"\"Euclidean distances between the rows of A and the rows of B.\"\"\"\n    return np.sqrt(np.sum((A[:, None, :] - B[None, :, :]) ** 2, axis=-1))\n\n\ndef assign(X, W):\n    \"\"\"Index of the nearest centroid for every data point.\"\"\"\n    return np.argmin(pairwise_distances(X, W), axis=1)\n\n\ndef lloyd(X, W0, max_iter=500):\n    \"\"\"k-means (alternating assignment and averaging) from the centroids\n    W0.\"\"\"\n    W = np.array(W0, dtype=float)\n    c = assign(X, W)\n    for _ in range(max_iter):\n        W = np.array([X[c == j].mean(axis=0) for j in range(len(W))])\n        c_new = assign(X, W)\n        if np.array_equal(c_new, c):\n            break\n        c = c_new\n    return W, c"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-subset]** The two clusters of k-means as subsets of the days: a partition into 192 cold and 174 warm days; the average distance between two days of one cluster against the average distance between a cold and a warm day; the nearest other day of almost every day is in its cluster."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-subset] the two clusters of k-means as subsets of the days\")\norder = np.argsort(X[:, 0])\nW0 = np.array([X[order[: m // 2]].mean(axis=0), X[order[m // 2:]].mean(axis=0)])\nW, c = lloyd(X, W0)\ncold, warm = sorted(range(2), key=lambda j: W[j, 1])\nC_cold = np.flatnonzero(c == cold)\nC_warm = np.flatnonzero(c == warm)\ncheck(f\"[B-subset] the two clusters partition the days: {len(C_cold)} cold \"\n      f\"and {len(C_warm)} warm days, none in both, none in neither\",\n      len(C_cold) + len(C_warm) == m\n      and len(np.intersect1d(C_cold, C_warm)) == 0)\ncheck(\"[B-subset] every January and February day is a cold day, every \"\n      \"July and August day a warm day\",\n      np.all(c[np.isin(months, [1, 2])] == cold)\n      and np.all(c[np.isin(months, [7, 8])] == warm))\nD = pairwise_distances(X, X)\noff = ~np.eye(m, dtype=bool)\nwithin = np.concatenate([D[np.ix_(C_cold, C_cold)][off[: len(C_cold),\n                                                       : len(C_cold)]],\n                         D[np.ix_(C_warm, C_warm)][off[: len(C_warm),\n                                                       : len(C_warm)]]])\nbetween = D[np.ix_(C_cold, C_warm)].ravel()\nd_within, d_between = within.mean(), between.mean()\ncheck(f\"[B-subset] two days of one cluster are {d_within:.1f} degrees \"\n      f\"apart on average, a cold and a warm day {d_between:.1f}\",\n      d_within < d_between / 2)\nD_self = D + np.diag(np.full(m, np.inf))\nnn = np.argmin(D_self, axis=1)\nsame = np.mean(c[nn] == c)\ncheck(f\"[B-subset] the nearest other day of {100 * same:.1f}% of the days \"\n      f\"({int(np.sum(c[nn] == c))} of {m}) lies in the same cluster\",\n      same > 0.95)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-centroid]** The cluster as the set of days nearer to its centroid: the boundary between the two clusters is the straight line through the midpoint of the centroids, at a right angle to their connecting segment."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-centroid] the cluster as the set of days nearer to its centroid\")\ncheck(\"[B-centroid] each centroid is the mean of the days of its cluster\",\n      all(np.allclose(W[j], X[c == j].mean(axis=0)) for j in range(2)))\ncheck(f\"[B-centroid] cold centroid ({W[cold, 0]:.1f}, {W[cold, 1]:.1f}), \"\n      f\"warm centroid ({W[warm, 0]:.1f}, {W[warm, 1]:.1f}); every day lies \"\n      \"in the cluster of its nearer centroid\",\n      np.array_equal(assign(X, W), c))\nmid = (W[cold] + W[warm]) / 2\ndirection = W[warm] - W[cold]\nside = (X - mid) @ direction\ncheck(\"[B-centroid] the boundary is the straight line through the midpoint \"\n      \"of the centroids at a right angle to their segment: a day is warm \"\n      \"exactly when it lies on the warm side of that line\",\n      np.array_equal(side > 0, c == warm))\nperp = np.array([-direction[1], direction[0]])\nperp = perp / np.linalg.norm(perp)\nXMIN, XMAX = -9.0, 23.0\nt_lo = (XMIN - mid[0]) / perp[0]\nt_hi = (XMAX - mid[0]) / perp[0]\nboundary = np.array([mid + t_lo * perp, mid + t_hi * perp])"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-gmm]** The cluster as a multivariate normal distribution: the EM algorithm never lowers the log-likelihood, the degrees of belonging of a day sum to one, the level sets of the two distributions are ellipses elongated along the direction in which both temperatures rise, and the days shared between the clusters."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-gmm] the cluster as a multivariate normal distribution\")\n\n\ndef mvn_pdf(X, mu, S):\n    d = X - mu\n    Si = np.linalg.inv(S)\n    q = np.einsum(\"ij,jk,ik->i\", d, Si, d)\n    return np.exp(-0.5 * q) / (2 * np.pi * np.sqrt(np.linalg.det(S)))\n\n\ndef em(X, mu, S, p, iters=300):\n    \"\"\"EM for a two-component GMM; returns the parameters, the degrees of\n    belonging, and the log-likelihood after every iteration.\"\"\"\n    loglik = []\n    for _ in range(iters):\n        dens = np.stack([p[j] * mvn_pdf(X, mu[j], S[j]) for j in range(2)],\n                        axis=1)\n        loglik.append(float(np.sum(np.log(dens.sum(axis=1)))))\n        U = dens / dens.sum(axis=1, keepdims=True)\n        nk = U.sum(axis=0)\n        p = nk / len(X)\n        mu = (U.T @ X) / nk[:, None]\n        S = np.array([((U[:, j, None] * (X - mu[j])).T @ (X - mu[j])) / nk[j]\n                      for j in range(2)])\n    return mu, S, p, U, loglik\n\n\nmu0 = W.copy()\nS0 = np.array([np.cov(X[c == j].T) for j in range(2)])\np0 = np.array([np.mean(c == j) for j in range(2)])\nmu, S, p, U, loglik = em(X, mu0, S0, p0)\ncheck(f\"[B-gmm] the EM algorithm never lowers the log-likelihood over \"\n      f\"{len(loglik) - 1} steps ({loglik[0]:.1f} -> {loglik[-1]:.1f})\",\n      all(b >= a - 1e-6 for a, b in zip(loglik, loglik[1:])))\ncheck(\"[B-gmm] the two degrees of belonging of every day are nonnegative \"\n      \"and sum to one\",\n      np.all(U >= 0) and np.allclose(U.sum(axis=1), 1.0))\naxes = {}\nfor j, name in ((cold, \"cold\"), (warm, \"warm\")):\n    ev, evec = np.linalg.eigh(S[j])\n    long_axis = evec[:, 1]\n    angle = np.degrees(np.arctan2(abs(long_axis[1]), abs(long_axis[0])))\n    axes[name] = (np.sqrt(ev[1]) / np.sqrt(ev[0]), angle, ev, evec)\ncheck(f\"[B-gmm] the level sets are ellipses: long axis over short axis \"\n      f\"{axes['cold'][0]:.1f} (cold) and {axes['warm'][0]:.1f} (warm), both \"\n      f\"tilted by {axes['cold'][1]:.0f} and {axes['warm'][1]:.0f} degrees, so \"\n      \"both temperatures rise together along the long axis\",\n      all(a[0] > 1.5 and 30 < a[1] < 60 for a in axes.values()))\ncheck(f\"[B-gmm] means ({mu[cold, 0]:.1f}, {mu[cold, 1]:.1f}) and \"\n      f\"({mu[warm, 0]:.1f}, {mu[warm, 1]:.1f}) differ from the centroids\",\n      not np.allclose(mu, W, atol=0.5))\nu_warm = U[:, warm]\nshared = (u_warm > 0.2) & (u_warm < 0.8)\nn_shared = int(np.sum(shared))\ncheck(f\"[B-gmm] {n_shared} of the {m} days have a degree of belonging \"\n      \"between 0.2 and 0.8 to both clusters\", 20 < n_shared < 120)\ng = np.where(u_warm > 0.5, warm, cold)\nagree = np.mean(g == c)\ncheck(f\"[B-gmm] the larger degree of belonging names the k-means cluster \"\n      f\"on {100 * agree:.1f}% of the days ({int(np.sum(g == c))} of {m})\",\n      agree > 0.85)\nn_certain = int(np.sum((u_warm < 0.01) | (u_warm > 0.99)))\nprint(f\"    {n_certain} days have a degree of belonging below 0.01 or \"\n      \"above 0.99\")\nshared_months = sorted(set(months[shared].tolist()))\ncheck(f\"[B-gmm] the shared days fall in the months {shared_months}: none \"\n      \"from November to February\",\n      not np.any(np.isin(months[shared], [11, 12, 1, 2])))\nt = np.linspace(0, 2 * np.pi, 181)\nellipses = {}\nfor j, name in ((cold, \"cold\"), (warm, \"warm\")):\n    ev, evec = axes[name][2], axes[name][3]\n    circ = np.stack([np.cos(t), np.sin(t)])\n    ellipses[name] = (mu[j][:, None] + evec @ (2 * np.sqrt(ev)[:, None]\n                                               * circ)).T"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-graph]** The cluster as a densely connected subset of the nodes of the 10-nearest-neighbor graph of the days: the fraction of edges inside the two clusters and the ratio cut of the clusters against that of the odd/even split of the days."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-graph] the cluster as a densely connected subset of the nodes of \"\n      \"the 10-nearest-neighbor graph\")\nK_NN = 10\nnbrs = np.argsort(D_self, axis=1)[:, :K_NN]\nA = np.zeros((m, m), dtype=int)\nfor i in range(m):\n    A[i, nbrs[i]] = 1\nA = np.maximum(A, A.T)\nn_edges = int(A.sum() // 2)\ninside = int(A[np.ix_(C_cold, C_cold)].sum() // 2\n             + A[np.ix_(C_warm, C_warm)].sum() // 2)\ncrossing = n_edges - inside\ncheck(f\"[B-graph] the graph has {n_edges} edges; {inside} of them \"\n      f\"({100 * inside / n_edges:.1f}%) join two days of the same cluster \"\n      f\"and {crossing} join a cold with a warm day\",\n      inside / n_edges > 0.9)\n\n\ndef ratio_cut(A, part):\n    \"\"\"Sum over the two subsets of the cut weight divided by the size.\"\"\"\n    total = 0.0\n    for sub in (part, ~part):\n        total += A[np.ix_(sub, ~sub)].sum() / sub.sum()\n    return float(total)\n\n\nrc_cluster = ratio_cut(A, c == warm)\nrc_index = ratio_cut(A, np.arange(m) % 2 == 1)\ncheck(f\"[B-graph] ratio cut {rc_cluster:.2f} for the two clusters against \"\n      f\"{rc_index:.2f} for the split into odd and even days\",\n      rc_cluster < rc_index / 10)\nseen = np.zeros(m, dtype=bool)\nn_comp = 0\nfor s in range(m):\n    if seen[s]:\n        continue\n    n_comp += 1\n    stack = [s]\n    seen[s] = True\n    while stack:\n        i = stack.pop()\n        for j in np.flatnonzero(A[i]):\n            if not seen[j]:\n                seen[j] = True\n                stack.append(j)\nprint(f\"    the graph has {n_comp} connected component(s)\")"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-plot]** Every 18th day for the figure (21 of 366 days) with its day of the year, cluster and degree of belonging, the centroids, the boundary line, the two ellipses, and the 3-nearest-neighbor graph of the 21 drawn days."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-plot] thinned scatter, centroids, boundary, ellipses, graph\")\nidx = np.arange(0, m, 18)\ncheck(f\"[B-plot] every 18th day: {len(idx)} of the {m} days are drawn\",\n      len(idx) == 21)\nwith open(OUT_DIR / \"cluster_points_plot.csv\", \"w\") as fh:\n    fh.write(\"doy,tmin,tmax,cluster,dobwarm,shared,gcluster\\n\")\n    for i in idx:\n        fh.write(f\"{i + 1},{X[i, 0]:.1f},{X[i, 1]:.1f},{int(c[i] == warm)},\"\n                 f\"{u_warm[i]:.3f},{int(shared[i])},{int(g[i] == warm)}\\n\")\nwith open(OUT_DIR / \"cluster_centroids.csv\", \"w\") as fh:\n    fh.write(\"tmin,tmax,cluster\\n\")\n    for j in (cold, warm):\n        fh.write(f\"{W[j, 0]:.4f},{W[j, 1]:.4f},{int(j == warm)}\\n\")\nwith open(OUT_DIR / \"cluster_boundary.csv\", \"w\") as fh:\n    fh.write(\"tmin,tmax\\n\")\n    for pt in boundary:\n        fh.write(f\"{pt[0]:.4f},{pt[1]:.4f}\\n\")\nfor name in (\"cold\", \"warm\"):\n    with open(OUT_DIR / f\"cluster_ellipse_{name}.csv\", \"w\") as fh:\n        fh.write(\"tmin,tmax\\n\")\n        for pt in ellipses[name]:\n            fh.write(f\"{pt[0]:.4f},{pt[1]:.4f}\\n\")\nXp = X[idx]\nDp = pairwise_distances(Xp, Xp) + np.diag(np.full(len(idx), np.inf))\nnb3 = np.argsort(Dp, axis=1)[:, :3]\nAp = np.zeros((len(idx), len(idx)), dtype=int)\nfor i in range(len(idx)):\n    Ap[i, nb3[i]] = 1\nAp = np.maximum(Ap, Ap.T)\nedges_plot = [(i, j) for i in range(len(idx)) for j in range(i + 1, len(idx))\n              if Ap[i, j]]\nn_cross_plot = sum(int(c[idx[i]] != c[idx[j]]) for i, j in edges_plot)\ncheck(f\"[B-plot] the 3-nearest-neighbor graph of the 21 drawn days has \"\n      f\"{len(edges_plot)} edges, {n_cross_plot} of them between the clusters\",\n      n_cross_plot <= 3)\nwith open(OUT_DIR / \"cluster_edges_plot.csv\", \"w\") as fh:\n    fh.write(\"tmin,tmax,u,v,cross\\n\")\n    for i, j in edges_plot:\n        fh.write(f\"{Xp[i, 0]:.1f},{Xp[i, 1]:.1f},\"\n                 f\"{Xp[j, 0] - Xp[i, 0]:.1f},{Xp[j, 1] - Xp[i, 1]:.1f},\"\n                 f\"{int(c[idx[i]] != c[idx[j]])}\\n\")\n\nfig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(9.6, 8.2))\nfor ax in (ax1, ax2, ax3):\n    ax.set_xlim(XMIN, XMAX)\n    ax.set_ylim(0, 50)\n# (a) centroid representation\nax1.set_xlabel(\"morning minimum temperature (\u00b0C)\")\nax1.set_ylabel(\"maximum daytime temperature (\u00b0C)\")\nfor j, mk, lab in ((cold, \"o\", \"cold days\"), (warm, \"s\", \"warm days\")):\n    sel = idx[c[idx] == j]\n    ax1.scatter(X[sel, 0], X[sel, 1], marker=mk, facecolors=\"none\",\n                edgecolors=\"black\", s=28, label=f\"{lab} (every 18th day)\")\nax1.scatter(W[:, 0], W[:, 1], marker=\"x\", color=\"black\", s=60,\n            label=\"cluster centroids\")\nax1.plot(boundary[:, 0], boundary[:, 1], \"k--\", lw=1, label=\"boundary\")\nax1.set_title(\"(a) cluster = days nearer to a centroid\")\nax1.legend(frameon=False, fontsize=8, loc=\"upper left\")\n# (b) distribution representation\nax2.set_xlabel(\"morning minimum temperature (\u00b0C)\")\nax2.set_ylabel(\"maximum daytime temperature (\u00b0C)\")\nfor j, mk, lab in ((cold, \"o\", \"cold\"), (warm, \"s\", \"warm\")):\n    sel = idx[(g[idx] == j) & ~shared[idx]]\n    ax2.scatter(X[sel, 0], X[sel, 1], marker=mk, facecolors=\"none\",\n                edgecolors=\"black\", s=28, label=f\"{lab} (larger degree)\")\n    sel = idx[(g[idx] == j) & shared[idx]]\n    ax2.scatter(X[sel, 0], X[sel, 1], marker=mk, color=\"black\", s=28,\n                label=f\"{lab}, shared (filled)\")\nax2.plot(ellipses[\"cold\"][:, 0], ellipses[\"cold\"][:, 1], \"k-\", lw=1,\n         label=\"cold distribution\")\nax2.plot(ellipses[\"warm\"][:, 0], ellipses[\"warm\"][:, 1], \"k--\", lw=1,\n         label=\"warm distribution\")\nax2.set_title(\"(b) cluster = normal distribution\")\nax2.legend(frameon=False, fontsize=7, loc=\"upper left\")\n# (c) graph representation\nax3.set_xlabel(\"morning minimum temperature (\u00b0C)\")\nax3.set_ylabel(\"maximum daytime temperature (\u00b0C)\")\nfor i, j in edges_plot:\n    style = \"k--\" if c[idx[i]] != c[idx[j]] else \"k-\"\n    ax3.plot([Xp[i, 0], Xp[j, 0]], [Xp[i, 1], Xp[j, 1]], style, lw=0.7)\nfor j, mk, lab in ((cold, \"o\", \"cold days\"), (warm, \"s\", \"warm days\")):\n    sel = idx[c[idx] == j]\n    ax3.scatter(X[sel, 0], X[sel, 1], marker=mk, facecolors=\"white\",\n                edgecolors=\"black\", s=28, zorder=3, label=lab)\nax3.plot([], [], \"k-\", lw=0.7, label=\"edge of the 3-NN graph (thin line)\")\nax3.set_title(\"(c) cluster = densely connected nodes\")\nax3.legend(frameon=False, fontsize=8, loc=\"upper left\")\n# (d) hard against soft assignment over the year\nax4.set_xlabel(\"day of the year\")\nax4.set_ylabel(\"assignment to the warm cluster\")\nax4.scatter(idx + 1, (c[idx] == warm).astype(float), marker=\"o\",\n            facecolors=\"none\", edgecolors=\"black\", s=28,\n            label=\"k-means: 0 or 1 (circles)\")\nax4.scatter(idx + 1, u_warm[idx], marker=\"x\", color=\"black\", s=36,\n            label=\"GMM: degree of belonging (crosses)\")\nax4.set_xlim(0, 370)\nax4.set_ylim(-0.05, 1.25)\nax4.set_title(\"(d) hard against soft assignment\")\nax4.legend(frameon=False, fontsize=8, loc=\"upper left\")\nfig.tight_layout()\nfig.savefig(OUT_DIR / \"cluster.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)"
  }
 ]
}