Try It: Normalization Methods Comparison
Try It: Normalization Methods Comparison
animation
analytics-ml
data-quality
Try Change 500 to 23.1 in Sensor values (comma-separated).
Observe Compare Raw, Min-Max, Z-Score, and Robust for highlight reading index 2.
Explain Describe how replacing 500 affects Min-Max, Z-Score, and Robust results.
Technical boundaries. At least 3 numeric values are required. Quartiles use sorted values at floor(n/4) and floor(3*n/4); zero range, standard deviation, or IQR returns 0 for the corresponding method.
Try It: Normalization Methods Comparison
Note
Enter a set of sensor values (including an outlier) to see how three normalization methods – Min-Max, Z-Score, and Robust Scaling – handle the data differently. Notice how outliers distort Min-Max and Z-Score but have less effect on Robust Scaling.
{
// Parse input values
const ncomp_raw = ncomp_values.split(",").map(s => parseFloat(s.trim())).filter(v => !isNaN(v));
if (ncomp_raw.length < 3) {
return html`<div style="background: var(--bs-light, #f8f9fa); padding: 1rem; border-radius: 8px; border-left: 4px solid #E74C3C; color: var(--bs-body-color);">
<p style="color: #E74C3C; font-weight: bold;">Please enter at least 3 comma-separated numeric values.</p>
</div>`;
}
const n = ncomp_raw.length;
const hlIdx = Math.min(ncomp_highlightIdx, n - 1);
// Min-Max scaling
const ncomp_min = Math.min(...ncomp_raw);
const ncomp_max = Math.max(...ncomp_raw);
const ncomp_mmRange = ncomp_max - ncomp_min;
const ncomp_minmax = ncomp_raw.map(v => ncomp_mmRange > 0 ? (v - ncomp_min) / ncomp_mmRange : 0);
// Z-Score normalization
const ncomp_mean = ncomp_raw.reduce((a, b) => a + b, 0) / n;
const ncomp_variance = ncomp_raw.reduce((a, v) => a + Math.pow(v - ncomp_mean, 2), 0) / n;
const ncomp_std = Math.sqrt(ncomp_variance);
const ncomp_zscore = ncomp_raw.map(v => ncomp_std > 0 ? (v - ncomp_mean) / ncomp_std : 0);
// Robust scaling (using median and IQR)
const ncomp_sorted = [...ncomp_raw].sort((a, b) => a - b);
const ncomp_median = n % 2 === 0
? (ncomp_sorted[n/2 - 1] + ncomp_sorted[n/2]) / 2
: ncomp_sorted[Math.floor(n/2)];
const ncomp_q1Idx = Math.floor(n / 4);
const ncomp_q3Idx = Math.floor(3 * n / 4);
const ncomp_q1 = ncomp_sorted[ncomp_q1Idx];
const ncomp_q3 = ncomp_sorted[ncomp_q3Idx];
const ncomp_iqr = ncomp_q3 - ncomp_q1;
const ncomp_robust = ncomp_raw.map(v => ncomp_iqr > 0 ? (v - ncomp_median) / ncomp_iqr : 0);
// Check for outlier distortion
const ncomp_rawNoOutlier = ncomp_raw.filter((_, i) => i !== hlIdx);
const ncomp_minNoOut = Math.min(...ncomp_rawNoOutlier);
const ncomp_maxNoOut = Math.max(...ncomp_rawNoOutlier);
const ncomp_rangeNoOut = ncomp_maxNoOut - ncomp_minNoOut;
const ncomp_distortion = ncomp_mmRange > 0 && ncomp_rangeNoOut > 0
? ((ncomp_mmRange / ncomp_rangeNoOut) - 1) * 100
: 0;
const viewportWidth = typeof width !== "undefined" ? width : window.innerWidth;
const compact = viewportWidth < 640;
const metricMin = compact ? 130 : 160;
const ncomp_highlightCards = [
{label: "Raw", value: ncomp_raw[hlIdx].toFixed(1), color: "#2C3E50"},
{label: "Min-Max", value: ncomp_minmax[hlIdx].toFixed(4), color: "#3498DB"},
{label: "Z-Score", value: ncomp_zscore[hlIdx].toFixed(4), color: "#16A085"},
{label: "Robust", value: ncomp_robust[hlIdx].toFixed(4), color: "#E67E22"}
];
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); margin-top: 0.5rem;">
${compact
? html`<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 0.7rem; margin-bottom: 0.9rem;">
${ncomp_highlightCards.map(card => html`<div style="background: var(--bs-body-bg, #fff); border: 1px solid #dde4ea; border-radius: 8px; padding: 0.7rem; text-align: center;">
<div style="font-size: 0.8rem; opacity: 0.7;">${card.label}</div>
<div style="font-size: 1.2rem; font-weight: 700; color: ${card.color};">${card.value}</div>
</div>`)}
</div>`
: html`<div style="overflow-x: auto; margin-bottom: 0.8rem;">
<table style="width: 100%; border-collapse: collapse; font-size: 0.8rem;">
<thead>
<tr style="border-bottom: 2px solid #2C3E50;">
<th style="padding: 6px; text-align: left; min-width: 100px;">Method</th>
${ncomp_raw.map((_, i) => html`<th style="padding: 6px; text-align: center; ${i === hlIdx ? 'background: #ffeaa7;' : ''}">v${i}</th>`)}
</tr>
</thead>
<tbody>
<tr style="border-bottom: 1px solid #ddd;">
<td style="padding: 6px; font-weight: bold; color: #2C3E50;">Raw</td>
${ncomp_raw.map((v, i) => html`<td style="padding: 6px; text-align: center; ${i === hlIdx ? 'background: #ffeaa7; font-weight: bold;' : ''}">${v.toFixed(1)}</td>`)}
</tr>
<tr style="border-bottom: 1px solid #ddd;">
<td style="padding: 6px; font-weight: bold; color: #3498DB;">Min-Max</td>
${ncomp_minmax.map((v, i) => html`<td style="padding: 6px; text-align: center; ${i === hlIdx ? 'background: #ffeaa7; font-weight: bold;' : ''}">${v.toFixed(4)}</td>`)}
</tr>
<tr style="border-bottom: 1px solid #ddd;">
<td style="padding: 6px; font-weight: bold; color: #16A085;">Z-Score</td>
${ncomp_zscore.map((v, i) => html`<td style="padding: 6px; text-align: center; ${i === hlIdx ? 'background: #ffeaa7; font-weight: bold;' : ''}">${v.toFixed(4)}</td>`)}
</tr>
<tr>
<td style="padding: 6px; font-weight: bold; color: #E67E22;">Robust</td>
${ncomp_robust.map((v, i) => html`<td style="padding: 6px; text-align: center; ${i === hlIdx ? 'background: #ffeaa7; font-weight: bold;' : ''}">${v.toFixed(4)}</td>`)}
</tr>
</tbody>
</table>
</div>`}
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(${metricMin}px, 1fr)); gap: 1rem; text-align: center;">
<div>
<div style="font-size: 0.8rem; color: var(--bs-body-color); opacity: 0.7;">Mean / Std Dev</div>
<div style="font-size: 1rem; font-weight: bold; color: #16A085;">${ncomp_mean.toFixed(2)} / ${ncomp_std.toFixed(2)}</div>
</div>
<div>
<div style="font-size: 0.8rem; color: var(--bs-body-color); opacity: 0.7;">Median / IQR</div>
<div style="font-size: 1rem; font-weight: bold; color: #E67E22;">${ncomp_median.toFixed(2)} / ${ncomp_iqr.toFixed(2)}</div>
</div>
<div>
<div style="font-size: 0.8rem; color: var(--bs-body-color); opacity: 0.7;">Min-Max Range</div>
<div style="font-size: 1rem; font-weight: bold; color: #3498DB;">${ncomp_mmRange.toFixed(2)}</div>
</div>
<div>
<div style="font-size: 0.8rem; color: var(--bs-body-color); opacity: 0.7;">Range Distortion</div>
<div style="font-size: 1rem; font-weight: bold; color: ${ncomp_distortion > 100 ? '#E74C3C' : '#16A085'};">${ncomp_distortion.toFixed(0)}%</div>
</div>
</div>
<p style="margin: 0.8rem 0 0 0; font-size: 0.85rem; text-align: center; color: var(--bs-body-color); opacity: 0.8;"><strong>Key insight:</strong> ${ncomp_distortion > 100
? "The highlighted value inflates the Min-Max range by " + ncomp_distortion.toFixed(0) + "%, compressing all normal values near zero. Robust Scaling uses median and IQR, which are resistant to this distortion. This demonstrates why you should clean outliers before normalizing -- or use Robust Scaling when outliers cannot be removed."
: "With no extreme outliers, all three methods produce reasonable results. Try adding an outlier (e.g., 500) to see how Min-Max and Z-Score get distorted while Robust Scaling remains stable."}</p>
</div>`;
}