{"spec_id":"learning-curve-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// learning-curve-basic: Model Learning Curve\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 85/100 | Updated: 2026-10-23\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport { Box, Typography } from \"@mui/material\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst METRIC = \"Classification Accuracy (%)\"; // the score both curves report, as a 0–1 fraction\n\n// Columns: set these to your data's column names\nconst TRAIN_SIZE = \"Training Set Size (labeled emails)\"; // role: train_sizes\nconst TRAIN_MEAN = \"Training score\"; // role: train_scores (mean across folds)\nconst TRAIN_STD = \"Training score std\"; // role: train_scores (std across folds)\nconst VAL_MEAN = \"Validation score\"; // role: validation_scores (mean across folds)\nconst VAL_STD = \"Validation score std\"; // role: validation_scores (std across folds)\n\n// Data: replace this block with your own records, one object per training size\n// A spam-filter classifier evaluated at 10 training-set sizes via 8-fold\n// cross-validation. Training accuracy drifts slightly downward as the fixed\n// model has to fit more varied examples, while validation accuracy climbs and\n// the two converge — but don't fully close — as more labeled email tightens\n// the ±1 std-dev spread across folds.\nconst data = [\n  { [TRAIN_SIZE]: 200, [TRAIN_MEAN]: 0.985, [TRAIN_STD]: 0.012, [VAL_MEAN]: 0.76, [VAL_STD]: 0.055 },\n  { [TRAIN_SIZE]: 400, [TRAIN_MEAN]: 0.978, [TRAIN_STD]: 0.01, [VAL_MEAN]: 0.82, [VAL_STD]: 0.045 },\n  { [TRAIN_SIZE]: 800, [TRAIN_MEAN]: 0.968, [TRAIN_STD]: 0.009, [VAL_MEAN]: 0.868, [VAL_STD]: 0.035 },\n  { [TRAIN_SIZE]: 1200, [TRAIN_MEAN]: 0.96, [TRAIN_STD]: 0.008, [VAL_MEAN]: 0.892, [VAL_STD]: 0.028 },\n  { [TRAIN_SIZE]: 1600, [TRAIN_MEAN]: 0.955, [TRAIN_STD]: 0.007, [VAL_MEAN]: 0.905, [VAL_STD]: 0.023 },\n  { [TRAIN_SIZE]: 2000, [TRAIN_MEAN]: 0.951, [TRAIN_STD]: 0.006, [VAL_MEAN]: 0.913, [VAL_STD]: 0.02 },\n  { [TRAIN_SIZE]: 2400, [TRAIN_MEAN]: 0.948, [TRAIN_STD]: 0.006, [VAL_MEAN]: 0.919, [VAL_STD]: 0.017 },\n  { [TRAIN_SIZE]: 2800, [TRAIN_MEAN]: 0.946, [TRAIN_STD]: 0.005, [VAL_MEAN]: 0.923, [VAL_STD]: 0.015 },\n  { [TRAIN_SIZE]: 3200, [TRAIN_MEAN]: 0.944, [TRAIN_STD]: 0.005, [VAL_MEAN]: 0.926, [VAL_STD]: 0.014 },\n  { [TRAIN_SIZE]: 3600, [TRAIN_MEAN]: 0.943, [TRAIN_STD]: 0.004, [VAL_MEAN]: 0.928, [VAL_STD]: 0.013 },\n];\n\n// Plot: reads only data and the constants above\nconst sizes = data.map((d) => d[TRAIN_SIZE]);\nconst trainMean = data.map((d) => d[TRAIN_MEAN]);\nconst valMean = data.map((d) => d[VAL_MEAN]);\n\n// ±1 std-dev confidence band via the stacked-area trick: a fully transparent\n// \"lower\" series carries the band's floor, and a \"width\" series (2×std)\n// stacked on top of it fills exactly the [mean-std, mean+std] interval.\nconst trainLower = data.map((d) => d[TRAIN_MEAN] - d[TRAIN_STD]);\nconst trainWidth = data.map((d) => 2 * d[TRAIN_STD]);\nconst valLower = data.map((d) => d[VAL_MEAN] - d[VAL_STD]);\nconst valWidth = data.map((d) => 2 * d[VAL_STD]);\n\n// y limits: the bands' extent, padded, capped at a perfect score\nconst bandLow = Math.min(...trainLower, ...valLower);\nconst bandHigh = Math.max(...data.map((d) => Math.max(d[TRAIN_MEAN] + d[TRAIN_STD], d[VAL_MEAN] + d[VAL_STD])));\nconst pad = (bandHigh - bandLow) * 0.08;\nconst yMin = Math.max(0, bandLow - pad);\nconst yMax = Math.min(1, bandHigh + pad);\n\nconst TITLE = \"learning-curve-basic · javascript · muix · anyplot.ai\";\nconst pct = (v) => `${(v * 100).toFixed(1)}%`;\n\n// --- Chart (default-exported component — the harness mounts it) ------------\nexport default function Chart() {\n  const size = window.ANYPLOT_SIZE;\n  const titleH = 64;\n  const legendH = 40;\n  const chartH = size.height - titleH - legendH;\n\n  return (\n    <Box\n      sx={{\n        width: size.width,\n        height: size.height,\n        boxSizing: \"border-box\",\n        display: \"flex\",\n        flexDirection: \"column\",\n      }}\n    >\n      <Box sx={{ height: titleH, display: \"flex\", alignItems: \"center\", justifyContent: \"center\" }}>\n        <Typography sx={{ color: t.ink, fontSize: 22, fontWeight: 600 }}>{TITLE}</Typography>\n      </Box>\n      <LineChart\n        width={size.width}\n        height={chartH}\n        skipAnimation\n        margin={{ top: 20, right: 40, bottom: 8, left: 24 }}\n        series={[\n          { id: \"trainLower\", data: trainLower, stack: \"trainBand\", area: true, curve: \"linear\", color: t.palette[0] },\n          { id: \"trainBand\", data: trainWidth, stack: \"trainBand\", area: true, curve: \"linear\", color: t.palette[0] },\n          {\n            id: \"trainMean\",\n            data: trainMean,\n            label: TRAIN_MEAN,\n            color: t.palette[0],\n            showMark: true,\n            shape: \"circle\",\n            curve: \"linear\",\n            valueFormatter: pct,\n          },\n          { id: \"valLower\", data: valLower, stack: \"valBand\", area: true, curve: \"linear\", color: t.palette[1] },\n          { id: \"valBand\", data: valWidth, stack: \"valBand\", area: true, curve: \"linear\", color: t.palette[1] },\n          {\n            id: \"valMean\",\n            data: valMean,\n            label: VAL_MEAN,\n            color: t.palette[1],\n            showMark: true,\n            shape: \"circle\",\n            curve: \"linear\",\n            valueFormatter: pct,\n          },\n        ]}\n        xAxis={[\n          {\n            data: sizes,\n            scaleType: \"linear\",\n            label: TRAIN_SIZE,\n            height: 64,\n            tickNumber: 10,\n            tickLabelStyle: { fontSize: 14 },\n            labelStyle: { fontSize: 16 },\n          },\n        ]}\n        yAxis={[\n          {\n            min: yMin,\n            max: yMax,\n            label: METRIC,\n            valueFormatter: (v) => `${Math.round(v * 100)}%`,\n            width: 84,\n            tickLabelStyle: { fontSize: 14 },\n            labelStyle: { fontSize: 16 },\n          },\n        ]}\n        grid={{ horizontal: true }}\n        slotProps={{\n          legend: {\n            position: { vertical: \"bottom\", horizontal: \"center\" },\n            direction: \"horizontal\",\n            sx: { fontSize: 16 },\n          },\n        }}\n        sx={{\n          '& .MuiLineChart-line[data-series=\"trainMean\"], & .MuiLineChart-line[data-series=\"valMean\"]': {\n            strokeWidth: 3,\n          },\n          '& .MuiLineChart-line[data-series=\"trainLower\"], & .MuiLineChart-line[data-series=\"valLower\"]': {\n            stroke: \"none\",\n          },\n          '& .MuiLineChart-line[data-series=\"trainBand\"]': { stroke: t.palette[0], strokeWidth: 1, strokeOpacity: 0.5 },\n          '& .MuiLineChart-line[data-series=\"valBand\"]': { stroke: t.palette[1], strokeWidth: 1, strokeOpacity: 0.5 },\n          '& .MuiLineChart-area[data-series=\"trainLower\"], & .MuiLineChart-area[data-series=\"valLower\"]': {\n            fillOpacity: 0,\n          },\n          '& .MuiLineChart-area[data-series=\"trainBand\"], & .MuiLineChart-area[data-series=\"valBand\"]': {\n            fillOpacity: 0.18,\n          },\n          '& .MuiLineChart-mark[data-series=\"trainMean\"]': { r: 7, fill: t.palette[0], stroke: t.pageBg, strokeWidth: 2 },\n          '& .MuiLineChart-mark[data-series=\"valMean\"]': { r: 7, fill: t.palette[1], stroke: t.pageBg, strokeWidth: 2 },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n        }}\n      />\n    </Box>\n  );\n}\n"}