Skip to content

KShape clustering loses first time point #385

@uksa10

Description

@uksa10

Describe the bug
In order to visualize it better, I added 4 to the first time point. But I don't see any spike in the first index of the cluster centers array.
The whole array seems to be shifted 1 to the left
Additionally the last index of the cluster center array is filled up with a random number.
sinuscos
Sometimes there is a Spike in one of the clusters, but then the entire array gets shifted.

sinus

Is this expected behavior or a bug?

In other tests
To Reproduce

import numpy as np
import matplotlib.pyplot as plt
from tslearn.clustering import KShape
from tslearn.preprocessing import TimeSeriesScalerMeanVariance
x = np.arange(0, 10, 0.1)
sincos=np.zeros((20, 100), dtype=float)

for i in range (0,10):
 sincos[i] = np.sin(x)+np.random.normal(scale=0.1, size=len(x))
 sincos[i+10] = np.cos(x)+np.random.normal(scale=0.1, size=len(x))
 sincos[i,0]=sincos[i,0]+4
 sincos[10+i,0]=sincos[10+i,0]+4
sincos = TimeSeriesScalerMeanVariance().fit_transform(sincos)
ks = KShape(n_clusters=2,verbose=True)
y_pred = ks.fit_predict(sincos)
plt.figure(figsize=(13,5))
for i in range(2):
    plt.subplot(2, 1, 1 + i)
    for xx in sincos[y_pred == i]:
        plt.plot(xx[0:100].ravel(), "k-" ,alpha=.2)
    plt.plot(ks.cluster_centers_[i,0:100].ravel(), "r-")
    plt.xlim(0, 100)
    plt.ylim(-2, 4)
    plt.title(f"Cluster {str(i + 1)} has {str(sincos[y_pred == i].shape[0])} timeseries")
plt.tight_layout()
plt.show()

Environment (please complete the following information):

  • tslearn version 0.5.2

Additional context
Maybe it is because of the shifting operations in the KShape algorithm.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions