{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  },
  "language_info": {
   "name": "python"
  },
  "colab": {
   "name": "sensattr.ipynb"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "# sensitive attribute \u2014 Python demo\n\nNumerical companion to the entry [sensitive attribute](https://dictionaryofml.org/terms/sensattr.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 'sensattr' (sensitive attribute). The entry claims that dropping an attribute from the feature vector does not remove it from the data, because learned features can be predictive of it, and that a linear classifier on the learned features recovers the dropped attribute with high accuracy. This script measures that recovery accuracy on real data.\n\nRequires NumPy and Matplotlib only, and uses fixed seeds, so the printed numbers reproduce exactly. Generated from [`pythondemos/sensattr.py`](https://dictionaryofml.org/terms/sensattr.py); CC BY 4.0.\n\nBefore running a downloaded copy, scan it: `uvx bandit -q -t B102,B307,B602,B605 sensattr.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(), \"sensattr.py\")\nos.makedirs(\"pythondemos\", exist_ok=True)"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "\"\"\"An attribute that is not among the features is still recovered from\nlearned features: the cold half of the year at Krems an der Donau, read\noff two principal components of weather measurements that never include\nthe half-year.\n\nPurpose\n-------\nNumerical companion to the glossary entry 'sensattr' (sensitive\nattribute).  The entry claims that dropping an attribute from the\nfeature vector does not remove it from the data, because learned\nfeatures can be predictive of it, and that a linear classifier on the\nlearned features recovers the dropped attribute with high accuracy.\nThis script measures that recovery accuracy on real data.\n\nEach day from January 6 to December 31 of 2024 at the GeoSphere Austria\nstation Krems (station id 3805) is a data point.  Its attribute s says\nwhether the day lies in the cold half of the year (October through\nMarch), and s is never used as a feature.  The raw features are the\neight daily measurements of each of the five previous days, forty\nnumbers, the same feature vector the entry 'featlearn' uses to forecast\nthe maximum temperature.  Principal component analysis on the\nstandardized raw features delivers two learned features, and a linear\nclassifier learned by least squares on those two features predicts s on\ndays it was not learned from.\n\nBecause temperature is an obvious proxy for the half-year, the same\nmeasurement is repeated with every temperature measurement removed from\nthe raw features: twenty-five numbers per day, holding precipitation,\nsunshine duration, humidity, air pressure and wind speed only.  The\nrecovery accuracy stays far above the majority-class rate.  A last\nblock projects the learned features onto the orthogonal complement of\nthe direction the classifier used and learns a new classifier there,\nwhich still beats that rate: removing one direction does not remove the\nattribute.\n\nDeterministic: the data are a fixed archive year, the learned features\nare an eigenvalue decomposition, and the twenty halvings of the days\nthat the accuracies average over are drawn with fixed seeds.\nSelf-contained (numpy and matplotlib only, stdlib urllib for the\ndownload, which falls back to the committed CSV).\n\nBlocks\n------\n[B-fetch]     Download the eight daily measurements at Krems for 2024\n              and write them to sensattr_weather.csv; check the count\n              and one pinned value against the archive.\n[B-attribute] Build the 361 data points: forty raw features (five\n              previous days times eight measurements) and the attribute\n              s (cold half of the year), which is not a feature; scale\n              every raw feature to zero sample mean and unit sample\n              variance.\n[B-features]  Learn two features: the two eigenvectors of the sample\n              covariance matrix with the largest eigenvalues.\n[B-recover]   A linear classifier learned by least squares on the two\n              learned features predicts s on the days it was not\n              learned from; the accuracy, averaged over twenty random\n              halvings of the 361 days, against the majority-class\n              rate, against the raw features, and for one to ten\n              learned features.  Repeated with every temperature\n              measurement removed from the raw features.\n[B-remove]    Projecting ten learned features onto the orthogonal\n              complement of the classifier's direction and learning\n              again there: the accuracy drops but stays above the\n              majority-class rate.\n[B-plot]      Preview: the twenty-one drawn days in the two learned\n              features with the decision boundary, and the accuracy\n              against the number of learned features kept.\n\nOutputs\n-------\nsensattr_weather.csv     : date and the eight measurements, 366 days of 2024\nsensattr_points_plot.csv : z1, z2, s of the 21 days drawn in the entry's\n                           Fig. 1(a) (every 18th day in calendar order)\nsensattr_boundary.csv    : the two endpoints of the decision boundary drawn\n                           in Fig. 1(a)\nsensattr_accuracy.csv    : number of learned features, recovery accuracy\n                           with all eight measurements and with every\n                           temperature measurement removed\nsensattr.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 = []                         # collects (check name, pass/fail) pairs\n\n\ndef check(name, ok):                # records and prints one verification\n    report.append((name, bool(ok)))\n    print(f\"  [{'ok' if ok else 'FAIL'}] {name}\")"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-fetch]** Download the eight daily measurements at Krems for 2024 and write them to sensattr_weather.csv; check the count and one pinned value against the archive."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-fetch] 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 / \"sensattr_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 trace of 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 = [line.split(\",\") for line in\n            CSV.read_text().strip().splitlines()[1:]]\n    stamps = [row[0] for row in rows]\n    M = np.array([[float(v) for v in row[1:]] for row in rows])\n\ncheck(\"[B-fetch] 366 days with eight measurements for 2024\",\n      M.shape == (366, 8))\ncheck(\"[B-fetch] the record matches the archive (Feb 1: -3.8 to 10.4 deg)\",\n      stamps[31] == \"2024-02-01\" and np.allclose(M[31, :2], [-3.8, 10.4]))"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-attribute]** Build the 361 data points: forty raw features (five previous days times eight measurements) and the attribute s (cold half of the year), which is not a feature; scale every raw feature to zero sample mean and unit sample variance."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-attribute] 361 days, forty raw features, the attribute s\")\nLAG = 5\ndays = np.arange(LAG, len(M))                   # January 6 .. December 31\nmonth = np.array([int(stamps[t][5:7]) for t in days])\ns = ((month <= 3) | (month >= 10)).astype(int)  # 1: cold half of the year\nt_pm = 2 * s - 1                                # the same attribute as +-1\nm = len(days)\nTEMPERATURE = [0, 1, 2]                         # tlmin, tlmax, tl_mittel\nALL_EIGHT = list(range(8))\nNO_TEMPERATURE = [j for j in ALL_EIGHT if j not in TEMPERATURE]\n\n\ndef raw_features(columns):\n    \"\"\"The measurements of the five previous days, one row per day,\n    each raw feature scaled to zero sample mean and unit sample variance.\"\"\"\n    raw = np.stack([M[day - LAG:day][:, columns].ravel() for day in days])\n    return (raw - raw.mean(axis=0)) / raw.std(axis=0)\n\n\nX = raw_features(ALL_EIGHT)\nX_no_temp = raw_features(NO_TEMPERATURE)\nmajority = float(max(s.mean(), 1 - s.mean()))\ncheck(f\"[B-attribute] {m} days with {X.shape[1]} raw features each; \"\n      f\"{int(s.sum())} of them lie in the cold half of the year, so the \"\n      f\"majority-class rate is {majority:.3f}\",\n      (m, X.shape[1]) == (361, 40) and X_no_temp.shape[1] == 25)\ncheck(\"[B-attribute] the attribute s is not among the raw features: no \"\n      \"raw feature is a function of the half-year alone\",\n      min(X[s == 1, 0].min(), X[s == 0, 0].min()) < 0 <\n      max(X[s == 1, 0].max(), X[s == 0, 0].max()))\ncheck(\"[B-attribute] every scaled raw feature has zero sample mean and \"\n      \"unit sample variance\",\n      np.allclose(X.mean(axis=0), 0.0) and np.allclose(X.var(axis=0), 1.0))"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-features]** Learn two features: the two eigenvectors of the sample covariance matrix with the largest eigenvalues."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-features] two learned features from the raw ones\")\n\n\ndef learned_features(raw, nr_kept):\n    \"\"\"The nr_kept eigenvectors of the sample covariance matrix with the\n    largest eigenvalues, applied to the raw features; also the eigenvalues.\"\"\"\n    cov = raw.T @ raw / len(raw)\n    eigvals, eigvecs = np.linalg.eigh(cov)\n    order = np.argsort(eigvals)[::-1]\n    return raw @ eigvecs[:, order[:nr_kept]], eigvals[order]\n\n\nZ, eigvals = learned_features(X, 2)\nZ_no_temp, eigvals_no_temp = learned_features(X_no_temp, 2)\ncheck(f\"[B-features] the largest eigenvalue carries \"\n      f\"{100 * eigvals[0] / eigvals.sum():.0f} percent of the sum of all \"\n      f\"40 eigenvalues, the second {100 * eigvals[1] / eigvals.sum():.0f} \"\n      f\"percent\", eigvals[0] > eigvals[1] > eigvals[2])\ncheck(\"[B-features] the two learned features have zero sample mean, so \"\n      \"no learned feature encodes the half-year by an offset\",\n      np.allclose(Z.mean(axis=0), 0.0, atol=1e-9))"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-recover]** A linear classifier learned by least squares on the two learned features predicts s on the days it was not learned from; the accuracy, averaged over twenty random halvings of the 361 days, against the majority-class rate, against the raw features, and for one to ten learned features. Repeated with every temperature measurement removed from the raw features."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-recover] recovery accuracy of a linear classifier on the \"\n      \"learned features\")\nNR_SPLITS = 20\n\n\ndef halvings():\n    \"\"\"Twenty halvings of the days into a training and a test set.\"\"\"\n    for seed in range(NR_SPLITS):\n        order = np.random.default_rng(seed).permutation(m)\n        yield order[:m // 2], order[m // 2:]\n\n\ndef classifier(features, train):\n    \"\"\"A linear classifier learned by least squares on the training days,\n    returned as its weight vector with the intercept last.\"\"\"\n    design = np.hstack([features, np.ones((len(features), 1))])\n    return np.linalg.lstsq(design[train], t_pm[train], rcond=None)[0]\n\n\ndef recovery_accuracy(features):\n    \"\"\"The accuracy of that classifier on the days it was not learned\n    from, averaged over the twenty halvings.\"\"\"\n    accs = []\n    for train, test in halvings():\n        weights = classifier(features, train)\n        design = np.hstack([features, np.ones((len(features), 1))])\n        accs.append(float((np.sign(design[test] @ weights)\n                           == t_pm[test]).mean()))\n    return float(np.mean(accs)), float(np.min(accs)), float(np.max(accs))\n\n\nacc_two, lo_two, hi_two = recovery_accuracy(Z)\nacc_raw = recovery_accuracy(X)[0]\nacc_two_nt, lo_two_nt, hi_two_nt = recovery_accuracy(Z_no_temp)\nacc_raw_nt = recovery_accuracy(X_no_temp)[0]\ncheck(f\"[B-recover] two learned features recover s with accuracy \"\n      f\"{acc_two:.3f} (over the twenty halvings between {lo_two:.3f} and \"\n      f\"{hi_two:.3f}), against the majority-class rate {majority:.3f}\",\n      acc_two > 0.9 > majority)\ncheck(f\"[B-recover] the forty raw features themselves give {acc_raw:.3f}, \"\n      f\"so the two learned features keep what the raw ones carry about s\",\n      acc_two > acc_raw - 0.05)\ncheck(f\"[B-recover] with every temperature measurement removed, two \"\n      f\"learned features still give {acc_two_nt:.3f} (between \"\n      f\"{lo_two_nt:.3f} and {hi_two_nt:.3f}) and the 25 raw features \"\n      f\"{acc_raw_nt:.3f}\", acc_two_nt > 0.8)\ncurve = []\nfor nr_kept in range(1, 11):\n    a_all = recovery_accuracy(learned_features(X, nr_kept)[0])[0]\n    a_nt = recovery_accuracy(learned_features(X_no_temp, nr_kept)[0])[0]\n    curve.append((nr_kept, a_all, a_nt))\n    print(f\"    {nr_kept:2d} learned features: {a_all:.3f} with all eight \"\n          f\"measurements, {a_nt:.3f} with every temperature removed\")\ncheck(\"[B-recover] one single learned feature already recovers s with \"\n      f\"accuracy {curve[0][1]:.3f}\", curve[0][1] > 0.85)\ncheck(\"[B-recover] the accuracy stays above the majority-class rate for \"\n      \"every number of kept learned features\",\n      all(a > majority and b > majority for _, a, b in curve))"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-remove]** Projecting ten learned features onto the orthogonal complement of the classifier's direction and learning again there: the accuracy drops but stays above the majority-class rate."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-remove] the attribute after one direction is projected out\")\nZ_ten = learned_features(X, 10)[0]\ntrain, test = next(iter(halvings()))\nweights_ten = classifier(Z_ten, train)\ndirection = weights_ten[:-1] / np.linalg.norm(weights_ten[:-1])\nZ_perp = Z_ten - np.outer(Z_ten @ direction, direction)\ndesign = np.hstack([Z_ten, np.ones((m, 1))])\nacc_before = float((np.sign(design[test] @ weights_ten) == t_pm[test]).mean())\nweights_perp = classifier(Z_perp, train)\ndesign_perp = np.hstack([Z_perp, np.ones((m, 1))])\nacc_after = float((np.sign(design_perp[test] @ weights_perp)\n                   == t_pm[test]).mean())\ncheck(\"[B-remove] the projected learned features carry no component \"\n      \"along the direction\", np.allclose(Z_perp @ direction, 0.0, atol=1e-9))\ncheck(f\"[B-remove] ten learned features recover s with accuracy \"\n      f\"{acc_before:.3f} on this halving; after the direction is projected \"\n      f\"out, a new classifier still reaches {acc_after:.3f}, above the \"\n      f\"majority-class rate {majority:.3f}\",\n      acc_before > acc_after > majority)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-plot]** Preview: the twenty-one drawn days in the two learned features with the decision boundary, and the accuracy against the number of learned features kept."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-plot] the days drawn in the figure, the boundary, and the preview\")\ndrawn = np.arange(0, m, 18)                 # every 18th day, 21 days\nwith open(OUT_DIR / \"sensattr_points_plot.csv\", \"w\") as fh:\n    fh.write(\"z1,z2,s\\n\")\n    for i in drawn:\n        fh.write(f\"{Z[i, 0]:.3f},{Z[i, 1]:.3f},{s[i]}\\n\")\nweights_two = classifier(Z, train)\nz1_lo, z1_hi = float(Z[drawn, 0].min()), float(Z[drawn, 0].max())\nz2_lo, z2_hi = float(Z[drawn, 1].min()), float(Z[drawn, 1].max())\n\n\ndef boundary_z2(z1):\n    \"\"\"The second learned feature on the decision boundary.\"\"\"\n    return -(weights_two[0] * z1 + weights_two[2]) / weights_two[1]\n\n\n# the drawn segment of the boundary: the part of the line that stays\n# inside the range of the drawn days in both learned features\non_line = np.linspace(z1_lo, z1_hi, 2001)\ninside = on_line[(boundary_z2(on_line) >= z2_lo)\n                 & (boundary_z2(on_line) <= z2_hi)]\nends = (float(inside[0]), float(inside[-1]))\nwith open(OUT_DIR / \"sensattr_boundary.csv\", \"w\") as fh:\n    fh.write(\"z1,z2\\n\")\n    for z1 in ends:\n        fh.write(f\"{z1:.3f},{boundary_z2(z1):.3f}\\n\")\nwith open(OUT_DIR / \"sensattr_accuracy.csv\", \"w\") as fh:\n    fh.write(\"kept,acc_all,acc_notemp\\n\")\n    for nr_kept, a_all, a_nt in curve:\n        fh.write(f\"{nr_kept},{a_all:.4f},{a_nt:.4f}\\n\")\ndrawn_right = float(np.mean(\n    (np.sign(np.hstack([Z[drawn], np.ones((len(drawn), 1))]) @ weights_two)\n     == t_pm[drawn])))\ncheck(f\"[B-plot] of the {len(drawn)} days drawn in the figure (every \"\n      f\"18th day in calendar order), the boundary puts \"\n      f\"{drawn_right * len(drawn):.0f} on the side of their own attribute \"\n      f\"value\", drawn_right >= 0.85)\n\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))\ncold, warm = s[drawn] == 1, s[drawn] == 0\nax1.scatter(Z[drawn][cold, 0], Z[drawn][cold, 1], marker=\"o\", color=\"black\",\n            s=22, label=\"cold half of the year (filled circles)\")\nax1.scatter(Z[drawn][warm, 0], Z[drawn][warm, 1], marker=\"s\",\n            facecolors=\"none\", edgecolors=\"black\", s=26,\n            label=\"warm half of the year (open squares)\")\nax1.plot(list(ends), [boundary_z2(ends[0]), boundary_z2(ends[1])], \"k-\",\n         lw=1.0, label=\"decision boundary (solid line)\")\nax1.set_xlabel(\"learned feature $z_1$\")\nax1.set_ylabel(\"learned feature $z_2$\")\nax1.set_title(\"21 days in two learned features\\nbuilt without the half-year\")\nax1.legend(frameon=False, fontsize=8)\nkept = [c[0] for c in curve]\nax2.plot(kept, [c[1] for c in curve], \"k-o\", ms=4,\n         label=\"all eight measurements (solid curve, filled circles)\")\nax2.plot(kept, [c[2] for c in curve], \"k--s\", ms=4, mfc=\"none\",\n         label=\"every temperature removed (dashed curve, open squares)\")\nax2.axhline(majority, color=\"black\", ls=\":\", lw=1.0,\n            label=\"majority-class rate (dotted line)\")\nax2.set_ylim(0.4, 1.02)\nax2.set_xlabel(\"number of learned features kept\")\nax2.set_ylabel(\"recovery accuracy of the attribute\")\nax2.set_title(\"the attribute is recovered from a few learned features\")\nax2.legend(frameon=False, fontsize=8, loc=\"center left\")\nfig.tight_layout()\nfig.savefig(OUT_DIR / \"sensattr.png\", dpi=110)\n\nprint()\nfailed = [name for name, ok in report if not ok]\nprint(f\"{len(report) - len(failed)}/{len(report)} checks passed\"\n      + (f\"; FAILED: {failed}\" if failed else \"\"))\nif failed:\n    raise SystemExit(1)"
  }
 ]
}