【问题标题】:Why is data assigned to the cluster reducing with the number of clusters?为什么分配给集群的数据会随着集群的数量而减少?
【发布时间】:2019-01-09 23:33:37
【问题描述】:

这是我用来对时间序列数据进行聚类的代码示例。我的数据是 12153 个长度相同的样本。

当我对我的数据进行聚类时,我意识到分配给聚类的数据样本会减少聚类的数量。例如,当集群为两个时,分配只有 12151 个样本。当集群为 3 时,分配有 12150 等等。我不明白为什么会这样。我在下面的代码中做错了什么吗?

def k_means_clust_eucl(self, data, initial_centroids):
            '''
            k-means clustering algorithm for time series data.
            using  Euclidean distance
            '''
            # create random centroids
            while True:
                orig = [i for i in range(12153)]
                self.new_centroids = deepcopy(self.centroids)
                # print('iteration ' + str(self.i))
                # assign data points to clusters
                self.assignments = {}
                # print('while_clustering :', len(data))
                for ind, i in enumerate(data):
                    min_dist = float('inf')
                    closest_clust = None
                    for c_ind, j in enumerate(self.centroids):
                        cur_dist = self.euclid_dist(i, j)
                        if cur_dist < min_dist:
                            min_dist = cur_dist
                            closest_clust = c_ind
                    if closest_clust in self.assignments:
                        self.assignments[closest_clust].append(ind)
                        if ind in orig:
                            orig.remove(ind)
                        else:
                            print(ind)
                    else:
                        print('not in assignment')
                        self.assignments[closest_clust] = []
                print(orig)

【问题讨论】:

  • 可能并非所有数据点都被聚类。有些被排除在外并被视为异常值。您是先执行任何类型的数据预处理,还是直接对数据应用聚类算法?
  • 我在聚类之前对数据进行了标准化
  • 您检查异常值了吗?使用箱线图可视化标准化数据中的异常值。

标签: python-3.x time-series cluster-analysis k-means


【解决方案1】:

因为你忘了把每个簇的first点放到新创建的簇中。

相反,在第一个点之后,您的集群是[]

【讨论】:

    猜你喜欢
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2017-03-18
    • 1970-01-01
    • 2018-09-26
    • 2018-06-08
    相关资源
    最近更新 更多