Skip to content

KMeans question #269

Open
Open

Description

@ssimontacchi

Hi, Thanks for the awesome library!

So I am running a Kmeans on lots of different datasets, which all have roughly four shapes, so I initialize with those shapes and it works well, except for just a few times. There are a few datasets that look different enough that I end up with empty clusters and the algorithm just hangs ("Resumed because of empty cluster" again and again).

I conceptually understand why this happens, but is there any way you know to avoid it, or finish at least? I'm not sure I understand what's going on behind the scenes well enough to debug any further. Thank you!

Activity

  1. GillesVandewiele commented on Jun 20, 2020

    @GillesVandewiele
    Contributor

    Hi @ssimontacchi,

    What you are describing is a common problem of KMeans (not only when the custom variant to timeseries but also the scikit-learn variant has these issues). Therefore, the KMeans algorithm is often ran multiple times with different random initializations, some score such as silhouette_score is then used to decide which of all those random restarts was the most qualitative.

    On the other hand, the algorithm definitely should not hang indeed... I'll label this with "bug" for now!

  2. GillesVandewiele commented on Jun 20, 2020

    @GillesVandewiele
    Contributor

    Would it be possible to construct some minimal example with a small dataset? This would help a lot to debug

  3. rtavenar commented on Jun 20, 2020

    @rtavenar
    Member

    One important question is which init method are you using ?
    Then what would you suggest Gilles when a cluster is empty ? Does anyone know what sklearn does in this case ?

  4. GillesVandewiele commented on Jun 20, 2020

    @GillesVandewiele
    Contributor

    Good question... I think some check is required that checks if the total number of unique clusters is equal to the specified number of clusters, if that is not the case, some warning should be raised that the number of clusters is probably not set well (and perhaps also display the number of clusters with the highest silhouette that we found over the random initializations).

    Sklearn will just assign some random values to the cluster in case there is an empty cluster apparently. (source)

  5. rtavenar commented on Jun 29, 2020

    @rtavenar
    Member

    Sklearn will just assign some random values to the cluster in case there is an empty cluster apparently. (source)

    In the link you provide, they state that it's not chosen at random btw:

    A problem with k-means is that one or more clusters can be empty. However, this problem is accounted for in the current k-means implementation in scikit-learn. If a cluster is empty, the algorithm will search for the sample that is farthest away from the centroid of the empty cluster. Then it will reassign the centroid to be this farthest point.

    We'll have to check.

  6. ssimontacchi commented on Jul 1, 2020

    @ssimontacchi
    Author

    Hi, I have tried to make a minimal example but am having trouble recreating it (and can't share the datasets doing it, unfortunately). I believe the example in the colab is the general situation though.

    Again, it should be pretty fast on these datasets but just seems to hang. My guess of what is happening is that it's reassigning empty clusters indefinitely, but I'm not sure. Here is what the log looks like:

    WARNING: QApplication was not created in the main() thread.
    Resumed because of empty cluster
    Resumed because of empty cluster
    Resumed because of empty cluster
    Resumed because of empty cluster
    Resumed because of empty cluster
    Resumed because of empty cluster
    Resumed because of empty cluster
    Resumed because of empty cluster
    Resumed because of empty cluster
    Resumed because of empty cluster
    

    It only shows "Resumed because of empty cluster" 10 times though, and I'm not sure why that would happen either if it were failing to reassign clusters infinitely.

    I'm trying to figure out what's different about this data and I'll let you know when I figure it out. Thanks!

  7. ssimontacchi commented on Jul 3, 2020

    @ssimontacchi
    Author

    I'm really having a hard time getting it to hang but found that a lot, though not all, that were having this problem have tiny amounts of data. If you run it with only a few data points you will definitely get lots of empty cluster messages.

    I guess the question is figuring out if there is some condition that could cause it not to converge?

  8. felipeffm commented on Nov 15, 2021

    @felipeffm

    Columns with np.nan values return the same print ("Resumed because of empty cluster" )

  9. jccarrascog commented on Oct 11, 2022

    @jccarrascog

    Hi everyone! First of all, thanks for creating such an amazing package. I am struggling with an error for my dataset and I hope that anyone could help me out with this.

    I am getting the following error: "ValueError: cannot reshape array of size 0 into shape (0,newaxis)". Traces of the error can be found below.

    Code that generates the output:

    `from tslearn.clustering import TimeSeriesKMeans, KShape, silhouette_score

    km_euc = TimeSeriesKMeans(n_clusters=2, max_iter=5,metric="dtw", random_state=0).fit(tserie)
    labels_euc = km_euc.labels_`

    Error:

    `---------------------------------------------------------------------------
    ValueError Traceback (most recent call last)
    ~\AppData\Local\Temp/ipykernel_19116/338135866.py in
    3 start_time = time.time()
    4
    ----> 5 km_euc = TimeSeriesKMeans(n_clusters=2, max_iter=5,metric="dtw", random_state=0).fit(tserie)
    6 labels_euc = km_euc.labels_

    ~\Anaconda3\lib\site-packages\tslearn\clustering\kmeans.py in fit(self, X, y)
    778 print("Init %d" % (n_successful + 1))
    779 n_attempts += 1
    --> 780 self.fit_one_init(X, x_squared_norms, rs)
    781 if self.inertia_ < min_inertia:
    782 best_correct_centroids = self.cluster_centers_.copy()

    ~\Anaconda3\lib\site-packages\tslearn\clustering\kmeans.py in fit_one_init(self, X, x_squared_norms, rs)
    656 raise ValueError("Value %r for parameter 'init'"
    657 "is invalid" % self.init)
    --> 658 self.cluster_centers
    = check_full_length(self.cluster_centers)
    659 old_inertia = numpy.inf
    660

    ~\Anaconda3\lib\site-packages\tslearn\clustering\utils.py in _check_full_length(centroids)
    42 """
    43 resampler = TimeSeriesResampler(sz=centroids.shape[1])
    ---> 44 return resampler.fit_transform(centroids)
    45
    46

    ~\Anaconda3\lib\site-packages\tslearn\preprocessing\preprocessing.py in fit_transform(self, X, y, **kwargs)
    69 Resampled time series dataset.
    70 """
    ---> 71 return self.fit(X).transform(X)
    72
    73 def transform(self, X, y=None, **kwargs):

    ~\Anaconda3\lib\site-packages\tslearn\preprocessing\preprocessing.py in transform(self, X, y, **kwargs)
    95 sz = ts_size(X_[i])
    96 for di in range(d):
    ---> 97 f = interp1d(numpy.linspace(0, 1, sz), X_[i, :sz, di],
    98 kind="slinear")
    99 X_out[i, :, di] = f(xnew)

    ~\Anaconda3\lib\site-packages\scipy\interpolate\interpolate.py in init(self, x, y, kind, axis, copy, bounds_error, fill_value, assume_sorted)
    472 # Interpolation goes internally along the first axis
    473 self.y = y
    --> 474 self._y = self._reshape_yi(self.y)
    475 self.x = x
    476 del y, x # clean up namespace to prevent misuse; use attributes

    ~\Anaconda3\lib\site-packages\scipy\interpolate\polyint.py in _reshape_yi(self, yi, check)
    108 self._y_extra_shape[:-self._y_axis])
    109 raise ValueError("Data must be of shape %s" % ok_shape)
    --> 110 return yi.reshape((yi.shape[0], -1))
    111
    112 def _set_yi(self, yi, xi=None, axis=None):

    ValueError: cannot reshape array of size 0 into shape (0,newaxis)
    `

  10. michelegentili93 commented on Sep 10, 2025

    @michelegentili93

    Attempting to fix this: #593

    The main problems that I found are:

    when setting init='random', pick a random set of ts as cluster centroids; it may take the same ts multiple times (because in np.random.choice, replace is set to True). Therefore the "duplicated" clusters stays empty, no centroid is computed and then you get an error.
    When computing the distance between two ts with multiple dimensions, if there is only a nan in one or more of the dimensions (but not all), the distance will be inf. It expects that if there are nans should be on all the dimensions observed in the time series. EX: a ts with 3 timepoints and 2 dimensions =[[1,2,3],[2,3,na]] is not supported.
    In the Barycenter computation again it is assumed that either a ts has all nans in all dimensions.
    These 3 problems should be addressed:
    when init is random, we should make sure that each cluster is initialized on a different ts. Done
    when we have a nan in a subset of the dimension of the ts, don't use the ts only for that dimension for the computation of the distance and the barycenter. Done

  11. charavelg commented on Aug 19, 2026

    @charavelg
    Contributor

    I do agree that

    when init is random, we should make sure that each cluster is initialized on a different ts.

    It's ok since #594.

    Regarding,

    when we have a nan in a subset of the dimension of the ts, don't use the ts only for that dimension for the computation of the distance and the barycenter.

    I don't believe that's a common usage. Imputing the data beforehand can be a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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