Problems
Loading...
1 / 1

Mean Squared Error (MSE)

Loss Functions
Easy

Compute the Mean Squared Error between predictions and targets.

Mathematical Definition

Mean Squared Error:

MSE=1Ni=0N1(ypred[i]ytrue[i])2\mathrm{MSE} = \frac{1}{N} \sum_{i=0}^{N-1} \left( y_{\text{pred}}[i] - y_{\text{true}}[i] \right)^{2}

where N is the number of samples.

Function Arguments

  • y_pred: array-like, shape (N,) - predicted values
  • y_true: array-like, shape (N,) - target values
Loading visualization...

Examples

Input: y_pred = [2, 3], y_true = [1, 1]

Output: MSE = 2.5

Input: y_pred = [0, 0, 0], y_true = [0, 0, 0]

Output: MSE = 0.0

Hint 1

Compute element-wise squared differences, then take the mean.

Hint 2

Use np.mean() on the squared differences.

Requirements

  • Convert inputs to NumPy arrays
  • Ensure shapes match (return None if mismatch)
  • Return a single float
  • NumPy only, no sklearn

Constraints

  • 1 ≤ N ≤ 10,000
  • NumPy only
  • Time limit: 200ms
Try Similar Problems