viewof trainMin = Inputs.range([0, 30], {
value: 15, step: 1, label: "Training data min (C):"
})
viewof trainMax = Inputs.range([20, 50], {
value: 25, step: 1, label: "Training data max (C):"
})
viewof testMin = Inputs.range([0, 40], {
value: 20, step: 1, label: "Test data min (C):"
})
viewof testMax = Inputs.range([20, 60], {
value: 35, step: 1, label: "Test data max (C):"
})Data Leakage Visualizer
Data Leakage Visualizer
animation
analytics-ml
normalization
Try Change Test data max (C):, then press Calculate.
Observe After Calculate, compare WRONG, CORRECT, and Absolute scaled-value difference.
Explain Explain why correct normalization exceeds 1.0 when the test value is outside the training range.
Technical boundaries. The comparison normalizes the test set midpoint using the full data range or training range. Each maximum is at least 1 above its minimum.
NoteTry It: Data Leakage Visualizer
Explore how normalizing before vs. after the train/test split changes the scaling parameters. Adjust the test set distribution to see how leakage distorts the training normalization.
{
const trMin = trainMin;
const trMax = Math.max(trainMax, trMin + 1);
const teMin = testMin;
const teMax = Math.max(testMax, teMin + 1);
// WRONG: fit on all data
const allMin = Math.min(trMin, teMin);
const allMax = Math.max(trMax, teMax);
// CORRECT: fit only on train
const correctMin = trMin;
const correctMax = trMax;
// Test a value from the test set midpoint
const testMid = (teMin + teMax) / 2;
const wrongNorm = (testMid - allMin) / (allMax - allMin);
const correctNorm = (testMid - correctMin) / (correctMax - correctMin);
const leakagePct = Math.abs(wrongNorm - correctNorm) * 100;
const w = 560;
const h = 200;
const absMin = Math.min(trMin, teMin) - 3;
const absMax = Math.max(trMax, teMax) + 3;
const scale = (v) => 60 + ((v - absMin) / (absMax - absMin)) * (w - 80);
return html`<div style="background: var(--bs-light, #f8f9fa); padding: 1.2rem; border-radius: 8px; border-left: 4px solid #E67E22; color: var(--bs-body-color);">
<h4 style="margin-top: 0; color: #2C3E50;">Train/Test Normalization Comparison</h4>
<svg width="${w}" height="${h}" style="font-family: Arial, sans-serif; max-width: 100%;" viewBox="0 0 ${w} ${h}">
<!-- Training range -->
<rect x="${scale(trMin)}" y="20" width="${scale(trMax) - scale(trMin)}" height="30" rx="4" fill="#3498DB" opacity="0.3" stroke="#3498DB" stroke-width="1.5"/>
<text x="${(scale(trMin) + scale(trMax)) / 2}" y="40" text-anchor="middle" font-size="12" fill="#2C3E50" font-weight="bold">Train [${trMin}-${trMax}]</text>
<!-- Test range -->
<rect x="${scale(teMin)}" y="60" width="${scale(teMax) - scale(teMin)}" height="30" rx="4" fill="#E67E22" opacity="0.3" stroke="#E67E22" stroke-width="1.5"/>
<text x="${(scale(teMin) + scale(teMax)) / 2}" y="80" text-anchor="middle" font-size="12" fill="#2C3E50" font-weight="bold">Test [${teMin}-${teMax}]</text>
<!-- Wrong: all-data range -->
<line x1="${scale(allMin)}" y1="110" x2="${scale(allMax)}" y2="110" stroke="#E74C3C" stroke-width="3"/>
<circle cx="${scale(allMin)}" cy="110" r="4" fill="#E74C3C"/>
<circle cx="${scale(allMax)}" cy="110" r="4" fill="#E74C3C"/>
<text x="${scale(allMin) - 2}" y="128" text-anchor="start" font-size="10" fill="#E74C3C">WRONG: fit on all [${allMin}-${allMax}]</text>
<!-- Correct: train-only range -->
<line x1="${scale(correctMin)}" y1="145" x2="${scale(correctMax)}" y2="145" stroke="#16A085" stroke-width="3"/>
<circle cx="${scale(correctMin)}" cy="145" r="4" fill="#16A085"/>
<circle cx="${scale(correctMax)}" cy="145" r="4" fill="#16A085"/>
<text x="${scale(correctMin) - 2}" y="163" text-anchor="start" font-size="10" fill="#16A085">CORRECT: fit on train [${correctMin}-${correctMax}]</text>
<!-- Test midpoint marker -->
<line x1="${scale(testMid)}" y1="15" x2="${scale(testMid)}" y2="170" stroke="#9B59B6" stroke-width="1" stroke-dasharray="4,3"/>
<text x="${scale(testMid)}" y="185" text-anchor="middle" font-size="10" fill="#9B59B6">test value = ${testMid.toFixed(0)}C</text>
</svg>
<table style="width: 100%; border-collapse: collapse; margin-top: 0.5rem; font-size: 0.9em; color: var(--bs-body-color);">
<tr style="border-bottom: 1px solid var(--bs-border-color, #dee2e6);">
<th scope="row" style="padding: 4px; font-weight: normal; text-align: left;"><span style="color: #E74C3C; font-weight: bold;">WRONG</span> (leakage)</th>
<td style="padding: 4px; font-family: monospace;">(${testMid.toFixed(0)} - ${allMin}) / (${allMax} - ${allMin}) = <strong>${wrongNorm.toFixed(3)}</strong></td>
</tr>
<tr style="border-bottom: 1px solid var(--bs-border-color, #dee2e6);">
<th scope="row" style="padding: 4px; font-weight: normal; text-align: left;"><span style="color: #16A085; font-weight: bold;">CORRECT</span> (no leakage)</th>
<td style="padding: 4px; font-family: monospace;">(${testMid.toFixed(0)} - ${correctMin}) / (${correctMax} - ${correctMin}) = <strong>${correctNorm.toFixed(3)}</strong></td>
</tr>
<tr>
<th scope="row" style="padding: 4px; font-weight: bold; text-align: left;">Absolute scaled-value difference</th>
<td style="padding: 4px; font-family: monospace; color: ${leakagePct > 10 ? "#E74C3C" : "#16A085"};">${leakagePct.toFixed(1)} percentage points of the 0–1 scale</td>
</tr>
</table>
<p style="margin-top: 0.5rem; margin-bottom: 0; font-size: 0.85em; color: #7F8C8D;">
${correctNorm > 1 ? "The correct normalization exceeds 1.0 because the test value is outside the training range. This is expected and correct -- it reveals the model is seeing unseen conditions." : correctNorm < 0 ? "The correct normalization is negative because the test value is below the training minimum. The wrong method hides this by using the full data range." : "Try making the test range very different from the training range to see how leakage hides the distribution shift."}
</p>
</div>`;
}