Data Science Residual Diagnostics
Look for structure in errors rather than trusting a single summary score.
How it works
A residual is observed minus predicted outcome. Residual patterns can reveal curvature, changing spread, time dependence or unusual observations that a scalar metric hides. A mean residual near zero is a property of many fitted models with an intercept, not a guarantee that individual predictions are good. Inspect residuals against fitted values, predictors and collection order.
Run the code
Try in playground opens the correct numpy editor and preloads the complete source. Choose Run to execute; loading does not execute it. The isolated browser runtime does not read personal PC files.
Local review: Python 3.12.14, NumPy 2.5.4, Pandas 3.0.6 and SciPy 1.18.0. Browser review: Pyodide 0.27.7, Python 3.12.7, NumPy 2.0.2, Pandas 2.2.3 and SciPy 1.14.1 in their respective editors. Save the download as residuals.py and run python residuals.py in your separate environment. Apache and MySQL are not needed. Environment setup.
Example
import numpy as np
x = np.arange(-2., 3.)
y = x*x
X = np.column_stack([np.ones(x.size), x])
coef = np.linalg.lstsq(X, y, rcond=None)[0]
residual = y-X@coef
assert np.isclose(residual.mean(), 0., atol=1e-12)
assert residual[0] > 0 and residual[2] < 0 and residual[-1] > 0
print("Mean residual near zero; curved residual pattern remains")
Expected result
Mean residual near zero; curved residual pattern remains
The response is quadratic, so a straight-line fit leaves positive residuals at both ends and a negative residual in the middle. The zero mean conceals that shape. This exact small fixture lets you predict the diagnostic before running it. A real analysis should visualise residuals and connect patterns to possible measurement or modelling problems.
Technical references: NumPy, Pandas and SciPy. For evaluation design see the scikit-learn guide to leakage; these examples do not require scikit-learn.