tslearn-team/tslearn

silhouette_score crashes on a pure python list.

GillesVandewiele opened this issue · 0 comments

Describe the bug
When passing a list to silhouette_score as such:

silhouette_score(X, tsk.labels_, metric='euclidean')

with X a pure python list. It crashes:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-77a8b5db76ff> in <module>
      4     tsk = TimeSeriesKMeans(n_clusters=k)
      5     tsk.fit(X)
----> 6     print(k, silhouette_score(X, tsk.labels_, metric='euclidean'))

/usr/local/lib/python3.8/dist-packages/tslearn/clustering/utils.py in silhouette_score(X, labels, metric, sample_size, metric_params, n_jobs, verbose, random_state, **kwds)
    177     elif metric == "euclidean":
    178         X_ = to_time_series_dataset(X)
--> 179         X_ = X_.reshape((X.shape[0], -1))
    180         sklearn_X = cdist(X_, X_, metric="euclidean")
    181     else:

AttributeError: 'list' object has no attribute 'shape'

Passing np.array(X) instead fixes this issue,

Expected behavior
One would expect tslearn to do the conversion to numpy array when a list is passed.