time-series-classification-and-clustering icon indicating copy to clipboard operation
time-series-classification-and-clustering copied to clipboard

the k_means_clust function has a error

Open kylinLiu opened this issue 7 years ago • 2 comments

if closest_clust in assignments:
    assignments[closest_clust].append(ind)
else:
    assignments[closest_clust]=[]

this code is error! when the element match the clus , the element will not append to the clust ,it make the result error!the right code like this:

assignments.setdefault(closest_clust,[])
assignments[closest_clust].append(ind)

kylinLiu avatar Aug 10 '17 07:08 kylinLiu

Thank you for pointing it out!

poweihuang17 avatar May 11 '18 03:05 poweihuang17

Thank you, this fixes the frequent error of trying to assign an oddly empty assignment to a centroid(clust_sum was still 0 at time of iteration) on line 47 of ts_cluster.py: https://github.com/alexminnaar/time-series-classification-and-clustering/blob/master/clustering/ts_cluster.py#L47

In many cases I would randomly see this error; especially for cases of many clusters as assignments was more likely to have missed an update that your above fix addresses.

Traceback (most recent call last): File "study_clustering_based_selection.py", line 584, in dtw_clustering(list(X), n_clusters) File "study_clustering_based_selection.py", line 549, in dtw_clustering tsc.k_means_clust(data=X, num_iter=3, w=5, progress=True) File "study_clustering_based_selection.py", line 223, in k_means_clust self.centroids[key]=[m/len(self.assignments[key]) for m in clust_sum] TypeError: 'int' object is not iterable

DMTSource avatar Dec 03 '20 02:12 DMTSource