Neural Mastery
← Back to Practice

Weighted RMSE

Difficulty: Medium · Concept: Linear Regression — The Cost Function

A harder variant of Mean Squared Error From Scratch: real training data is often not equally trustworthy — some examples might be known to be noisier, less representative, or more expensive to get wrong, and a per-example weight lets the loss reflect that directly. This also switches from MSE to RMSE (take the square root at the end) — same information, but back in the original units of the target, which is what makes an error number actually interpretable ("off by 2 degrees," not "off by 4 degrees-squared").

weighted RMSE=iwi(y^(i)y(i))2iwi\text{weighted RMSE} = \sqrt{\frac{\sum_i w_i (\hat{y}^{(i)} - y^{(i)})^2}{\sum_i w_i}}

The genuine edge case: if every weight is 0 (or the weights list is otherwise degenerate), the denominator is 0 and the result is undefined — a real implementation needs to raise, not silently produce a ZeroDivisionError or a nan.

Your task: implement weighted_rmse(y_true, y_pred, weights). Raise ValueError if the three lists aren't the same length, or if the weights sum to 0.

Implement it yourself
assert weighted_rmse([1, 2, 3], [1, 2, 3], [1, 1, 1]) == 0.0 assert weighted_rmse([0, 0], [1, 1], [1, 1]) == 1.0 assert abs(weighted_rmse([0, 0, 0], [2, 0, 0], [1, 1, 1]) - (4 / 3) ** 0.5) < 1e-9 assert weighted_rmse([0, 0], [10, 0], [0, 1]) == 0.0 assert check_raises(ValueError, weighted_rmse, [1, 2], [1, 2], [0, 0]) == True

Next: One Gradient Descent Step for Linear Regression

Last updated Sep 5, 2026Edit this pageReport an issue