Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KShape clustering loses first time point #385

Open
uksa10 opened this issue Feb 15, 2022 · 0 comments
Open

KShape clustering loses first time point #385

uksa10 opened this issue Feb 15, 2022 · 0 comments
Labels

Comments

@uksa10
Copy link

uksa10 commented Feb 15, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant