TheAlgorithms/Python · error · ValueError
The length of the two arrays should be the same.
Error message
The length of the two arrays should be the same.
What it means
Thrown by mean_absolute_percentage_error when y_true and y_pred have different lengths. MAPE averages |(y_true - y_pred)/y_true| per element, so both arrays must be aligned; zero y_true entries are replaced by epsilon to avoid division by zero.
Source
Thrown at machine_learning/loss_functions.py:476
Examples:
>>> y_true = np.array([10, 20, 30, 40])
>>> y_pred = np.array([12, 18, 33, 45])
>>> float(mean_absolute_percentage_error(y_true, y_pred))
0.13125
>>> y_true = np.array([1, 2, 3, 4])
>>> y_pred = np.array([2, 3, 4, 5])
>>> float(mean_absolute_percentage_error(y_true, y_pred))
0.5208333333333333
>>> y_true = np.array([34, 37, 44, 47, 48, 48, 46, 43, 32, 27, 26, 24])
>>> y_pred = np.array([37, 40, 46, 44, 46, 50, 45, 44, 34, 30, 22, 23])
>>> float(mean_absolute_percentage_error(y_true, y_pred))
0.064671076436071
"""
if len(y_true) != len(y_pred):
raise ValueError("The length of the two arrays should be the same.")
y_true = np.where(y_true == 0, epsilon, y_true)
absolute_percentage_diff = np.abs((y_true - y_pred) / y_true)
return np.mean(absolute_percentage_diff)
def perplexity_loss(
y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-7
) -> float:
"""
Calculate the perplexity for the y_true and y_pred.
Compute the Perplexity which useful in predicting language model
accuracy in Natural Language Processing (NLP.)
Perplexity is measure of how certain the model in its predictions.
Perplexity Loss = exp(-1/N (Σ ln(p(x)))View on GitHub (pinned to f5988cc097)
Solutions
- Align both arrays to the same evaluation window before computing MAPE.
- If using pandas, drop NaN rows jointly: df = df.dropna(subset=['y_true','y_pred']).
- Add an assert len(y_true) == len(y_pred) guard in the eval script.
Example fix
# before y_true = np.array([1, 2, 3]) y_pred = np.array([2, 3, 4, 5]) mean_absolute_percentage_error(y_true, y_pred) # after y_true = np.array([1, 2, 3, 4]) mean_absolute_percentage_error(y_true, y_pred)
Defensive patterns
Strategy: validation
Validate before calling
assert len(y_true) == len(y_pred) mape = mean_absolute_percentage_error(y_true, y_pred)
Type guard
def same_length(y_true: np.ndarray, y_pred: np.ndarray) -> bool:
return len(y_true) == len(y_pred) Prevention
- In pandas, dropna jointly over both columns before extracting arrays.
- Treat zero y_true values carefully: they are silently replaced by epsilon here, which can inflate MAPE.
- Standardize the evaluation window length across all metrics.
When it happens
Trigger: Calling mean_absolute_percentage_error with len(y_true) != len(y_pred), e.g. 4 truth values vs 4 predictions is fine but 4 vs 5 raises.
Common situations: Time-series evaluation where predicted horizon length differs from the label window; dataframes aligned by index with NaNs dropped in one column only.
Related errors
- Input arrays must have the same length.
- Input arrays must have the same shape.
- y_true must be one-hot encoded.
- Predicted probabilities must sum to approximately 1.
- Shape of y_true and y_pred must be the same.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/5f008444776167f5.
Report an issue: GitHub.