tslearn icon indicating copy to clipboard operation
tslearn copied to clipboard

MAE, MSE and MASE silently accept incompatible prediction dataset shapes

Open j45856021-dev opened this issue 1 month ago • 0 comments

Describe the bug

tslearn.metrics.performance.mae and mse reshape y_pred using the dimensions of y_true before calling scikit-learn. If the two datasets have the same total number of values but different numbers of time series, timestamps or features, this silently changes the prediction layout instead of rejecting the mismatch. mase inherits this through its call to mae.

For example, two bivariate series of length three and three bivariate series of length two can receive a perfect score even though their observations do not correspond.

To Reproduce

import numpy as np
from tslearn.metrics import performance

y_true = np.arange(12.0).reshape(2, 3, 2)
y_pred = y_true.reshape(3, 2, 2)

print(y_true.shape, y_pred.shape)
print(performance.mae(y_true, y_pred))
print(performance.mse(y_true, y_pred))
print(performance.mase(y_true, y_pred, train_data=y_true))

# Valid-shape controls:
print(performance.mae(y_true, y_true + 1))
print(performance.mse(y_true, y_true + 1))
print(performance.mase(y_true, y_true + 1, train_data=y_true))

Output on current main (5aaed32dc9b693a779dfcdd39ec3af605a0f922a):

(2, 3, 2) (3, 2, 2)
0.0
0.0
0.0
1.0
1.0
0.5

Changing the prediction shape to (2, 2, 3) or (2, 6, 1) also returns zero, so this affects timestamp/feature mismatches as well as the number of series.

Expected behavior

Raise a descriptive ValueError when the dataset shapes differ after to_time_series_dataset conversion. Equivalent representations of the same dataset, such as a single univariate series passed as (3,), (1, 3) or (1, 3, 1), should remain accepted. Valid inputs and their weighting should be unchanged.

Environment

  • macOS 26.7, arm64
  • Python 3.13.16
  • tslearn 0.10.0.dev, source checkout at 5aaed32dc9b693a779dfcdd39ec3af605a0f922a
  • NumPy 2.5.3, scikit-learn 1.9.1
  • Also reproduced with CPU PyTorch 2.14.1 inputs

Additional context

This is separate from the per-feature MASE scaling fixed in #742/#750. A focused regression suite reproduces the shape mismatch and checks valid normalized shapes and weighted results. A small shared validation before the two reshapes fixes the issue without changing the metric calculations.

j45856021-dev avatar Oct 12 '26 01:10 j45856021-dev