from functools import lru_cache from pathlib import Path from fastapi import FastAPI from fastapi.staticfiles import StaticFiles from sklearn.datasets import make_blobs, make_s_curve, make_swiss_roll app = FastAPI() HERE = Path(__file__).parent @lru_cache(maxsize=1) def _datasets(): s, sl = make_s_curve(n_samples=5000, noise=0.03, random_state=0) sr, srl = make_swiss_roll(n_samples=5000, noise=0.15, random_state=0) b, bl = make_blobs( n_samples=5000, n_features=3, centers=5, cluster_std=1.0, random_state=0 ) return { "s_curve": { "name": "S-Curve", "path": "sklearn.datasets.make_s_curve", "description": ( "A 2-D manifold warped into R³. Continuous label encodes position " "along the curve — a good test of whether a reducer unrolls the " "sheet without tearing." ), "kind": "continuous", "points": s.tolist(), "labels": sl.tolist(), }, "swiss_roll": { "name": "Swiss Roll", "path": "sklearn.datasets.make_swiss_roll", "description": ( "A rolled-up plane. The canonical hard case for linear methods: " "PCA collapses the spiral, non-linear methods should recover the " "unroll." ), "kind": "continuous", "points": sr.tolist(), "labels": srl.tolist(), }, "blobs": { "name": "Gaussian Blobs", "path": "sklearn.datasets.make_blobs", "description": ( "Five isotropic Gaussian clusters in R³. Discrete class labels. " "Tests whether a reducer preserves cluster separation when " "projected to 2-D." ), "kind": "categorical", "points": b.tolist(), "labels": bl.tolist(), }, } @app.get("/data.json") def data(): return _datasets() app.mount("/", StaticFiles(directory=str(HERE), html=True), name="static")