Compression Is Learning: An Afternoon With scikit-learn on Your Own Machine
A hands-on scikit-learn tutorial: compress a facade photo with k-means, see why prediction equals compression, and keep every pixel inside Swiss jurisdiction.
Start from a result that should unsettle anyone who thinks of a machine-learning model as a black box that only ever guesses: prediction and compression are the same act. A system that estimates the probability of the next symbol given everything it has seen can drive an arithmetic coder to optimal compression; an optimal compressor can be run backwards to predict. DeepMind made this concrete when it turned its Chinchilla 70B language model loose as a lossless compressor and it squeezed image data to 43.4% and audio to 16.4% of original size — beating PNG on images and FLAC on audio at their own job. (The honest caveat, which the researchers name themselves: some test data likely overlapped the training set, so read the numbers as a ceiling, not a promise.)
That equivalence — laid out plainly in Wikipedia’s own Machine learning entry, alongside the AIXI and Hutter Prize framing that the smallest program generating a string is its best compression — is the cleanest doorway an architect has into ML. The same entry examines three representative lossless compressors — LZW, LZ77 and PPM — as maps from strings into implicit feature spaces, which is exactly what a learned model does. You do not need a 70-billion-parameter model to walk through it. You need one European open-source library and an image off your own site camera.
←TODAY: In 2026 a laptop runs k-means over a facade photo in seconds — no pixel leaves the building, no API key changes hands. →3012: The Zurich-3012 office that owns its models owns its record; the one that rented cognition inherited someone else’s terms of service. Fulcrum: Compression and learning are the same operation — and both can run exactly where your data already lives.
The Tool: scikit-learn is the workhorse ML library of Python — classifiers, regressors, clustering, the lot — under a permissive BSD licence. It matters for a PAZ reader for a reason that is as much civic as technical: it was born inside INRIA, the French public research institute, out of David Cournapeau’s 2007 Google Summer of Code project, and it still ships as code you clone and run on your own hardware. No inference is phoned home. For a Swiss Büro that means the whole pipeline sits inside your data-residency perimeter by default — the opposite posture to a hosted model API.
Setup:
git clone https://github.com/scikit-learn/scikit-learn.git # the source, to read
python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install scikit-learn pillow numpy
python -c "import sklearn; print(sklearn.__version__)" # prints 1.x — you are live
First steps:
- Drop a site photo next to your script as
facade.jpg. - Run the block below. It flattens the image to a list of RGB pixels, runs k-means clustering — the same unsupervised algorithm the source names as a data-compression method — to find 16 representative colours, and repaints every pixel with its nearest centroid.
- Open
facade_16.png. You have just compressed a photograph by replacing thousands of distinct colours with 16, and you can see what the model decided mattered — the shadow line, the mullion, the sky gradient it flattened.
import numpy as np
from PIL import Image
from sklearn.cluster import KMeans
img = np.asarray(Image.open("facade.jpg")) / 255.0
w, h, d = img.shape
pixels = img.reshape(-1, d)
km = KMeans(n_clusters=16, n_init=4).fit(pixels)
out = km.cluster_centers_[km.labels_].reshape(w, h, d)
Image.fromarray((out * 255).astype("uint8")).save("facade_16.png")
This is not a toy. The lineage running under those two imports is the whole history the field tells about itself — from Arthur Samuel coining “machine learning” at IBM in 1959 while writing a checkers program that scored each side’s chance of winning, through Donald Hebb’s 1949 The Organization of Behavior that gave us the neuron-weighting intuition, to Tom Mitchell’s operational definition (a program learns from experience E at task T measured by P if P improves with E). The modern surge has dates too: AlexNet in 2012, when Krizhevsky, Sutskever and Hinton won ImageNet by a margin that made deep networks unignorable, and word2vec in 2013, when Tomáš Mikolov’s team learned word vectors from raw text at scale. k-means is the humble, transparent end of that same shelf — and it is the right end to start from, because you can inspect every decision it makes.
Atelier: For a working architecture or computational-design office, the move here is not “adopt AI” — it is relocate it. A local scikit-learn pipeline classifying your own drawing archive, clustering material samples, or quantising site imagery keeps every byte inside the office network and inside Swiss jurisdiction, which is exactly the property a hosted API cannot give you at any price. Your Monday move: take one small, real task the studio currently sends to a cloud tool — tagging photos, deduplicating a component library — and prototype it with scikit-learn on one machine, then read whether the local result is good enough before you renew the subscription.
Hack: Measure exactly what sixteen centroids threw away before you trust the compressed facade. k-means gives you the reconstruction error for free — the mean distance from each real pixel to the colour it got rounded to — so you never ship a lossy artefact on faith. Run this straight after fitting km above:
from sklearn.metrics import pairwise_distances_argmin_min
_, dist = pairwise_distances_argmin_min(pixels, km.cluster_centers_)
print(f"16 colours · mean error {dist.mean():.4f} · {len(pixels):,} px → 16 centroids")
Bump n_clusters to 8, then 32, and watch the error fall — you are reading the compression–fidelity trade-off with your own eyes, the same curve the Chinchilla result sits on at the far, expensive end.
The trade-off, stated plainly: a local model you own is bounded by your own hardware and your own labelled data, and it will not match a frontier hosted model on raw capability — that gap is real and worth naming. What you buy back is sovereignty over the data and the ability to audit every step. The Switzerland worth living in kept its digital independence because a few cantons insisted, late and almost by accident, on local data residency when nobody was watching the procurement clause. Federalism is a slowness budget; spend it. When your municipality or your practice procures software this year, read the data-residency line yourself — and if it is missing, write to whoever signs the contract. A `pip install` that keeps your data at home is one small, present-day way of building the future you would rather live in.
Learn-it:
- Repo: github.com/scikit-learn/scikit-learn — the BSD-licensed source, INRIA-rooted.
- Hands-on tutorial: Google Machine Learning Crash Course.
- Plain-English grounding: IBM — What is machine learning?
- Structured path: GeeksforGeeks Machine Learning Tutorial.
- Root concept: Machine learning — Wikipedia (read the compression section).
- Where it goes next: Google DeepMind — Science, for ML that reaches past the chatbot.
SOURCE · ↗
PAZ Kaffi · multidisciplinary editorial, led by PAZ Academy