KShape returns a sign-flipped centroid channel on multivariate series

Open Beginner friendly
#854 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
numpy, python

Research direction

Start in KShape._shape_extraction and compare the sign check for feature i with the per-channel behavior described in the issue. The reproduction uses a three-channel input and reports each centroid channel’s correlation and inertia. The change is done when all three correlations are 1.0 and inertia is approximately 0.

Written by the indexing model from the issue text.

Description

Describe the bug
In KShape._shape_extraction, the sign check for feature i computes Xp - mu_k and Xp + mu_k over all features of Xp, so the (sz, 1) eigenvector is broadcast across every channel. When channel 0 has the opposite sign to the other channels, the other channels decide the sign and channel 0 of the centroid comes out negated.

To Reproduce

import numpy as np
from tslearn.clustering import KShape

w = np.array([1.0, 2.0, 4.0, 3.0, 1.0, 0.0])
X = np.repeat(np.stack([w, -w, -w], axis=-1)[None], 3, axis=0)  # 3 identical series

ks = KShape(n_clusters=1, random_state=0).fit(X)
for ch in range(3):
    print(ch, round(np.corrcoef(X[0, :, ch], ks.cluster_centers_[0, :, ch])[0, 1], 3))
print("inertia", ks.inertia_)

Output on main (27e3753):

0 -1.0
1 1.0
2 1.0
inertia 0.4444444444444444

Expected behavior
Every centroid channel has correlation 1.0 with the input and the inertia is 0, since all three series are identical.

Environment (please complete the following information):

  • OS: macOS 26.7
  • tslearn version: 0.11.0.dev (main at 27e3753), Python 3.11, NumPy 2.4.6

Additional context
#844 compares each eigenvector with its own channel (Xp[:, :, i] against mu_k[:, 0]); with it the output above is 1.0 for all channels and inertia ~0.

Dominant language
Python
Stars
3.2k
Forks
418
Avg merge
2d 14h
Merged PRs (30d)
24

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tslearn-team/tslearn

All issues in tslearn-team/tslearn

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.